Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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