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:
parent
b3444e0d3b
commit
e2990be264
2 changed files with 32 additions and 8 deletions
|
@ -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
16
ext/cache/01_cache.js
vendored
|
@ -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.
|
||||
|
|
Loading…
Add table
Reference in a new issue