Csaba Varga
Csaba Varga
CDCloudflare Developers
Created by Csaba Varga on 7/26/2024 in #workers-help
Deadlock in cache.put() - platform issue or am I doing something wrong?
The following worker code results in a deadlock (no response is ever sent to the client), both in a local Wrangler instance and in the Playground:
export default {
async fetch(request, env, ctx) {
let myResponse = await fetch('http://example.com/');
myResponse = new HTMLRewriter()
.on('h1', {
async element(e) {
await caches.default.put(new Request('http://example.com/dummy'), new Response('foo'));
e.setInnerContent('Rewritten');
}
})
.transform(myResponse);
caches.default.put(request, myResponse.clone());
return myResponse;
},
};
export default {
async fetch(request, env, ctx) {
let myResponse = await fetch('http://example.com/');
myResponse = new HTMLRewriter()
.on('h1', {
async element(e) {
await caches.default.put(new Request('http://example.com/dummy'), new Response('foo'));
e.setInnerContent('Rewritten');
}
})
.transform(myResponse);
caches.default.put(request, myResponse.clone());
return myResponse;
},
};
This is a minimized version of an issue we have with our worker on Production, the (presumably) same issue is causing some of our requests to never be answered, causing visitor frustration. The issue seems to be triggered by a cache put() call happening while another put() call is waiting for the provided response to complete. As the cache keys are different, I can't see a good reason why the two calls should block each other, though. My question is: does this code try to do something unsupported, or is this supposed to work? It feels like a bug in the worker implementation, but I may be missing something.
11 replies