Skip to content

Fix unhandled rejection when writing a JSON-RPC request fails - #1855

Open
Heejae Chang (heejaechang) wants to merge 1 commit into
microsoft:mainfrom
heejaechang:heejaechang-json-rpc-request-errors
Open

Heejae Chang (heejaechang) wants to merge 1 commit into
microsoft:mainfrom
heejaechang:heejaechang-json-rpc-request-errors

Conversation

@heejaechang

Copy link
Copy Markdown
Contributor

Summary

When a peer exits or a transport closes while a request is being written, sendRequest rejects the caller-visible promise with ResponseError(ErrorCodes.MessageWriteError, ...). Its catch block then rethrows the original writer error from an async Promise executor. The Promise constructor ignores that executor's returned promise, so the rethrow creates a second, unhandled rejection even when the caller correctly handles the request failure. This can fail tests or terminate a host process.

Remove only the redundant rethrow. The original request rejection and error message, pending-response removal, cancellation cleanup, and logging are preserved.

Regression coverage

Add a deterministic Node test using createMessageConnection and a destroyed PassThrough stream. A child process runs with --unhandled-rejections=strict, explicitly handles the request rejection, and exits naturally. This isolates rejection handling from Mocha without suppressing unhandled rejections or using sleeps. The test also checks transport error reporting, logging, pending-response removal, cancellation cleanup, and cancellation-listener disposal.

Validation

  • Before the fix: the new regression fails with an unhandled ERR_STREAM_DESTROYED.
  • After the fix: the regression and seven related request/cancellation/disposal tests pass.
  • npm run compile:jsonrpc
  • npm run test:node --prefix jsonrpc -- --grep "Request write failure does not cause an unhandled rejection|Handle Single Request|Handle Multiple Requests|Cancellation|cancel|dispose"
  • npm run lint --prefix jsonrpc

Validated on Windows with Node.js 24.15.0. A full root dependency restore was blocked by an unavailable package in the local package mirror; focused validation used the repository's exact direct compiler/test/lint tool versions without modifying repository manifests or lockfiles. Browser runtime tests were not run.

Remove the redundant throw after rejecting the request promise. Add a strict subprocess regression covering a destroyed stream, the caller error, error reporting, and request cancellation cleanup.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

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.

1 participant