0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-02-01 20:25:12 -05:00

fix(ext/cache): close resource on error (#16129)

This commit is contained in:
Marcos Casagrande 2022-10-03 06:18:59 +02:00 committed by GitHub
parent b3444e0d3b
commit e2990be264
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 8 deletions

View file

@ -106,7 +106,7 @@ Deno.test(async function cachePutReaderLock() {
response, response,
); );
assertRejects( await assertRejects(
async () => { async () => {
await response.arrayBuffer(); await response.arrayBuffer();
}, },
@ -116,3 +116,25 @@ Deno.test(async function cachePutReaderLock() {
await promise; await promise;
}); });
Deno.test(async function cachePutResourceLeak() {
const cacheName = "cache-v1";
const cache = await caches.open(cacheName);
const stream = new ReadableStream({
start(controller) {
controller.error(new Error("leak"));
},
});
await assertRejects(
async () => {
await cache.put(
new Request("https://example.com/"),
new Response(stream),
);
},
Error,
"leak",
);
});

16
ext/cache/01_cache.js vendored
View file

@ -141,15 +141,17 @@
}, },
); );
if (reader) { if (reader) {
while (true) { try {
const { value, done } = await reader.read(); while (true) {
if (done) { const { value, done } = await reader.read();
await core.shutdown(rid); if (done) {
core.close(rid); break;
break; }
} else {
await core.write(rid, value); await core.write(rid, value);
} }
} finally {
await core.shutdown(rid);
core.close(rid);
} }
} }
// Step 12-19: TODO(@satyarohith): do the insertion in background. // Step 12-19: TODO(@satyarohith): do the insertion in background.