Skip to content

Commit de46572

Browse files
zahalzelbenbrandt
authored andcommitted
fix: avoid spurious unhandledRejection when transport fails mid-sendRequest
If the transport fails (or #receive observes stream closure) while sendRequest is awaiting #sendMessage, #close() rejects the response promise before the caller's await has attached a handler. Node then reports a spurious unhandledRejection (followed by PromiseRejectionHandledWarning), even though the caller's await does observe the rejection. Attach a noop .catch to responsePromise so the rejection is considered handled at the time it's raised. The rejection is still delivered to the caller verbatim via 'return responsePromise'.
1 parent c6e6ee2 commit de46572

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

src/acp.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,6 +1468,14 @@ class Connection {
14681468
const responsePromise = new Promise((resolve, reject) => {
14691469
this.pendingResponses.set(id, { resolve, reject });
14701470
});
1471+
// If the transport fails (or receive observes stream closure) during the
1472+
// `await sendMessage` below, close() will reject `responsePromise`
1473+
// before the caller has had a chance to attach its own handler. Node then
1474+
// reports a spurious `unhandledRejection`, even though the caller's
1475+
// subsequent `await` does observe the rejection. Attach a noop catch so
1476+
// the rejection is considered handled at the time it's raised; the
1477+
// rejection is still delivered to the caller via `return responsePromise`.
1478+
responsePromise.catch(() => {});
14711479
await this.sendMessage({ jsonrpc: "2.0", id, method, params });
14721480
return responsePromise as Promise<Resp>;
14731481
}

0 commit comments

Comments
 (0)