Fix CLI streaming invocation close race - #3804
Merged
Merged
Conversation
Amp-Thread-ID: https://ampcode.com/threads/T-01a05c08-c013-7723-aa3b-a964c063a85c Co-authored-by: Amp <amp@ampcode.com>
✅ Deploy Preview for golemcloud canceled.
|
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Problem
A streaming agent can cancel its input while the CLI still has stdin frames queued in its independent WebSocket writer. The server then sends
StreamCancel(InputConsumer),Finished(Success), and a normal WebSocket close.If the CLI reader observes the close before the writer drains those queued frames, tungstenite moves the shared split socket into its closing state. The writer's next frame then returns
ProtocolError::SendAfterClosing. The CLI did not classify that terminal local write state as a closed connection, so a successfully completed invocation could fail with a WebSocket protocol error. The same race could also mask a malformed piped-input diagnostic collected during cancellation.Fix
SendAfterClosingprotocol variant as a closed connection, alongsideConnectionClosed,AlreadyClosed, and broken pipe.ReceivedAfterClosing) remains an error.This remains safe before protocol completion: a closed writer only stops outbound input handling, while the invocation still cannot succeed until the reader receives and validates
Finished.Verification
cargo test -p golem-cli --lib -- peer_close_racing_with_a_send_is_connection_closed connection_truncation_before_finish_is_an_error --report-timeintegration::app::agents::test_streaming_invocation_cli_end_to_endpassed 5 consecutive runscargo fmt -p golem-cli -- --checkcargo clippy -p golem-cli --all-targets --no-deps -- -D warningscargo check -p golem-cli --all-targetscargo make build-cli-test-bins