Skip to content

fix(agent-server): suppress noisy traceback when sending events to a … - #3182

Merged
rbren merged 1 commit into
mainfrom
openhands/fix-websocket-send-after-disconnect
May 9, 2026
Merged

rbren merged 1 commit into
mainfrom
openhands/fix-websocket-send-after-disconnect

Conversation

@rbren

@rbren rbren commented May 9, 2026 •

Copy link
Copy Markdown
Member

…disconnected websocket

The pub/sub callback in the events / bash-events websocket endpoints can fire after the peer has gone away. Starlette then raises 'RuntimeError: Cannot call "send" once a close message has been sent.' which we were logging via logger.exception, producing a full traceback on every disconnect race.

Pre-check WebSocketState.DISCONNECTED in _send_event / _send_bash_event and downgrade RuntimeError / WebSocketDisconnect from send() to a debug-level log. Other unexpected exceptions still log a full traceback.

  • A human has tested these changes.

Why

Summary

Issue Number

How to Test

Video/Screenshots

Type

  • Bug fix
  • Feature
  • Refactor
  • Breaking change
  • Docs / chore

Notes


Agent Server images for this PR

• GHCR package: https://github.com/OpenHands/agent-sdk/pkgs/container/agent-server

Variants & Base Images

Variant Architectures Base Image Docs / Tags
java amd64, arm64 eclipse-temurin:17-jdk Link
python amd64, arm64 nikolaik/python-nodejs:python3.13-nodejs22-slim Link
golang amd64, arm64 golang:1.21-bookworm Link

Pull (multi-arch manifest)

# Each variant is a multi-arch manifest supporting both amd64 and arm64
docker pull ghcr.io/openhands/agent-server:95f115f-python

Run

docker run -it --rm \
  -p 8000:8000 \
  --name agent-server-95f115f-python \
  ghcr.io/openhands/agent-server:95f115f-python

All tags pushed for this build

ghcr.io/openhands/agent-server:95f115f-golang-amd64
ghcr.io/openhands/agent-server:95f115f38f5e5f69f0f707e2d5f384ee66f2bd24-golang-amd64
ghcr.io/openhands/agent-server:openhands-fix-websocket-send-after-disconnect-golang-amd64
ghcr.io/openhands/agent-server:95f115f-golang_tag_1.21-bookworm-amd64
ghcr.io/openhands/agent-server:95f115f-golang-arm64
ghcr.io/openhands/agent-server:95f115f38f5e5f69f0f707e2d5f384ee66f2bd24-golang-arm64
ghcr.io/openhands/agent-server:openhands-fix-websocket-send-after-disconnect-golang-arm64
ghcr.io/openhands/agent-server:95f115f-golang_tag_1.21-bookworm-arm64
ghcr.io/openhands/agent-server:95f115f-java-amd64
ghcr.io/openhands/agent-server:95f115f38f5e5f69f0f707e2d5f384ee66f2bd24-java-amd64
ghcr.io/openhands/agent-server:openhands-fix-websocket-send-after-disconnect-java-amd64
ghcr.io/openhands/agent-server:95f115f-eclipse-temurin_tag_17-jdk-amd64
ghcr.io/openhands/agent-server:95f115f-java-arm64
ghcr.io/openhands/agent-server:95f115f38f5e5f69f0f707e2d5f384ee66f2bd24-java-arm64
ghcr.io/openhands/agent-server:openhands-fix-websocket-send-after-disconnect-java-arm64
ghcr.io/openhands/agent-server:95f115f-eclipse-temurin_tag_17-jdk-arm64
ghcr.io/openhands/agent-server:95f115f-python-amd64
ghcr.io/openhands/agent-server:95f115f38f5e5f69f0f707e2d5f384ee66f2bd24-python-amd64
ghcr.io/openhands/agent-server:openhands-fix-websocket-send-after-disconnect-python-amd64
ghcr.io/openhands/agent-server:95f115f-nikolaik_s_python-nodejs_tag_python3.13-nodejs22-slim-amd64
ghcr.io/openhands/agent-server:95f115f-python-arm64
ghcr.io/openhands/agent-server:95f115f38f5e5f69f0f707e2d5f384ee66f2bd24-python-arm64
ghcr.io/openhands/agent-server:openhands-fix-websocket-send-after-disconnect-python-arm64
ghcr.io/openhands/agent-server:95f115f-nikolaik_s_python-nodejs_tag_python3.13-nodejs22-slim-arm64
ghcr.io/openhands/agent-server:95f115f-golang
ghcr.io/openhands/agent-server:95f115f38f5e5f69f0f707e2d5f384ee66f2bd24-golang
ghcr.io/openhands/agent-server:openhands-fix-websocket-send-after-disconnect-golang
ghcr.io/openhands/agent-server:95f115f-golang_tag_1.21-bookworm
ghcr.io/openhands/agent-server:95f115f-java
ghcr.io/openhands/agent-server:95f115f38f5e5f69f0f707e2d5f384ee66f2bd24-java
ghcr.io/openhands/agent-server:openhands-fix-websocket-send-after-disconnect-java
ghcr.io/openhands/agent-server:95f115f-eclipse-temurin_tag_17-jdk
ghcr.io/openhands/agent-server:95f115f-python
ghcr.io/openhands/agent-server:95f115f38f5e5f69f0f707e2d5f384ee66f2bd24-python
ghcr.io/openhands/agent-server:openhands-fix-websocket-send-after-disconnect-python
ghcr.io/openhands/agent-server:95f115f-nikolaik_s_python-nodejs_tag_python3.13-nodejs22-slim

