Repository navigation
fix(tui_gateway): drain in-flight turns before finalizing sessions on compute-host shutdown #77053
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
briandevans
wants to merge
3
commits into
NousResearch:main
from
briandevans:fix/tui-gateway-shutdown-drain-before-finalize
Closed
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
04ad269
fix(tui_gateway): drain in-flight turns before finalizing sessions on…
briandevans 80b663c
fix(tui_gateway): bound the shutdown drain sleep by the time left to it
briandevans 0361958
fix(tui_gateway): retain live-turn sessions unfinalized when the drai…
briandevans File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the drain deadline expires with a future still running, this invokes the one-shot
_finalize_sessionwhile that turn remains live;shutdown(wait=False)below does not join it. That retains the same persistence/lifecycle race for long turns. Please establish a terminal turn state before finalizing this session, or retain it unfinalized for recovery.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, and thanks — the reserve only relocated the race past the deadline rather than removing it. Fixed in
036195851, taking your second option (retain unfinalized for recovery).The blocker was that
_turn_futureswas a bareset[Future], so on deadline expiry the drain could tell that something was still running but not whose — leavingflush_all_sessionsno choice but to finalize everything. It's now adict[Future, str]mapping each in-flight turn to itssid, captured at both submit sites (_handle_turn_start,_handle_spike_turn_start) from thesidalready in scope. That's the same key space asserver._sessions, which_ensure_server_sessionlooks up bysiddirectly, so no new identity plumbing was needed.On expiry,
shutdownnow computes the sids whose futures are still running and passes them toflush_all_sessions(skip_sids=...). Those sessions are left unfinalized — one-shot latch unspent,active_session_leaseunreleased — so they stay recoverable. Sessions with no live turn finalize in the same pass exactly as before.Unchanged:
waitsemantics, themin(_FLUSH_RESERVE_SECS, budget / 2.0)reserve, and themin(0.05, remaining)sleep bound. No added shutdown latency. The done-callback now pops under_turn_futures_lockrather than relying onset.discardas a bare callback, so entries still can't accumulate.Tests in
tests/tui_gateway/test_compute_host_phase1.py:test_shutdown_retains_a_live_turns_session_when_the_drain_deadline_expires— replacestest_shutdown_still_finalizes_when_the_drain_deadline_expires, which asserted the behaviour you flagged. Two sessions, one with a turn outliving the window: the live one is retained, the idle one still flushes, and the call still returns insidewait.test_shutdown_retains_live_sessions_within_the_stdin_closed_budget— new, pinned to thewait=2.0stdin_closedpath, the tightest budget any caller uses and where the reserve has least room. Also asserts the drain still consumes its full window.test_shutdown_drains_in_flight_turn_before_finalizing_sessions— extended to guard the other direction: a turn that did drain must still finalize, so the skip can't over-reach into completed work. It also now asserts_turn_futuresempties, covering the container change.Both new assertions were verified to fail against the previous flush behaviour and pass with the fix.
flush_all_sessionshas exactly one call site repo-wide, and all threeshutdowncallers (orphan,sigterm,stdin_closed) funnel through it, so this covers every path of the concern.