Repository navigation
fix(acp): keep the connection open when an incoming line is not valid UTF-8 - #504
Merged
Merged
Conversation
… UTF-8 `ByteStreams`, `Stdio` and `AcpAgent` read incoming lines with `BufReader::lines()`, which reports a line that is not valid UTF-8 as a read error. The incoming transport actor treats every read error as a transport failure, so one such line ended the whole connection, and every session on it. A line that is only not JSON was already a malformed frame that is answered while the connection stays usable. One shared reader now replaces bytes that are not valid UTF-8 with U+FFFD, as the agent's stderr is already decoded. A line with such a byte inside a JSON string is delivered with the replacement. Any other line is still not JSON, so it is a malformed frame like any other. Replacing the byte instead of dropping the line keeps a response or notification from going missing. `Lines` takes decoded strings and is unchanged. Its docs now say that an error from its incoming stream ends the connection.
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.
Problem
ByteStreams,StdioandAcpAgentread incoming lines withBufReader::lines(), which reports a line that is not valid UTF-8 as anInvalidDataread error. The incoming transport actor treats every read error as a transport failure. One such line therefore ended the whole connection withInternal error: stream did not contain valid UTF-8, along with every session on it.A line that is only not JSON is already handled differently. It becomes a
TransportFrame::Malformed, is answered with a parse error, and the connection stays usable.Change
One shared reader,
lossy_lines, splits the byte stream into lines and replaces bytes that are not valid UTF-8 with U+FFFD, the way the agent's stderr is already decoded. All three transports use it.Replacing the byte instead of dropping the line keeps a response or notification from going missing, which would leave the other side waiting.
Linesis unchanged. It takes strings that are already decoded, and anErrfrom its incoming stream still ends the connection. Its docs now say so.Testing
ByteStreams,test_invalid_utf8_is_replaced_and_connection_stays_alive. It fails without the fix.lossy_linesfor line endings and the replacement.just test, clippy for native and wasm targets,cargo fmt --check, and rustdoc with-D warningspass locally.StdioandAcpAgenthave no test of their own for this. They call the same function asByteStreams.