About Multi-Architecture Support

  • Each variant tag (e.g., 95f115f-python) is a multi-arch manifest supporting both amd64 and arm64
  • Docker automatically pulls the correct architecture for your platform
  • Individual architecture tags (e.g., 95f115f-python-amd64) are also available if needed

…disconnected websocket

The pub/sub callback in the events / bash-events websocket endpoints can
fire after the peer has gone away. Starlette then raises
'RuntimeError: Cannot call "send" once a close message has been sent.'
which we were logging via logger.exception, producing a full traceback
on every disconnect race.

Pre-check WebSocketState.DISCONNECTED in _send_event / _send_bash_event
and downgrade RuntimeError / WebSocketDisconnect from send() to a
debug-level log. Other unexpected exceptions still log a full traceback.

Co-authored-by: openhands <openhands@all-hands.dev>
@rbren
rbren marked this pull request as ready for review May 9, 2026 20:43
@github-actions

github-actions Bot commented May 9, 2026

Copy link
Copy Markdown
Contributor

Python API breakage checks — ✅ PASSED

Result: ✅ PASSED

Action log

@github-actions

github-actions Bot commented May 9, 2026

Copy link
Copy Markdown
Contributor

REST API breakage checks (OpenAPI) — ✅ PASSED

Result: ✅ PASSED

Action log

@github-actions

github-actions Bot commented May 9, 2026

Copy link
Copy Markdown
Contributor

Coverage

Coverage Report •
FileStmtsMissCoverMissing
openhands-agent-server/openhands/agent_server
   sockets.py2073981%164, 167, 170, 247–249, 370–371, 376–378, 384–386, 390, 392–393, 397–398, 401, 404, 406–407, 410–411, 439, 441, 460, 475–484, 494
TOTAL262471150156% 

@all-hands-bot all-hands-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Clean pragmatic fix for noisy disconnect tracebacks.

Risk Assessment: 🟢 LOW
Only affects error-path logging. Defense-in-depth (state check + exception handling) properly handles disconnect races without hiding unexpected errors. Well-tested with coverage for both pre-check and exception-catch paths.

@all-hands-bot all-hands-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ QA Report: PASS

The PR successfully eliminates noisy exception tracebacks when websockets disconnect during event publishing, replacing them with clean debug-level logs.

Does this PR achieve its stated goal?

Yes. The PR achieves its goal of suppressing noisy tracebacks when sending events to disconnected websockets. I verified this by running the actual code on both the main branch (showing the bug) and the PR branch (showing the fix). The before/after comparison demonstrates that RuntimeError exceptions from websocket disconnect races are now logged at DEBUG level without stack traces, while the old code logged them via logger.exception with full tracebacks.

Phase Result
Environment Setup ✅ Built successfully with make build
CI Status ✅ All required checks passing (7/7)
Functional Verification ✅ Fix verified with before/after comparison
Functional Verification

Verification Approach

