diff --git a/cli/tests/unit/flash_test.ts b/cli/tests/unit/flash_test.ts index 07be6dfcca..fef45beb9b 100644 --- a/cli/tests/unit/flash_test.ts +++ b/cli/tests/unit/flash_test.ts @@ -57,6 +57,35 @@ Deno.test(async function httpServerCanResolveHostnames() { await server; }); +Deno.test(async function httpServerRejectsOnAddrInUse() { + const ac = new AbortController(); + const listeningPromise = deferred(); + + const server = Deno.serve({ + handler: (_req) => new Response("ok"), + hostname: "localhost", + port: 4501, + signal: ac.signal, + onListen: onListen(listeningPromise), + onError: createOnErrorCb(ac), + }); + + assertRejects( + () => + Deno.serve({ + handler: (_req) => new Response("ok"), + hostname: "localhost", + port: 4501, + signal: ac.signal, + onListen: onListen(listeningPromise), + onError: createOnErrorCb(ac), + }), + Deno.errors.AddrInUse, + ); + ac.abort(); + await server; +}); + Deno.test({ permissions: { net: true } }, async function httpServerBasic() { const ac = new AbortController(); const promise = deferred(); diff --git a/ext/flash/01_http.js b/ext/flash/01_http.js index 4867da1617..fbc24d73d2 100644 --- a/ext/flash/01_http.js +++ b/ext/flash/01_http.js @@ -28,7 +28,7 @@ const { Function, ObjectPrototypeIsPrototypeOf, - PromiseResolve, + PromiseAll, TypedArrayPrototypeSubarray, TypeError, Uint8Array, @@ -249,7 +249,8 @@ core.opAsync("op_flash_wait_for_listening", serverId).then((port) => { onListen({ hostname: listenOpts.hostname, port }); - }); + }).catch(() => {}); + const finishedPromise = serverPromise.catch(() => {}); const server = { id: serverId, @@ -257,7 +258,7 @@ hostname: listenOpts.hostname, port: listenOpts.port, closed: false, - finished: PromiseResolve(serverPromise), + finished: finishedPromise, async close() { if (server.closed) { return; @@ -551,7 +552,10 @@ }, 1000); } - return await server.serve().catch(console.error); + await PromiseAll([ + server.serve().catch(console.error), + serverPromise, + ]); } function createRequestBodyStream(serverId, token) { diff --git a/ext/flash/lib.rs b/ext/flash/lib.rs index 2c76c450fe..90363c0825 100644 --- a/ext/flash/lib.rs +++ b/ext/flash/lib.rs @@ -1313,8 +1313,11 @@ fn op_flash_wait_for_listening( server_ctx.listening_rx.take().unwrap() }; Ok(async move { - let port = listening_rx.recv().await.unwrap(); - Ok(port) + if let Some(port) = listening_rx.recv().await { + Ok(port) + } else { + Err(generic_error("This error will be discarded")) + } }) }