Skip to content

sdk/python: teardown is not exception-safe — an error mid-close can skip resource cleanup permanently #909

Description

@AbirAbbas

Deferred follow-up from the #907 review (inline thread on http_connection_manager.py).

cancel_and_await_if_same_loop now (correctly) propagates unrelated task-cleanup exceptions. But its callers treat the whole teardown sequence as if it can't raise, so a propagated error aborts teardown midway:

  • ConnectionManager._close_on_owner_loop: an escape between self._closed = True and the session/connector close leaves the session open — and because _claim_close_request sees _closed, a retry close() is a no-op. The session leaks permanently and unrecoverably (probed during review).
  • async_execution_manager.py (~:359): same shape — an abort skips the remaining task cancels, all state nulling, and active-execution cancellation.
  • result_cache.py (~:237): same shape.

Not reachable today: every SDK background loop breaks on CancelledError, so cleanup can't currently raise — this is latent hardening, which is why it was split out of #907 rather than folded in.

Fix shape: wrap the cancel/await steps in try/finally so resource close + state nulling always run, and re-raise after. One test per site forcing a cleanup exception and asserting the session/connector still gets closed and state is nulled.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions