Route handler-level parse errors back to peer instead of completing client futures#17
Merged
Merged
Conversation
…lient futures HeaderDelimitedMessageHandler.receive() returned JsonRpcError.invalidRequest(...) for frame and parse failures on inbound messages. JsonRpc.bind() then treated every JsonRpcError as a peer response and dispatched it through openRequests based on id. With PR #14's extractId() recovering an id from malformed bytes where possible, this caused two distinct silent failure modes: - if the extracted id collided with an open client request, that unrelated client future was completed exceptionally with the peer's malformed message treated as a wire error response; - if the extracted id was null (the more common case), the "Error with no id" branch failed every open client future at once. In neither case was the actual error sent back to the peer. Introduce JsonRpcReceiveException carrying the recovered id and an JsonRpcError.Detail. Both HeaderDelimitedMessageHandler and NewLineDelimitedMessageHandler now throw it for frame and parse failures. JsonRpc.bind() catches it and forks the error response back to the peer; openRequests is left untouched. NewLineDelimitedMessageHandler also now distinguishes mid-message EOF (peer died with partial bytes — throws EOFException, matches Header) from a parse failure on a complete frame (throws JsonRpcReceiveException with id extraction where possible). Extracts the previously private extractId logic into IdExtractor so both handlers share it.
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
HeaderDelimitedMessageHandler.receive()returnedJsonRpcError.invalidRequest(...)for frame and parse failures on inbound messages, andJsonRpc.bind()treated everyJsonRpcErroras a peer response. Combined with PR Fix silent hang when deserialization fails with no request id #14'sextractId()recovering an id from malformed bytes where possible, this gave two silent failure modes:if the extracted id collided with an open client request, that unrelated client future was completed exceptionally with the peer's malformed message treated as a wire error response;
if the extracted id was
null(the more common case), the "Error with no id" branch inJsonRpc.bindfailed every open client future at once.In neither case was the actual error sent back to the peer that produced the malformed message.
This PR:
JsonRpcReceiveException(extendsIOException) carrying the recovered id +JsonRpcError.Detail. Both delimited handlers now throw it for frame/parse failures rather than returning aJsonRpcError.JsonRpc.bind()that forks the error response back to the peer;openRequestsis left untouched.NewLineDelimitedMessageHandlerto symmetric error semantics: distinguishes mid-message EOF (peer died with partial bytes — throwsEOFException) from parse failure on a complete frame (throwsJsonRpcReceiveExceptionwith id extraction where possible).extractIdrecovery logic fromHeaderDelimitedMessageHandlerinto a package-privateIdExtractorso both handlers share it.Test plan
./gradlew test— all 22 tests pass (1 pre-existing@Disabled)JsonRpcTest.malformedInboundDoesNotCompleteOpenClientRequests— new regression test demonstrating the fix. Verified by neutralizing the new catch inJsonRpc.bindand observing the test fail (no error response sent to peer), then restoring.NewLineDelimitedMessageHandlerTest— new test class (the handler had no tests previously); covers happy path, EOF between messages, mid-message EOF, malformed JSON in a complete frame, and id extraction from a partial frame.HeaderDelimitedMessageHandlerTest.receiveStillReturnsInvalidRequestForNonEmptyMalformedHeaderrenamed toreceiveThrowsForNonEmptyMalformedHeaderand updated to assert the new throw contract.