From de4657288ff34043aebc5ce0006655a9553287d8 Mon Sep 17 00:00:00 2001 From: Zach Halzel Date: Fri, 17 Apr 2026 14:12:32 -0400 Subject: [PATCH] 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'. --- src/acp.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/acp.ts b/src/acp.ts index 51355bf..0693a72 100644 --- a/src/acp.ts +++ b/src/acp.ts @@ -1468,6 +1468,14 @@ class Connection { const responsePromise = new Promise((resolve, reject) => { this.pendingResponses.set(id, { resolve, reject }); }); + // If the transport fails (or receive observes stream closure) during the + // `await sendMessage` below, close() will reject `responsePromise` + // before the caller has had a chance to attach its own handler. Node then + // reports a spurious `unhandledRejection`, even though the caller's + // subsequent `await` does observe the rejection. Attach a noop catch so + // the rejection is considered handled at the time it's raised; the + // rejection is still delivered to the caller via `return responsePromise`. + responsePromise.catch(() => {}); await this.sendMessage({ jsonrpc: "2.0", id, method, params }); return responsePromise as Promise; }