Skip to content
Discussion options

You must be logged in to vote

Thanks @sueun-dev for the precise analysis and workaround, the default exit stack is keyed by the function, so concurrent calls of the same injectable share one stack, which is why per-event cleanup_exit_stack_of_func() races with parallel work (one event's cleanup tears down another's in-flight resources), and why cleaning only after the TaskGroup leaks for long-running consumers.

So I made a injectable_scope in v1.5.0, injectable_scope() gives each unit of work its own exit stack and cache, with contextvars keeping parallel tasks isolated — the same request-scoped model FastAPI uses internally. Cleanup runs when each scope's async with block exits, so there's no global accumulation. Your

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@thomas-touhey
Comment options

Comment options

You must be logged in to vote
1 reply
@thomas-touhey
Comment options

Answer selected by JasperSui
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants