fix: reconnect when the WebSocket was closed by the far end - #1103
Open
sergiopaniego wants to merge 1 commit into
Open
fix: reconnect when the WebSocket was closed by the far end#1103sergiopaniego wants to merge 1 commit into
sergiopaniego wants to merge 1 commit into
Conversation
`_connect_async` only dropped the cached socket when it came from another event loop, so a socket closed by the far end stayed cached: `self._ws` is still not None, and every later call raised `ConnectionClosed` for the rest of the process's life. Drop it when its state is CLOSING or CLOSED, and have `_receive` ensure the connection rather than assume it.
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
EnvClientkeeps one persistent WebSocket._connect_asynconly dropped the cached socket when it came from a different event loop, so a socket closed by the far end stayed cached:self._wsis still notNone, and every laterreset/stepraisedConnectionClosedfor the rest of the process's life. This drops the socket when its state isCLOSINGorCLOSED, and has_receiveensure the connection instead of asserting it exists.Hit this training against two env Spaces: a transient network blip closed both clients, and neither recovered. The Spaces answered
POST /resetin 0.3s from outside while the jobs could not reach them for the rest of the run. Two runs lost, ~4.5 GPU-hours burnt on groups that scored zero.Type of Change
Alignment Checklist
Before submitting, verify:
.claude/docs/PRINCIPLES.mdand this PR aligns with our principles.claude/docs/INVARIANTS.mdand no invariants are violated/pre-submit-pr(orbash .claude/hooks/lint.shand tests) and addressed all issuesRFC Status
Test Plan
test_protocol_client_reconnects_after_closecloses the socket from under the client and asserts the next call still works. It fails onmainwithConnectionClosedOKand passes with the fix.Only a demonstrably closed socket is dropped, so one still
CONNECTINGis left alone and the existing same-loop no-op behaviour is unchanged.Claude Code Review
N/A
Note
Medium Risk
Touches persistent WebSocket lifecycle for all env clients; behavior change is narrowly scoped to demonstrably closed sockets, but long-running training jobs depend on this path.
Overview
EnvClientno longer treats a dead WebSocket as a live session. When_connect_asyncsees a cached socket inCLOSINGorCLOSED, it clears_wsand reconnects instead of reusing it and failing every later call withConnectionClosed. Sockets stillCONNECTINGor on the same event loop keep the existing short-circuit behavior._receivenow calls_ensure_connected()(like_send) so a receive path can trigger that reconnect after the far end drops the connection.Adds integration test
test_protocol_client_reconnects_after_close: force-close the socket under a sync client and assertreset/call_toolstill work.Reviewed by Cursor Bugbot for commit f872213. Bugbot is set up for automated code reviews on this repo. Configure here.