Repository navigation
Description
Summary
EventService.close() cancels _run_task then waits for it with asyncio.wait_for(..., timeout=10.0). The task's _run_and_publish finally block runs await loop.run_in_executor(None, self._callback_wrapper.wait_for_pending, 30.0) — a drain that can take up to 30 seconds. When the drain takes longer than 10 seconds, wait_for fires a second task.cancel() that injects CancelledError into the await, aborting the rest of the finally block. _publish_state_update() is never called and buffered events are dropped.
Affected code
event_service.py lines 1208–1221 (_run_and_publish finally block):
finally:
if self._callback_wrapper:
await loop.run_in_executor(
None, self._callback_wrapper.wait_for_pending, 30.0 # up to 30 s
)
self._run_task = None
await self._publish_state_update() # ← never reached when drain > 10 sevent_service.py lines 1680–1687 (close()):
self._run_task.cancel()
try:
await asyncio.wait_for(self._run_task, timeout=10.0) # 10 s < 30 s ceiling
except asyncio.CancelledError:
pass
except Exception as exc:
logger.warning("Run task did not exit cleanly during close: %s", exc)
self._run_task = NoneVerified with a minimal reproducer (Python 3.14)
async def _run_and_publish():
try:
await asyncio.sleep(10) # simulates conversation.arun()
except Exception:
pass
finally:
print("finally: starting drain")
await loop.run_in_executor(None, slow_cleanup) # takes 3 s
print("finally: publish_state_update") # ← NOT PRINTED
task = loop.create_task(_run_and_publish())
task.cancel()
await asyncio.wait_for(task, timeout=1.0) # timeout < drain → second cancelOutput:
finally: starting drain
TIMEOUT — task still running!
[3 s later] cleanup thread done
task.done(): True
publish_state_update line is never printed. The executor thread completes independently, but the rest of the finally block is gone.
Impact
Trigger: any slow or stuck WebSocket subscriber causes wait_for_pending to block longer than 10 seconds while draining the callback queue.
- WebSocket clients receive no final conversation-state update (FINISHED / ERROR / PAUSED) and see the conversation as perpetually running until their connection drops.
- In-flight
AsyncCallbackWrapperevents (agent messages buffered mid-run) are silently dropped.
Relationship to open issues
- agent-server: conversation wedges at execution_status=idle while /run returns 409 (stale _run_task never reaped, no self-heal) #3842 —
_run_taskwedges indefinitely and never completes. Different root cause; here the task does complete, but only afterclose()has moved on. - agent-server: message arriving during a finished run's wait_for_pending tail is never processed (stranded until next send) #3363 / fix(agent-server): re-run input stranded in a finished run's cleanup tail #3364 — message stranded during the
wait_for_pendingtail. Different symptom (message delivery), but the same 10 s vs 30 s mismatch is the structural cause.
Proposed fix
Option A (simplest) — raise the close() timeout to match or exceed wait_for_pending's ceiling:
await asyncio.wait_for(self._run_task, timeout=35.0)Option B (robust) — shield the drain so a second cancellation cannot abort it:
finally:
if self._callback_wrapper:
try:
await asyncio.shield(loop.run_in_executor(
None, self._callback_wrapper.wait_for_pending, 30.0
))
except asyncio.CancelledError:
pass # drain interrupted; still proceed to publish state
self._run_task = None
try:
await self._publish_state_update()
except Exception:
logger.warning("Final state update failed during close", exc_info=True)
Activity
all-hands-bot commentedon Sep 15, 2026
[Automatic Post]: This issue was automatically closed due to 40 days of inactivity. We do this to help keep the issue tracker manageable and focused on active issues. If this is still relevant, feel free to reopen it.
Generated by OpenHands automation on behalf of the user.