Skip to content

Commit

Permalink
fix(ext/cache): acquire reader lock before async op (denoland#16126)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosc90 authored Oct 1, 2022
1 parent 048c06f commit a55b194
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
22 changes: 22 additions & 0 deletions cli/tests/unit/cache_api_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,25 @@ Deno.test(async function cacheApi() {
assert(await caches.delete(cacheName));
assertFalse(await caches.has(cacheName));
});

Deno.test(async function cachePutReaderLock() {
const cacheName = "cache-v1";
const cache = await caches.open(cacheName);

const response = new Response("consumed");

const promise = cache.put(
new Request("https://example.com/"),
response,
);

assertRejects(
async () => {
await response.arrayBuffer();
},
TypeError,
"Body already consumed.",
);

await promise;
});
7 changes: 4 additions & 3 deletions ext/cache/01_cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,10 @@

// Step 8.
if (innerResponse.body !== null && innerResponse.body.unusable()) {
throw new TypeError("Response body must not already used");
throw new TypeError("Response body is already used");
}
// acquire lock before async op
const reader = innerResponse.body?.stream.getReader();

// Remove fragment from request URL before put.
reqUrl.hash = "";
Expand All @@ -138,8 +140,7 @@
responseStatusText: innerResponse.statusMessage,
},
);
if (innerResponse.body) {
const reader = innerResponse.body.stream.getReader();
if (reader) {
while (true) {
const { value, done } = await reader.read();
if (done) {
Expand Down

0 comments on commit a55b194

Please sign in to comment.