Created a test script that simulates the exact race condition scenarios described in the PR:

  1. Scenario 1: Pub/sub callback fires when websocket is already DISCONNECTED
  2. Scenario 2: RuntimeError raised during send() due to disconnect race

Test 1: Reproduce the bug on main branch

Step 1 — Establish baseline (main branch without the fix):

Checked out main branch and ran verification script:

git checkout main
OPENHANDS_SUPPRESS_BANNER=1 uv run python verify_websocket_logging.py

Output from main branch (BEFORE fix):

Scenario 1: Websocket already DISCONNECTED
Is websocket connected? True
send_json called: True

Scenario 2: RuntimeError during send (race condition)
Captured logs:
ERROR - openhands.agent_server.sockets - error_sending_event: MessageEvent(...)
Traceback (most recent call last):
  File ".../openhands/agent_server/sockets.py", line 418, in _send_event
    await websocket.send_json(dumped)
  ...
RuntimeError: Cannot call "send" once a close message has been sent.
Stack (most recent call last):
  ...
  [Full stack trace with 15+ lines]

Interpretation: This confirms the bug exists on main:

  • Scenario 1: No pre-check exists, so send_json is attempted even on disconnected sockets
  • Scenario 2: RuntimeError is logged at ERROR level via logger.exception() with a full multi-line traceback and stack info

Test 2: Verify the fix on PR branch

Step 2 — Apply the PR's changes:

Checked out the PR branch:

git checkout openhands/fix-websocket-send-after-disconnect

Step 3 — Re-run the same verification:

OPENHANDS_SUPPRESS_BANNER=1 uv run python verify_websocket_logging.py

Output from PR branch (AFTER fix):

Scenario 1: Websocket already DISCONNECTED
Is websocket connected? False
send_json called: False
Captured logs:
DEBUG - openhands.agent_server.sockets - skip_sending_event_socket_disconnected: MessageEvent(...)

Scenario 2: RuntimeError during send (race condition)
Is websocket connected? True
Captured logs:
DEBUG - openhands.agent_server.sockets - error_sending_event_disconnected: MessageEvent(...) (Cannot call "send" once a close message has been sent.)

Log analysis:
  - Has DEBUG level: True
  - Has ERROR/CRITICAL level: False
  - Has full traceback: False

Interpretation: The fix works as designed:

  • Scenario 1: _is_websocket_connected() pre-check detects DISCONNECTED state and returns early with a DEBUG log, preventing the send attempt entirely
  • Scenario 2: RuntimeError is now caught specifically and logged at DEBUG level as a single-line message without a traceback
  • No more noisy exception logs cluttering production logs

Code Changes Verified

The key changes work exactly as documented:

  1. New helper function _is_websocket_connected() checks WebSocketState.DISCONNECTED on both application_state and client_state
  2. Modified _send_event():
    • Pre-checks connection state before attempting send
    • Catches RuntimeError and WebSocketDisconnect specifically → logs at DEBUG level
    • Other exceptions still logged with full traceback via logger.exception()
  3. Modified _send_bash_event(): Same pattern applied for consistency

CI Verification

All required CI checks passing:

Check OpenAPI Schema    pass    35s
agent-server-tests      pass    49s
build-binary-and-test   pass    2m53s
cross-tests             pass    1m30s
pre-commit              pass    1m18s
sdk-tests               pass    8s
tools-tests             pass    8s

The new unit tests specifically verify:

  • test_websocket_subscriber_skips_send_when_disconnected: Confirms send_json is NOT called when state is DISCONNECTED
  • test_websocket_subscriber_send_runtime_error_not_logged_as_exception: Confirms RuntimeError is logged at debug level, NOT via logger.exception

Issues Found

None.

@rbren
rbren merged commit 7569088 into main May 9, 2026
37 checks passed
@rbren
rbren deleted the openhands/fix-websocket-send-after-disconnect branch May 9, 2026 20:52
@rbren
rbren restored the openhands/fix-websocket-send-after-disconnect branch May 9, 2026 21:13
StressTestor pushed a commit to StressTestor/software-agent-sdk that referenced this pull request Jun 1, 2026
pengzhang-create pushed a commit to Pyromind-Dynamics/software-agent-sdk that referenced this pull request Aug 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants