From 2c2e6adae86874ccb9a3fd2843cf2b50a0847bac Mon Sep 17 00:00:00 2001 From: Matt Mastracci Date: Mon, 3 Jul 2023 09:30:02 -0600 Subject: [PATCH] fix(ext/http): Catch errors in eager stream timeout to avoid uncaught promise rejections (#19691) Fixes #19687 by adding a rejection handler to the write inside the setTimeout. There is a small window where the promise is actually not awaited and may reject without a handler. --- ext/http/00_serve.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/ext/http/00_serve.js b/ext/http/00_serve.js index 761c3219eb..43b04ff28c 100644 --- a/ext/http/00_serve.js +++ b/ext/http/00_serve.js @@ -427,7 +427,17 @@ async function asyncResponse(responseBodies, req, status, stream) { responseRid = op_http_set_response_body_stream(req); SetPrototypeAdd(responseBodies, responseRid); op_http_set_promise_complete(req, status); - timeoutPromise = core.writeAll(responseRid, value1); + // TODO(mmastrac): if this promise fails before we get to the await below, it crashes + // the process with an error: + // + // 'Uncaught (in promise) BadResource: failed to write'. + // + // To avoid this, we're going to swallow errors here and allow the code later in the + // file to re-throw them in a way that doesn't appear to be an uncaught promise rejection. + timeoutPromise = PromisePrototypeCatch( + core.writeAll(responseRid, value1), + () => null, + ); }, 250); const { value: value2, done: done2 } = await reader.read();