diff options
| author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2021-09-08 10:12:23 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-09-08 10:12:23 +0200 |
| commit | 2de5587547247e3acdffecae1c74caf52a021580 (patch) | |
| tree | 65405417c62008ffcf73a6721bc032b69775279e | |
| parent | c04117134ecb07fb6379bfe1057c9da8e0c4f206 (diff) | |
fix(ext/http): resource leak if request body is not consumed (#11955)
| -rw-r--r-- | cli/tests/unit/http_test.ts | 42 | ||||
| -rw-r--r-- | ext/http/01_http.js | 7 |
2 files changed, 49 insertions, 0 deletions
diff --git a/cli/tests/unit/http_test.ts b/cli/tests/unit/http_test.ts index ccacfd751..c243d6442 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 073cc7a7c..2e742d097 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 */ } } }; } |
