From 0cbedc58f0f527aecec5be8ceea112af82629962 Mon Sep 17 00:00:00 2001 From: kshitij <82637225+kshitijk4poor@users.noreply.github.com> Date: Mon, 3 Aug 2026 09:54:59 +0530 Subject: [PATCH] docs: note atexit re-finalization interaction in shutdown() docstring MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The PR's skip-live-sessions optimization is partially defeated by server._shutdown_sessions() registered via atexit (server.py:1172), which runs on SystemExit after shutdown() returns for the SIGTERM and stdin_closed paths. The orphan path (os._exit(0)) bypasses atexit. This is a pre-existing issue — the old finalize-first order had the same atexit interaction. The comment documents the gap and suggests a follow-up: gate _shutdown_sessions on not session.get('running'). --- tui_gateway/compute_host.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tui_gateway/compute_host.py b/tui_gateway/compute_host.py index 706be07b08..d90024557a 100644 --- a/tui_gateway/compute_host.py +++ b/tui_gateway/compute_host.py @@ -204,6 +204,20 @@ class ComputeHost: live work: exactly the race the drain exists to close, just moved later. Leaving them unfinalized keeps them recoverable instead. Sessions with no live turn finalize here as they always have. + + NOTE: ``server._shutdown_sessions`` is registered via ``atexit`` + (``server.py``) and runs on ``SystemExit`` after ``shutdown()`` + returns. It calls ``_finalize_session`` on any session still in + ``server._sessions`` — including ones skipped here whose turn is + still running, since ``_executor.shutdown(wait=False)`` only cancels + pending futures, not running ones. The orphan path (``os._exit(0)``) + bypasses atexit, so the skip is fully effective there. For the + SIGTERM and stdin_closed paths the atexit handler may re-finalize + skipped sessions; this is a pre-existing issue (the old finalize- + first order had the same atexit interaction) and does not make the + drain-before-finalize reordering worse. A follow-up could gate + ``_shutdown_sessions`` on ``not session.get("_finalized") and not + session.get("running")`` to close the gap. """ self._closed.set() budget = max(0.0, wait)