From 56fec538e1aa7558dc4a7adea7134394f76251f6 Mon Sep 17 00:00:00 2001 From: Matt Mastracci Date: Mon, 29 Apr 2024 09:40:02 -0600 Subject: fix(ext/http): ensure signal is created iff requested (#23601) This correctly creates the `AbortSignal` regardless of when we request it. If the signal is requested after the request has completed, the signal is created in the aborted state. Using GC counts, we can see a reduction in object creation: This PR: 440 deno 1.42.4: 1650 deno 1.43.0+b02ffec: 874 --- tests/unit/serve_test.ts | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'tests') diff --git a/tests/unit/serve_test.ts b/tests/unit/serve_test.ts index 8978c4f7e..74628ace1 100644 --- a/tests/unit/serve_test.ts +++ b/tests/unit/serve_test.ts @@ -23,6 +23,7 @@ const { addTrailers, serveHttpOnListener, serveHttpOnConnection, + getCachedAbortSignal, // @ts-expect-error TypeScript (as of 3.7) does not support indexing namespaces by symbol } = Deno[Deno.internal]; @@ -2838,6 +2839,34 @@ for (const delay of ["delay", "nodelay"]) { } } +// Test for the internal implementation detail of cached request signals. Ensure that the request's +// signal is aborted if we try to access it after the request has been completed. +Deno.test( + { permissions: { net: true } }, + async function httpServerSignalCancelled() { + let stashedRequest; + const { finished, abort } = await makeServer((req) => { + // The cache signal is `undefined` because it has not been requested + assertEquals(getCachedAbortSignal(req), undefined); + stashedRequest = req; + return new Response("ok"); + }); + await (await fetch(`http://localhost:${servePort}`)).text(); + abort(); + await finished; + + // `false` is a semaphore for a signal that should be aborted on creation + assertEquals(getCachedAbortSignal(stashedRequest!), false); + // Requesting the signal causes it to be materialized + assert(stashedRequest!.signal.aborted); + // The cached signal is now a full `AbortSignal` + assertEquals( + getCachedAbortSignal(stashedRequest!).constructor, + AbortSignal, + ); + }, +); + Deno.test( { permissions: { net: true } }, async function httpServerCancelFetch() { -- cgit v1.2.3