Skip to content

agent-server: close() 10 s timeout < wait_for_pending 30 s ceiling — second cancel aborts finally-block, _publish_state_update never fires #4387

Closed as not planned

Description

@harsh4vardhan

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 s

event_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 = None

Verified 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 cancel

Output:

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 AsyncCallbackWrapper events (agent messages buffered mid-run) are silently dropped.

Relationship to open issues

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

  1. added
    bug
    priority:medium
    on Aug 6, 2026
  2. all-hands-bot commented on Sep 15, 2026

    @all-hands-bot
    Collaborator

    [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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    agent-executionbugpriority:mediumsdksession

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions