Skip to content

Commit

Permalink
fix(ext/cache): close resource on error (denoland#16129)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosc90 authored Oct 3, 2022
1 parent b3444e0 commit e2990be
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
24 changes: 23 additions & 1 deletion cli/tests/unit/cache_api_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ Deno.test(async function cachePutReaderLock() {
response,
);

assertRejects(
await assertRejects(
async () => {
await response.arrayBuffer();
},
Expand All @@ -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 changes: 9 additions & 7 deletions ext/cache/01_cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit e2990be

Please sign in to comment.