Skip to content

The agent that grows with you

fix(compression): don't mistake a worker's TimeoutError for a poll timeout

Trevor Nash-Keller8305113328b3
diff --git a/agent/conversation_compression.py b/agent/conversation_compression.py
index 36275e766f9..73c65e899ee 100644
--- a/agent/conversation_compression.py
+++ b/agent/conversation_compression.py
@@ -1059,6 +1059,15 @@ def _await_worker_within_budget(
try:
return True, future.result(timeout=wait_slice)
except concurrent.futures.TimeoutError:
+ # concurrent.futures.TimeoutError IS builtin TimeoutError (3.11+), so this also fires when
+ # the WORKER died raising a timeout-class error (the aux client raises bare TimeoutError on a
+ # stalled summary stream). A settled future never becomes unsettled: without this check the
+ # loop re-waits on a dead future, each result() returning instantly, spinning the CPU at
+ # ~2k iterations/sec until the full idle budget elapses and reporting the dead worker as
+ # "still streaming". Take the stall path immediately instead. A non-timeout worker exception
+ # still propagates out of result() unchanged.
+ if future.done():
+ return False, None
waited = time.monotonic() - wait_started
since_progress = fence.seconds_since_progress()
if not fence.deadline_exceeded and since_progress < idle and waited < ceiling:
@@ -1100,6 +1109,12 @@ def _await_in_flight_commit(
try:
return future.result(timeout=remaining)
except concurrent.futures.TimeoutError:
+ # A settled future never becomes unsettled, so a timeout-class exception raised BY the
+ # commit worker (concurrent.futures.TimeoutError aliases builtin TimeoutError on 3.11+)
+ # would otherwise loop forever at full CPU, never surfacing the failure. Re-raise the
+ # worker's own exception once the future is done; a live commit still loops as before.
+ if future.done():
+ return future.result()
# Commit-phase progress is informative only — the commit must complete; loop
# and re-report with the updated overrun window.
continue
diff --git a/agent/tool_executor.py b/agent/tool_executor.py
index 65cc84e2685..880c8b88c6a 100644
--- a/agent/tool_executor.py
+++ b/agent/tool_executor.py
@@ -846,6 +846,12 @@ def _poll_sequential_future(agent, future, function_name: str, deadline: float |
try:
return "done", future.result(timeout=wait_slice)
except concurrent.futures.TimeoutError:
+ # concurrent.futures.TimeoutError aliases builtin TimeoutError (3.11+), so this also fires
+ # when the TOOL WORKER itself raised a timeout-class error. A settled future never becomes
+ # unsettled: without this the loop re-waits on a dead future forever (deadline=None) or until
+ # the deadline, burning a core. Let the worker's own exception propagate to the caller.
+ if future.done():
+ return "done", future.result()
if agent._interrupt_requested:
return "interrupted", None
elapsed = int(time.monotonic() - started)
diff --git a/tests/agent/test_future_timeout_worker_death.py b/tests/agent/test_future_timeout_worker_death.py
new file mode 100644
index 00000000000..41c7c815273
--- /dev/null
+++ b/tests/agent/test_future_timeout_worker_death.py
@@ -0,0 +1,164 @@
+"""A worker that DIES raising a timeout-class error must not be mistaken for a live one.
+
+On Python 3.11+ ``concurrent.futures.TimeoutError`` IS the builtin ``TimeoutError``
+(and ``asyncio.TimeoutError`` and ``socket.timeout`` alias it too). Every poll loop
+shaped like::
+
+ try:
+ return future.result(timeout=slice)
+ except concurrent.futures.TimeoutError:
+ ...keep waiting...
+
+therefore cannot tell "the wait slice expired" (worker alive) from "the worker
+raised TimeoutError" (worker dead). The aux client raises bare ``TimeoutError``
+when a summary stream stalls, so this is reachable in production: the host re-waits
+on a settled future, each ``result()`` returns instantly, and the loop spins at
+thousands of iterations/sec — logging "still streaming" about a corpse — until the
+whole idle budget burns. That wedged a real session for 535s and flooded the agent
+log with ~90k duplicate lines.
+
+The guard is ``if future.done()``: a settled future never becomes unsettled.
+"""
+
+import concurrent.futures
+import time
+
+import pytest
+
+
+def test_timeout_error_aliases_are_indistinguishable():
+ """The premise of the bug — if this ever stops holding, the guards can be revisited."""
+ import asyncio
+ import socket
+
+ assert concurrent.futures.TimeoutError is TimeoutError
+ assert asyncio.TimeoutError is TimeoutError
+ assert socket.timeout is TimeoutError
+
+
+ "exc",
+ [TimeoutError("aux stream stalled"), __import__("asyncio").TimeoutError("aux stream stalled")],
+ ids=["builtin-TimeoutError", "asyncio-TimeoutError"],
+)
+def test_compression_wait_returns_promptly_when_worker_dies(exc):
+ """_await_worker_within_budget must take the stall path at once, not burn the idle budget."""
+ from agent.conversation_compression import (
+ CompressionCommitFence,
+ _await_worker_within_budget,
+ )
+
+ def worker():
+ raise exc
+
+ with concurrent.futures.ThreadPoolExecutor(max_workers=1) as pool:
+ future = pool.submit(worker)
+ concurrent.futures.wait([future], timeout=5)
+ fence = CompressionCommitFence()
+ fence.touch_progress()
+ started = time.monotonic()
+ settled, result = _await_worker_within_budget(
+ future, fence, idle=30.0, ceiling=60.0, wait_started=started
+ )
+ elapsed = time.monotonic() - started
+
+ # Pre-fix this consumed the full 30s idle window in a hot spin.
+ assert elapsed < 5.0, f"host waited {elapsed:.1f}s on an already-dead worker"
+ assert settled is False
+ assert result is None
+
+
+def test_compression_wait_still_polls_a_live_worker():
+ """The guard must not collapse the normal case: an unfinished future keeps waiting."""
+ from agent.conversation_compression import (
+ CompressionCommitFence,
+ _await_worker_within_budget,
+ )
+
+ release = __import__("threading").Event()
+
+ def worker():
+ release.wait(timeout=10)
+ return "compressed"
+
+ with concurrent.futures.ThreadPoolExecutor(max_workers=1) as pool:
+ future = pool.submit(worker)
+ fence = CompressionCommitFence()
+
+ def keep_alive():
+ for _ in range(40):
+ fence.touch_progress()
+ time.sleep(0.01)
+ release.set()
+
+ __import__("threading").Thread(target=keep_alive, daemon=True).start()
+ settled, result = _await_worker_within_budget(
+ future, fence, idle=5.0, ceiling=10.0, wait_started=time.monotonic()
+ )
+
+ assert settled is True
+ assert result == "compressed"
+
+
+def test_compression_wait_propagates_non_timeout_worker_errors():
+ """A non-timeout worker exception must still surface, exactly as before the fix."""
+ from agent.conversation_compression import (
+ CompressionCommitFence,
+ _await_worker_within_budget,
+ )
+
+ with concurrent.futures.ThreadPoolExecutor(max_workers=1) as pool:
+ future = pool.submit(lambda: (_ for _ in ()).throw(ValueError("boom")))
+ concurrent.futures.wait([future], timeout=5)
+ fence = CompressionCommitFence()
+ fence.touch_progress()
+ with pytest.raises(ValueError, match="boom"):
+ _await_worker_within_budget(
+ future, fence, idle=5.0, ceiling=10.0, wait_started=time.monotonic()
+ )
+
+
+def test_in_flight_commit_surfaces_worker_timeout_instead_of_looping():
+ """_await_in_flight_commit had no ceiling on this path — a dead worker spun forever."""
+ from agent.conversation_compression import _await_in_flight_commit
+
+ def worker():
+ raise TimeoutError("commit stream stalled")
+
+ with concurrent.futures.ThreadPoolExecutor(max_workers=1) as pool:
+ future = pool.submit(worker)
+ concurrent.futures.wait([future], timeout=5)
+ started = time.monotonic()
+ with pytest.raises(TimeoutError):
+ _await_in_flight_commit(
+ future, ceiling=30.0, wait_started=started, on_commit_overrun=None
+ )
+ elapsed = time.monotonic() - started
+
+ assert elapsed < 5.0, f"commit wait hung {elapsed:.1f}s on a dead worker"
+
+
+def test_sequential_tool_poll_surfaces_worker_timeout():
+ """_poll_sequential_future with deadline=None would otherwise spin indefinitely."""
+ from unittest.mock import MagicMock
+
+ from agent.tool_executor import _poll_sequential_future
+
+ agent = MagicMock()
+ agent._interrupt_requested = False
+
+ gate = MagicMock()
+ gate.excluded_seconds = MagicMock(return_value=0.0)
+
+ def worker():
+ raise TimeoutError("tool call timed out")
+
+ with concurrent.futures.ThreadPoolExecutor(max_workers=1) as pool:
+ future = pool.submit(worker)
+ concurrent.futures.wait([future], timeout=5)
+ started = time.monotonic()
+ with pytest.raises(TimeoutError):
+ _poll_sequential_future(agent, future, "some_tool", None, started, gate)
+ elapsed = time.monotonic() - started
+
+ assert elapsed < 5.0, f"tool poll hung {elapsed:.1f}s on a dead worker"