Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 123 additions & 38 deletions orchestrator/routes/pipelines.py
Original file line number Diff line number Diff line change
Expand Up @@ -7567,8 +7567,6 @@ def _run_concurrent_phase(
exception propagates. Distinguishes spawn failures from container
exits so the outer caller's ``pipeline.error`` is accurate.
"""
from concurrent.futures import ThreadPoolExecutor, as_completed

from models import (
AgentExecution as StateAgentExecution,
)
Expand Down Expand Up @@ -7873,10 +7871,9 @@ def _run_concurrent_phase(
docker_client = spawner.backend
all_logs: list[str] = []
has_failures = [False] # Mutable container for closure access
# Lock protects all_logs and has_failures mutations from the
# ThreadPoolExecutor threads in the timeout fallback path (step 6).
# The main polling loop is single-threaded, but the lock is cheap
# and makes the code safe regardless of GIL guarantees.
# Lock kept for forward-compat; the polling loop is single-threaded
# after the #1921 refactor but _record_container_exit uses the lock
# and is called from multiple code paths.
_logs_lock = threading.Lock()

poll_interval = 5 # seconds
Expand Down Expand Up @@ -8413,56 +8410,130 @@ def _update_agents_complete() -> None:
consensus.get("blocking_agents", []),
)

# Fall back: wait for remaining containers with ThreadPoolExecutor
# Fall back: event-driven wait for remaining containers.
#
# Issue #1921: the previous implementation used a
# ThreadPoolExecutor with a blocking
# wait_for_container(timeout=3600) per container. During
# that hour the polling loop was blind to BRC progress —
# a NACK → re-propose → ACK cycle completing in the final
# minute could still be force-killed. Now we poll
# container status in short steps and re-check consensus
# between steps, early-returning on completion before
# force-killing anything.
remaining = [e for e in active_executions if e.container_id not in exited_containers]
if remaining:
with ThreadPoolExecutor(max_workers=len(remaining)) as pool:
post_timeout_budget = 3600 # seconds total
post_timeout_poll_interval = 30 # seconds between checks
post_timeout_start = time.monotonic()

def _wait_remaining(exec_info):
try:
final_info = docker_client.wait_for_container(
exec_info.container_id,
timeout=3600,
while remaining:
elapsed_post_timeout = time.monotonic() - post_timeout_start
if elapsed_post_timeout >= post_timeout_budget:
break

# A. Re-check consensus; if agents converged during
# the wait, stop containers and return success
# before force-killing them.
try:
_wait_consensus = executor.check_consensus()
except Exception as _wait_consensus_err:
logger.warning(
"Consensus recheck during post-timeout wait failed",
pipeline_id=pipeline_id,
error=str(_wait_consensus_err),
)
_wait_consensus = None

if (
_wait_consensus
and _wait_consensus.get("is_complete")
and not _wait_consensus.get("has_unresolved_nacks")
):
combined_logs = "\n".join(all_logs)
_total_elapsed = time.monotonic() - start_time
if _emit_event is not None:
_emit_event(
EventType.CONSENSUS_REACHED,
pipeline_id,
data={"elapsed_seconds": _total_elapsed},
)
logger.info(
"Consensus reached during post-timeout wait",
pipeline_id=pipeline_id,
elapsed_post_timeout_seconds=round(elapsed_post_timeout, 1),
total_elapsed_seconds=round(_total_elapsed, 1),
)
_update_agents_complete()
_stop_running_containers()
return 0, combined_logs

# B. Non-blocking container status check; record
# any that have exited naturally.
still_running = []
for exec_info in remaining:
try:
info = docker_client.get_container_info(exec_info.container_id)
except (
ContainerNotFoundError,
ContainerOperationError,
PodNotFoundError,
JobOperationError,
):
# Timeout or lost — stop the container so it doesn't
# keep running as an orphan (issue #1691).
try:
docker_client.stop_container(exec_info.container_id, timeout=30)
except Exception:
pass
final_info = ContainerInfo(
) as _wait_status_err:
logger.warning(
"Container lost during post-timeout wait",
container_id=exec_info.container_id,
role=exec_info.role.value,
error=str(_wait_status_err),
)
info = ContainerInfo(
container_id=exec_info.container_id,
container_name=f"{pipeline_id}-{exec_info.role.value}",
status=ContainerStatus.FAILED,
exit_code=-1,
exited_at=datetime.now(UTC),
)
_record_container_exit(exec_info, final_info)

futures = {pool.submit(_wait_remaining, e): e for e in remaining}
for future in as_completed(futures):
exc = future.exception()
if exc:
logger.error(
"Error waiting for container after timeout",
role=futures[future].role.value,
error=str(exc),
)
with _logs_lock:
has_failures[0] = True

if info.status in (
ContainerStatus.EXITED,
ContainerStatus.FAILED,
ContainerStatus.REMOVED,
):
exited_containers[exec_info.container_id] = info
_record_container_exit(exec_info, info)
else:
still_running.append(exec_info)

remaining = still_running
if not remaining:
break

time.sleep(post_timeout_poll_interval)

# Budget exhausted with containers still running —
# force-kill so they don't orphan (issue #1691).
for exec_info in remaining:
try:
docker_client.stop_container(exec_info.container_id, timeout=30)
except Exception:
pass
final_info = ContainerInfo(
container_id=exec_info.container_id,
container_name=f"{pipeline_id}-{exec_info.role.value}",
status=ContainerStatus.FAILED,
exit_code=-1,
exited_at=datetime.now(UTC),
)
exited_containers[exec_info.container_id] = final_info
_record_container_exit(exec_info, final_info)

combined_logs = "\n".join(all_logs)
if has_failures[0]:
# Consensus recheck: consensus may have completed during the
# ThreadPoolExecutor wait window (issue #1691). Without this
# recheck, containers that were force-killed after consensus
# was already confirmed would cause the phase to fail.
# Consensus recheck: consensus may have completed right as the
# post-timeout budget elapsed and containers were force-killed
# (issue #1691). The in-loop consensus check covers the common
# case; this recheck catches the narrow race where consensus
# completed between the last in-loop check and force-kill.
try:
_timeout_consensus = executor.check_consensus()
except Exception as e:
Expand Down Expand Up @@ -8543,6 +8614,20 @@ def _wait_remaining(exec_info):
_stop_running_containers()
return 0, combined_logs

# Consensus not complete on recheck. Mirror the non-failure
# branch's NACK summary so operators see which reviewer edges
# are still blocking, even when containers had non-zero exits.
if _timeout_consensus.get("has_unresolved_nacks"):
nack_details = _timeout_consensus.get("unresolved_nacks", [])
nack_summary = _format_nack_summary(nack_details)
logger.warning(
"Timeout with unresolved NACKs (has_failures path)",
pipeline_id=pipeline_id,
nack_count=len(nack_details),
)
combined_logs += (
f"\n--- UNRESOLVED NACKs ({len(nack_details)}) ---\n{nack_summary}"
)
return 1, combined_logs

# After timeout, check the BRC approval matrix for unresolved
Expand Down
43 changes: 21 additions & 22 deletions orchestrator/tests/test_consensus_polling.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,29 +271,26 @@ def test_timeout_creates_hitl_decision(
self, MockExecutor, mock_prompt, mock_lock, mock_emit, mock_monotonic, mock_sleep
):
"""When consensus times out, a HITL decision is created."""
# Use a callable side_effect instead of a finite list to avoid
# StopIteration if leaked threads from other tests call time.monotonic()
# while the module-level mock is active.
# Use a callable side_effect that (a) starts before the timeout,
# (b) jumps past the 30-min consensus timeout, and (c) keeps
# advancing so the post-timeout polling budget (#1921) also
# exhausts within a bounded number of iterations.
_calls = [0]

def _monotonic():
_calls[0] += 1
return 0.0 if _calls[0] == 1 else 1801.0
if _calls[0] == 1:
return 0.0
# Each subsequent call jumps 2000s so both the 1800s
# consensus timeout and the 3600s post-timeout budget
# elapse quickly.
return float(1801.0 + _calls[0] * 2000.0)

mock_monotonic.side_effect = _monotonic

executions = [_make_execution(AgentRole.CODER, "coder-1")]
pipeline, mock_store, mock_spawner, mock_docker = _base_mocks(executions)

# Container exits cleanly during the fallback wait
mock_docker.wait_for_container.return_value = ContainerInfo(
container_id="coder-1",
container_name="issue-999-coder",
status=ContainerStatus.EXITED,
exit_code=0,
exited_at=datetime.now(UTC),
)

mock_executor_instance = MagicMock()
mock_executor_instance.spawn_all.return_value = executions
mock_executor_instance.check_consensus.return_value = {
Expand All @@ -317,7 +314,12 @@ def _monotonic():
**_CALL_ARGS,
)

assert exit_code == 0
# Timeout with no convergence → force-kill path → exit 1 (#1921).
# Prior to #1921 this returned 0 because wait_for_container was
# mocked to return a clean exit; post-#1921 the polling loop
# force-kills still-running containers when the 3600s budget
# elapses, which is the realistic outcome.
assert exit_code == 1
# HITL decision should have been created on the pipeline
mock_add_decision.assert_called_once()
call_args = mock_add_decision.call_args
Expand All @@ -336,23 +338,20 @@ def test_timeout_emits_event(
"""CONSENSUS_TIMEOUT event is emitted on timeout."""
from events import EventType

# See test_timeout_creates_hitl_decision for why monotonic must
# keep advancing past the 3600s post-timeout budget (#1921).
_calls = [0]

def _monotonic():
_calls[0] += 1
return 0.0 if _calls[0] == 1 else 1801.0
if _calls[0] == 1:
return 0.0
return float(1801.0 + _calls[0] * 2000.0)

mock_monotonic.side_effect = _monotonic

executions = [_make_execution(AgentRole.CODER, "coder-1")]
pipeline, mock_store, mock_spawner, mock_docker = _base_mocks(executions)
mock_docker.wait_for_container.return_value = ContainerInfo(
container_id="coder-1",
container_name="issue-999-coder",
status=ContainerStatus.EXITED,
exit_code=0,
exited_at=datetime.now(UTC),
)

mock_executor_instance = MagicMock()
mock_executor_instance.spawn_all.return_value = executions
Expand Down
Loading
Loading