fix(proxy): shield stateful proxy disconnect during session teardown (#4363)

This commit is contained in:
Jeremiah Lowin 2026-06-24 10:56:40 -04:00 committed by GitHub
commit ddcdf64813
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1232,7 +1232,13 @@ class StatefulProxyClient(ProxyClient[ClientTransportT]):
async def _on_session_exit():
self._caches.pop(session)
logger.debug(f"{proxy_client} will be disconnect")
await proxy_client._disconnect(force=True)
# This callback runs while the server session's exit stack is
# unwinding, which usually happens because the owning task is
# being cancelled. Shield the disconnect so the forced cleanup
# actually runs to completion instead of aborting at the first
# cancellation checkpoint (e.g. acquiring the session lock).
with anyio.CancelScope(shield=True):
await proxy_client._disconnect(force=True)
session._exit_stack.push_async_callback(_on_session_exit)