Skip to content

Python(fix): sift client lifecycle improvements#595

Merged
alexluck-sift merged 1 commit into
mainfrom
al/python/feat/sift-client-shutdown
May 29, 2026
Merged

Python(fix): sift client lifecycle improvements#595
alexluck-sift merged 1 commit into
mainfrom
al/python/feat/sift-client-shutdown

Conversation

@alexluck-sift

@alexluck-sift alexluck-sift commented May 28, 2026

Copy link
Copy Markdown
Collaborator

Changes

  • GrpcClient.close_sync() / close() now clear _channels_async and _stubs_async_map after teardown so the channel objects are released promptly instead of lingering past interpreter exit.
  • close_sync() is now idempotent (guards against double-close), skips work when the loop is already stopped, and uses a .result(timeout=...) so an unresponsive channel can't block forever at exit.
  • Added TestGrpcClientClose covering the ref clearing and the idempotent second-call paths.

Why

The pytest plugin (and any long-lived sync caller) was leaving grpc_wait_for_shutdown_with_timeout() timed out at process exit, plus a silent ~2s stall while gRPC waited.

The gRPC C-core releases a channel's resources only when the Python channel object is destroyed, not when close() returns. Under pytest the session-scoped client kept channels alive in its maps until interpreter finalization, so the C-core's own exit-time shutdown raced live channels and timed out. Clearing the maps inside close() drops the refs, refcounting destroys the channels, and the C-core's later C-level atexit then finds nothing to wait on.

The idempotency guard fixes a separate latent hang: closing explicitly (or via the context manager) and then having atexit close again submitted a coroutine to the already-stopped loop, where .result() blocked forever.

@alexluck-sift alexluck-sift changed the base branch from main to al/python/feat/pytest-dev-branch May 28, 2026 02:14
@alexluck-sift alexluck-sift force-pushed the al/python/feat/sift-client-shutdown branch from 144ed4f to 702d577 Compare May 28, 2026 17:01
@alexluck-sift alexluck-sift changed the base branch from al/python/feat/pytest-dev-branch to main May 28, 2026 17:02
@alexluck-sift alexluck-sift marked this pull request as ready for review May 28, 2026 17:05
solidiquis
solidiquis previously approved these changes May 29, 2026
@alexluck-sift alexluck-sift enabled auto-merge (squash) May 29, 2026 23:34
@github-actions

Copy link
Copy Markdown
Contributor

Python docs preview: https://sift-stack.github.io/sift/python/pr-595/

Deployed from d945180. The link may take up to a minute to become live as GitHub Pages propagates.

@alexluck-sift alexluck-sift merged commit 960fe17 into main May 29, 2026
23 checks passed
@alexluck-sift alexluck-sift deleted the al/python/feat/sift-client-shutdown branch May 29, 2026 23:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants