1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-24 08:09:08 -05:00

fix(ext/http): resource leak if request body is not consumed (#11955)

This commit is contained in:
Bartek Iwańczuk 2021-09-08 10:12:23 +02:00 committed by GitHub
parent c04117134e
commit 2de5587547
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 0 deletions

View file

@ -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;
},
);

View file

@ -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 */ }
}
};
}