The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(tools): persist the kill outcome when the reader thread finalises first
Konstantin Khlopkov0724a6a0fd17
diff --git a/tests/tools/test_process_registry.py b/tests/tools/test_process_registry.pyindex 444c5e9a48a..6703c313932 100644--- a/tests/tools/test_process_registry.py+++ b/tests/tools/test_process_registry.py@@ -1446,6 +1446,58 @@ class TestKillProcess:finally:registry._running.pop(s.id, None)+ def test_kill_receipt_rewritten_when_reader_finalises_first(self, registry):+ """A kill racing the reader thread must not persist as a plain exit.++ The signal path blocks for the SIGKILL grace window, during which the+ reader thread can observe the exit and finalise the session first. The+ durable receipt from that first save says ``exited``; the kill result+ returned to the caller says ``killed``. The second save must rewrite+ the receipt so the persisted record matches what the caller was told.+ """+ s = _make_session(sid="proc_kill_race", command="sleep 999")+ s.pid = 424243+ s.detached = True+ registry._running[s.id] = s++ def reader_wins_during_signal(pid, start=None):+ # The reader thread observes the SIGTERMed exit while the signal+ # path is still inside its grace window and finalises first.+ registry._finish_exited(s, 0)++ saved = []++ def record_save(session):+ saved.append(+ (session.completion_reason, session.termination_source, session.exit_code)+ )++ try:+ host_guard = patch.object(+ ProcessRegistry, "_host_pid_is_ours", return_value=True+ )+ term_patch = patch.object(+ ProcessRegistry,+ "_terminate_host_pid",+ side_effect=reader_wins_during_signal,+ )+ saver = patch(+ "tools.process_registry.save_completed_result", side_effect=record_save+ )+ with host_guard, term_patch, saver:+ result = registry.kill_process(s.id)++ assert result["status"] == "killed"+ assert result["completion_reason"] == "killed"+ assert result["termination_source"] == "process.kill"+ # First save: the reader won the race and persisted a plain exit.+ assert saved[0] == ("exited", "", 0)+ # Second save: the receipt rewritten with the kill outcome.+ assert saved[-1] == ("killed", "process.kill", -15)+ finally:+ registry._running.pop(s.id, None)+ registry._finished.pop(s.id, None)+# =========================================================================# Tool handlerdiff --git a/tools/process_registry.py b/tools/process_registry.pyindex ff8122a3ade..2b0fd982092 100644--- a/tools/process_registry.py+++ b/tools/process_registry.py@@ -1355,10 +1355,11 @@ class ProcessRegistry(ProcessCheckpointMixin):session.mark_exited(exit_code)self._move_to_finished(session)- def _move_to_finished(self, session: ProcessSession):+ def _move_to_finished(self, session: ProcessSession) -> bool:"""Move a session from running to finished.Idempotent: kill_process() and the reader thread can both call this; only- the FIRST move enqueues the completion notification, so no duplicates."""+ the FIRST move enqueues the completion notification, so no duplicates.+ Returns True when this call is the one that persisted the session."""with self._lock:was_running = session.id in self._runningif was_running:@@ -1396,6 +1397,7 @@ class ProcessRegistry(ProcessCheckpointMixin):_redact_process_result(notification)self.completion_queue.put(notification)session._completion_event.set()+ return was_running@staticmethoddef _exit_fields(session: ProcessSession) -> dict:@@ -1887,7 +1889,12 @@ class ProcessRegistry(ProcessCheckpointMixin):session.exit_code = -15 # SIGTERMsession.completion_reason = "killed"session.termination_source = source- self._move_to_finished(session)+ # The reader thread can finalise the session while the signal path+ # blocks in the SIGKILL grace window: its ``save_completed_result``+ # then persists this kill as a plain ``exited``. Re-write the receipt+ # so the durable record matches what the caller was told.+ if not self._move_to_finished(session):+ save_completed_result(session)self._write_checkpoint()return {"status": "killed", "session_id": session.id, "completion_reason": session.completion_reason,