Repository navigation
fix(agent-server): re-run input stranded in a finished run's cleanup tail - #3364
Conversation
…tail A user message delivered over the events WebSocket while the previous run is still in its _run_and_publish wait_for_pending tail had its run() rejected as conversation_already_running and suppressed, and nothing re-ran it once the tail cleared — so the message sat unprocessed until the next send (no agent response; resend appears to work). The in-flight case is already handled by LocalConversation.run(); this closes the post-run executor-tail gap. Re-arm a run at the end of _run_and_publish when the conversation is left IDLE, which only happens when send_message reset a terminal FINISHED/STUCK status during the tail (pending input, no run scheduled). Zero overhead on the normal path, where status is FINISHED. Refs #3363
Python API breakage checks — ✅ PASSEDResult: ✅ PASSED |
REST API breakage checks (OpenAPI) — ✅ PASSEDResult: ✅ PASSED |
Coverage Report •
|
||||||||||||||||||||
Using IDLE as the proxy for a suppressed send_message(run=True) was too broad: a deliberate run=False append (a real HTTP path via event_router) or an IDLE reached another way (e.g. confirmation rejection) would wrongly trigger a run. Track request intent instead — set _rerun_requested only when a run=True is rejected as conversation_already_running, and consume it in _run_and_publish (still gated on IDLE so an already-absorbed message doesn't re-run). Add a run=False-during-tail regression test. Refs #3363
all-hands-bot
left a comment
There was a problem hiding this comment.
🟢 Taste Rating: Good taste — targeted fix, and the explicit _rerun_requested flag addresses the prior run=False concern.
I didn’t find any code issues that need to be addressed. Targeted verification passed: uv run pytest tests/agent_server/test_event_service.py -k 'message_in_run_cleanup_tail_is_not_stranded or run_false_message_in_cleanup_tail_is_not_run' (2 passed).
[RISK ASSESSMENT]
- [Overall PR]
⚠️ Risk Assessment: 🟡 MEDIUM — this touches agent-server run scheduling / conversation loop behavior. Per repo policy, I’m leaving a COMMENT rather than APPROVE and deferring merge/eval-risk sign-off to a human maintainer.
VERDICT: ✅ Worth merging from code review; needs human maintainer sign-off for the run-scheduling/eval-risk policy.
KEY INSIGHT: Preserving explicit run=True intent is the right data flow; IDLE alone is no longer used as a proxy.
This review was created by an AI agent (OpenHands) on behalf of the user.
Was this automated review useful? React with 👍 or 👎 to this review to help us measure review quality.
Workflow run: https://github.com/OpenHands/software-agent-sdk/actions/runs/26310353309
all-hands-bot
left a comment
There was a problem hiding this comment.
Code Review
The re-arm approach is well-structured. The key improvement over the previous iteration is using an explicit _rerun_requested flag rather than inferring intent from IDLE status — this directly addresses the concern raised in the prior review and correctly preserves the run=False API contract. The negative-case test (test_run_false_message_in_cleanup_tail_is_not_run) is a particularly good addition: it guards the explicit-intent contract and would have caught the earlier IDLE-proxy design.
Two minor issues are worth noting inline.
This review was generated by an AI agent (OpenHands) on behalf of the user via OpenHands Automation.
all-hands-bot
left a comment
There was a problem hiding this comment.
✅ QA Report: PASS
Verified the run-tail race fix with a real ConversationService/EventService flow: the pre-fix behavior strands the second message, while this PR processes it after cleanup-tail completion and preserves run=False append behavior.
Does this PR achieve its stated goal?
Yes. The PR set out to fix a message sent with run=True while the previous run is still in wait_for_pending() cleanup being appended but never processed. I reproduced that exact race with the base event_service.py (call_count stayed 1, status ended idle), then reran the same script on the PR branch and observed the second message trigger a follow-up agent turn (call_count=2, status finished). I also exercised the related run=False behavior and confirmed it did not auto-run (call_count stayed 1).
| Phase | Result |
|---|---|
| Environment Setup | ✅ uv sync --dev completed and the QA script ran via uv run python |
| CI Status | ⏳ At check time: 32 success, 4 skipped, 1 in progress (qa-changes) |
| Functional Verification | ✅ Before/after race reproduced and fixed; run=False side effect checked |
Functional Verification
Test 1: cleanup-tail run=True message is no longer stranded
Step 1 — Reproduce baseline without the fix:
Temporarily restored openhands-agent-server/openhands/agent_server/event_service.py from origin/main, then ran:
OPENHANDS_SUPPRESS_BANNER=1 uv run python -u /tmp/qa_tail_race.pyObserved:
after first: call_count=1, status=finished, task_alive=True
during tail after second send: run=True, call_count=1, same_task=True, status=idle
final: call_count=1, status=idle, run_task=None
RESULT=UNEXPECTED
script_exit=1
This confirms the bug: the second user message reset the conversation to idle during the first run's cleanup tail, but no second agent turn ran after the tail cleared.
Step 2 — Apply the PR's changes:
Restored the PR branch version of event_service.py and reran the same real service flow.
Step 3 — Re-run with the fix in place:
OPENHANDS_SUPPRESS_BANNER=1 uv run python -u /tmp/qa_tail_race.pyObserved:
after first: call_count=1, status=finished, task_alive=True
during tail after second send: run=True, call_count=1, same_task=True, status=idle
final: call_count=2, status=finished, run_task=None
RESULT=EXPECTED
script_exit=0
This confirms the fix: the second message remained pending during cleanup, then the PR re-armed the run once the cleanup tail finished.
Test 2: cleanup-tail run=False append does not auto-run
Ran the same setup on the PR branch, but appended the second message with run=False:
OPENHANDS_SUPPRESS_BANNER=1 uv run python -u /tmp/qa_tail_race.py run-falseObserved:
after first: call_count=1, status=finished, task_alive=True
during tail after second send: run=False, call_count=1, same_task=True, status=idle
final: call_count=1, status=idle, run_task=None
RESULT=EXPECTED
script_exit=0
This shows the re-arm is tied to explicit run intent and does not unexpectedly process a deliberate append-only message.
Issues Found
None.
This QA review was created by an AI agent (OpenHands) on behalf of the user.
…asks Fixes OpenHands#3842. Two defects compounded into permanently wedged conversations: 1. WebhookSubscriber._post_events retried on a fixed delay with no backoff, ignored Retry-After, ran overlapping volleys, and after exhausting retries only tried again when a NEW event arrived - so against a rate-limited downstream (the OpenHands app server rate-limits /api/v1/webhooks/events/* like any other route) delivery stalled forever and the queue trim dropped events permanently. 2. EventService.run() raised conversation_already_running whenever _run_task was alive, even with execution_status reporting non-RUNNING; a run task that never completes therefore blocked the conversation forever - the self-heal from OpenHands#3364 never fires because the task never reaches its cleanup tail. Webhook poster: serialise flushes; exponential backoff honouring Retry-After; exponentially growing cooldown (1s floor, 60s cap) instead of hammering; re-arm the flush timer on failure so delivery resumes without new events; prepend re-queued events to preserve delivery order; enforce the queue bound on enqueue as well as on failed-flush trim. EventService.run(): when a live _run_task coexists with non-RUNNING status, start a staleness clock; reap the same task if still lingering past STALE_RUN_TASK_REAP_SECONDS (120s, well beyond the 30s-bounded wait_for_pending tail) and start the requested run. The task finally block now only clears its own task reference so a reaped task cannot clobber its replacement. Rebased onto main and reconciled with the webhook_subscriber refactor.
Summary
Fixes #3363.
A user message delivered over the events WebSocket while the previous run is still in its post-finish cleanup tail (
EventService._run_and_publishawaitingwait_for_pending) was appended to the conversation but never triggered an agent run — the user sees no response and has to resend.send_message(run=True)suppressesrun()'sconversation_already_runningwhile_run_taskis wrapping up, and onceconversation.run()has returned nothing re-runs the message.Fix
Re-arm a run at the end of
_run_and_publishwhen the conversation is leftIDLE. That state only arises whensend_messagereset a terminalFINISHED/STUCKstatus during the tail — i.e. there is pending input with no run scheduled. Otherwise the run endsFINISHEDand the re-arm is a no-op (zero overhead on the normal path).Safe because:
_run_taskis alreadyNoneandrun()re-checks under_run_lock, so a concurrent message's ownrun()and the re-arm can't both start a run.FINISHED→ the next check is false, so it terminates (it only chains while real input keeps landing in each tail).PAUSED/WAITING_FOR_CONFIRMATION/ERRORare not reset toIDLEbysend_message, so they're never auto-re-armed.This is distinct from the in-flight case, which
LocalConversation.run()already handles by deliberately not breaking onFINISHED.Test
test_message_in_run_cleanup_tail_is_not_strandeddrives a real conversation, parks the first run in itswait_for_pendingtail, sends a second message into that window, releases the tail, and asserts the second message gets processed. It fails onmain(call_count=1, stuckIDLE) and passes with this change.Agent Server images for this PR
• GHCR package: https://github.com/OpenHands/agent-sdk/pkgs/container/agent-server
Variants & Base Images
eclipse-temurin:17-jdknikolaik/python-nodejs:python3.13-nodejs22-slimgolang:1.21-bookwormPull (multi-arch manifest)
# Each variant is a multi-arch manifest supporting both amd64 and arm64 docker pull ghcr.io/openhands/agent-server:c51e488-pythonRun
All tags pushed for this build
About Multi-Architecture Support
c51e488-python) is a multi-arch manifest supporting both amd64 and arm64c51e488-python-amd64) are also available if needed