Thoughts about loop issue #3171
Replies: 1 comment 1 reply
-
|
I would look closely at this line: event_dispatcher: EventDispatcher = ContextVar(
"event_dispatcher", default=EventDispatcher()
).get()That does not leave you with a per-request So if I would keep the actual |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
We recently experienced middleware being in a 'loop' on our production environment. I'm wondering if somebody has a clever idea as to what logic issue we're hitting here.
I built an event/listener system for our FastAPI application. To that end, we have the following middleware, which 'flushes' events that were 'deferred' (my own terminology) during the request:
Recently, we hit a bug that caused the
flushcall to fail. Full traceback:So far, so good. But this exception started occurring at least once every second (and not that many requests were coming in, so I doubt it's some ASGI race condition).
In other words, our middleware was in a loop. I have not found any logic for Starlette to 'retry' middleware. I did find https://starlette.dev/middleware/#asgi-middleware-should-be-stateless, which states that "middleware should be stateless", which is a bit of a grey area in my case...
event_dispatcheris aContextVarinstance ofEventDispatcher:... which contains a list of event listeners to handle:
... and therefore arguably contains state outside
__call__as the documentation prescribes, but not with the ability to "lead to conflicting variable reads/writes across requests" asContextVars are per-request. (Let me know if I'm skipping a thought here.)I'm unable to reproduce this issue locally, causing the same
ValidationErroras in production:I haven't been able to wrap my head around how all this came together to form an infinite loop (I had to restart Uvicorn to 'fix' this).
Clever ideas?
Beta Was this translation helpful? Give feedback.
All reactions