diff --git a/cli/tests/unit/http_test.ts b/cli/tests/unit/http_test.ts index ccacfd751d..c243d6442c 100644 --- a/cli/tests/unit/http_test.ts +++ b/cli/tests/unit/http_test.ts @@ -882,3 +882,45 @@ unitTest( clientConn.close(); }, ); + +// https://github.com/denoland/deno/issues/11926 +unitTest( + { perms: { net: true } }, + async function httpServerDoesntLeakResources2() { + let listener: Deno.Listener; + let httpConn: Deno.HttpConn; + + const promise = (async () => { + listener = Deno.listen({ port: 4502 }); + for await (const conn of listener) { + httpConn = Deno.serveHttp(conn); + for await (const { request, respondWith } of httpConn) { + assertEquals(new URL(request.url).href, "http://127.0.0.1:4502/"); + // not reading request body on purpose + respondWith(new Response("ok")); + } + } + })(); + + const resourcesBefore = Deno.resources(); + const response = await fetch("http://127.0.0.1:4502", { + method: "POST", + body: "hello world", + }); + await response.text(); + const resourcesAfter = Deno.resources(); + // verify that the only new resource is "httpConnection", to make + // sure "request" resource is closed even if its body was not read + // by server handler + + for (const rid of Object.keys(resourcesBefore)) { + delete resourcesAfter[Number(rid)]; + } + + assertEquals(Object.keys(resourcesAfter).length, 1); + + listener!.close(); + httpConn!.close(); + await promise; + }, +); diff --git a/ext/http/01_http.js b/ext/http/01_http.js index 073cc7a7c7..2e742d0979 100644 --- a/ext/http/01_http.js +++ b/ext/http/01_http.js @@ -297,6 +297,13 @@ ws[_eventLoop](); } + } else { + // Try to close "request" resource. It might have been already consumed, + // but if it hasn't been we need to close it here to avoid resource leak. + try { + SetPrototypeDelete(httpConn.managedResources, requestRid); + core.close(requestRid); + } catch { /* pass */ } } }; }