Skip to content

Fix websocket infinite loop on user disconnect - #439

Merged
xingyaoww merged 4 commits into
mainfrom
fix-websocket-disconnect-loop
Sep 23, 2025
Merged

xingyaoww merged 4 commits into
mainfrom
fix-websocket-disconnect-loop

Conversation

@tofarr

@tofarr tofarr commented Sep 23, 2025

Copy link
Copy Markdown
Collaborator

Problem

The websocket code in event_router.py had several issues that caused infinite loops when users disconnected:

  1. Missing break statement: The WebSocketDisconnect exception handler was missing a break statement, causing the loop to continue indefinitely after a disconnect
  2. Double unsubscription: The code could potentially unsubscribe from events twice - once in the exception handler and once in the finally block
  3. Inadequate exception handling: The code wasn't properly distinguishing between recoverable errors and critical errors that should terminate the connection

Solution

Core Fixes

  • Added break statement in WebSocketDisconnect exception handler to properly exit the loop when a user disconnects
  • Prevented double unsubscription by adding an unsubscribed flag to track subscription state
  • Improved exception handling to re-raise critical errors (RuntimeError, ConnectionError) that indicate the websocket is broken
  • Simplified loop condition to rely on proper exception handling rather than state checking

Testing

  • Created comprehensive test suite with 7 test cases covering:
    • WebSocket subscriber functionality (success and error cases)
    • Disconnect handling (proper loop termination)
    • Double unsubscription prevention
    • Exception handling for different error types
    • Message processing verification
    • Finally block cleanup

Changes Made

Modified Files

  • openhands/agent_server/event_router.py: Fixed the websocket loop logic
  • tests/agent_server/test_event_router_websocket.py: Added comprehensive test suite

Key Changes

  1. Added break statement in WebSocketDisconnect handler
  2. Added unsubscribed flag to prevent double unsubscription
  3. Enhanced exception handling to re-raise critical errors
  4. Created thorough test coverage for websocket disconnect scenarios

Testing

  • ✅ All 7 new websocket tests pass
  • ✅ All existing 130 agent_server tests pass
  • ✅ Pre-commit hooks pass (formatting, linting, type checking)

Impact

This fix resolves the infinite loop issue that occurred when users disconnected from websocket connections, ensuring proper cleanup and resource management.

@tofarr can click here to continue refining the PR

openhands-agent and others added 3 commits September 23, 2025 03:14
- Add break statement in WebSocketDisconnect handler to exit loop properly
- Prevent double unsubscription with unsubscribed flag tracking
- Improve exception handling for critical errors (RuntimeError, ConnectionError)
- Add comprehensive test suite covering disconnect scenarios
- Ensure proper cleanup in finally block

Fixes infinite loop issue that occurred when users disconnected from websocket

Co-authored-by: openhands <openhands@all-hands.dev>
@tofarr
tofarr marked this pull request as ready for review September 23, 2025 14:13
@xingyaoww
xingyaoww enabled auto-merge (squash) September 23, 2025 14:33
@xingyaoww
xingyaoww merged commit f89a683 into main Sep 23, 2025
@xingyaoww
xingyaoww deleted the fix-websocket-disconnect-loop branch September 23, 2025 14:35
@github-actions

Copy link
Copy Markdown
Contributor

Agent Server image for this PR

Pull (multi-arch manifest):

docker pull ghcr.io/all-hands-ai/agent-server:a00799a

Run:

docker run -it --rm \
  -p 8000:8000 \
  --name agent-server-a00799a \
  ghcr.io/all-hands-ai/agent-server:a00799a

This tag is a multi-arch manifest (amd64/arm64). Your client pulls the right arch automatically.

StressTestor pushed a commit to StressTestor/software-agent-sdk that referenced this pull request Jun 1, 2026
Co-authored-by: openhands <openhands@all-hands.dev>
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