fix(hindsight): prevent late retain sync during interpreter shutdown#15507
Closed
Sanjays2402 wants to merge 1 commit into
Closed
fix(hindsight): prevent late retain sync during interpreter shutdown#15507Sanjays2402 wants to merge 1 commit into
Sanjays2402 wants to merge 1 commit into
Conversation
This was referenced Apr 25, 2026
Contributor
Author
|
Closing — superseded by main. The hindsight provider was refactored to use a queue-backed writer thread with a sentinel-based shutdown ( Verified by attempting a rebase against current main: every conflict zone shows main already implements equivalent or stronger guards ( Closing rather than dropping the rebase commit since nothing remains for me to land. Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The Hindsight memory provider can submit async retain work after shutdown begins, causing
RuntimeError: cannot schedule new futures after interpreter shutdownduring Python teardown.Root Cause
shutdown()joins existing background threads and closes the client, but there's no lifecycle gate preventingsync_turn()from submitting newasyncio.run_coroutine_threadsafe()calls after shutdown starts. A latesync_turn()call races against the interpreter's cleanup of the event loop machinery.Fix
1. Lifecycle state gating
_shutting_down,_closedflags and_state_locksync_turn()returns early once_shutting_downis set_run_sync()acceptsallow_shutdown=Trueparameter — normal callers are blocked after shutdown starts, but the shutdown path itself can still flush/close2. Extracted
_submit_retain()helpersync_turn()so both normal sync and shutdown flush use the same code path_last_retained_turnto avoid duplicating a full-session retain during shutdown flush3. Shutdown final flush
shutdown()checks for un-retained pending turns and flushes them via_submit_retain(allow_shutdown=True)Changes
plugins/memory/hindsight/__init__.py— lifecycle state,_submit_retain(), guardedsync_turn(), enhancedshutdown()Fixes #15497