You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Unshielded session teardown in streamable_http_client aborts DELETE under cancellation, leaking server session #3877
When caller execution exits due to cancellation or timeout (for example, wrapping the client session in anyio.move_on_after(...) or cancelling the enclosing task), the active cancel scope is already cancelled when entering the finally: block.
Because transport.terminate_session(client) performs an asynchronous HTTP DELETE request without a shielded cancel scope:
The DELETE request raises CancelledError (or anyio.get_cancelled_exc_class()) immediately at the first checkpoint.
terminate_session catches Exception, but not BaseException / CancelledError. The exception propagates, aborting the DELETE request before it can reach the server.
The server session is leaked and remains open until remote timeout.
Line 792 (tg.cancel_scope.cancel()) is never executed because the exception unwinds out of the finally block before reaching it.
Expected Behavior
Teardown logic in finally: blocks that issues network calls should be executed inside a shielded cancel scope (with an optional bounded timeout) so that cancellation of the client caller does not prevent sending the DELETE session termination request, nor block task group cancellation:
Independently reproduced this on main at 77001c9 with a real Client and stateful server through the in-process HTTP transport. Cancellation before DELETE, and cancellation after DELETE has started, both leave the old session accepting ping (HTTP 200). Normal close makes that same session return 404.
I've prepared regression coverage for those paths, terminate_on_close=False, and a stalled DELETE. A candidate fix using bounded, shielded cleanup passes all 6,241 tests with 100% coverage locally. The proposed five-second bound also limits normal teardown when the configured HTTP timeout is longer, so that budget needs a maintainer decision.
@shivamm-gupta, happy to contribute the regression tests to your implementation rather than open a competing PR.
Investigation, tests, and candidate patch were AI-assisted with OpenAI Codex; the before/after checks were executed locally.
The five-second limit is the part I'd be careful with. Shielding cleanup makes sense, but I'd probably keep the timeout configurable rather than silently overriding the client's existing HTTP timeout. Otherwise a slow but valid DELETE could still leave the session around. Did you test what happens when the server takes longer than five seconds to process the termination?
Activity
ar-ramchandra commentedon Nov 19, 2026
Independently reproduced this on main at 77001c9 with a real Client and stateful server through the in-process HTTP transport. Cancellation before DELETE, and cancellation after DELETE has started, both leave the old session accepting ping (HTTP 200). Normal close makes that same session return 404.
I've prepared regression coverage for those paths, terminate_on_close=False, and a stalled DELETE. A candidate fix using bounded, shielded cleanup passes all 6,241 tests with 100% coverage locally. The proposed five-second bound also limits normal teardown when the configured HTTP timeout is longer, so that budget needs a maintainer decision.
@shivamm-gupta, happy to contribute the regression tests to your implementation rather than open a competing PR.
Investigation, tests, and candidate patch were AI-assisted with OpenAI Codex; the before/after checks were executed locally.
AtharvaJoshi8216 commentedon Nov 20, 2026
The five-second limit is the part I'd be careful with. Shielding cleanup makes sense, but I'd probably keep the timeout configurable rather than silently overriding the client's existing HTTP timeout. Otherwise a slow but valid DELETE could still leave the session around. Did you test what happens when the server takes longer than five seconds to process the termination?