HELP: Random Errors when Storing Data inside the Cache with Workers

Hi.

I am getting from time to time always this strange random Errors for the Code here when trying to store something inside the Cache.

The strange error output is:

TypeError: This readable stream is currently locked to a reader.

TypeError: This readable stream is currently locked to a reader.
at handleRequest (worker.js:185:68)

Error: Cannot perform I/O on behalf of a different request. I/O objects (such as streams, request/response bodies, and others) created in the context of one request handler cannot be accessed from a different request’s handler. This is a limitation of Cloudflare Workers which allows us to improve overall performance.

Error: Cannot perform I/O on behalf of a different request. I/O objects (such as streams, request/response bodies, and others) created in the context of one request handler cannot be accessed from a different request’s handler. This is a limitation of Cloudflare Workers which allows us to improve overall performance.
** at handleRequest (worker.js:185:68)**

This happens only from time to time randomly other times there are no Errors !

I am using this code here to Store Data inside the Cache in my Worker:
What i am doing wrong ?

Is it something related to me or is this a CloudFlare Bug ?

Thanks in advance for helping me solve this erratic Bug !

// Get access to the local zone cache
// Create localCache as Global Variable to avoid memory over usage per Worker
var localCache = caches.default

async function handleRequest(event) {
//console.log(event.request.url)

try {
// Store the response in this local cache zone
// Use waitUntil so computational expensive tasks don"t delay the response
event.waitUntil(localCache.put(event.request, await response.clone()))
} catch (err) {
return new Response(‘Cache Response localCache.put 500 Internal Error\n\n’ + err.toString() + ‘\n\n’ + err.stack, {status: 500})
}

return response
}

Hi!
You have an error on put to cache.
Change: localCache.put(event.request, await response.clone()) to localCache.put(event.request, response.clone())

1 Like

Hi Aksi !

Thanks a lot for your Help !

Indeed the Errors have nothing to do with Cloudflare but with my Own Worker Script !
The Fault was on my side with the response.clone()

I just checked the Example and they dont use await.

I just removed this and fixed some other bugs i had in my code like variable not defined and availble.
Now everything looks perfect !

Thanks a lot !