1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-21 21:50:00 -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,
);
assertRejects(
await assertRejects(
async () => {
await response.arrayBuffer();
},
@ -116,3 +116,25 @@ Deno.test(async function cachePutReaderLock() {
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) {
while (true) {
const { value, done } = await reader.read();
if (done) {
await core.shutdown(rid);
core.close(rid);
break;
} else {
try {
while (true) {
const { value, done } = await reader.read();
if (done) {
break;
}
await core.write(rid, value);
}
} finally {
await core.shutdown(rid);
core.close(rid);
}
}
// Step 12-19: TODO(@satyarohith): do the insertion in background.