Sharing Promises across requests results in error for local dev environment, but works in production

If a particular response for an origin URL is not found in cache, in order to stop Cloudflare from making multiple requests to my origin server if two or more parallel requests come in at the same time for the same uncached URL, I “share” the fetch() Promise across multiple requests using a global variable.

This seems to work perfectly fine in production (only one request is made to the origin server while the other requests wait for the shared fetch() Promise to resolve), but if I’m running wrangler dev on my local machine and make multiple parallel requests to uncached URLs (so that the sharing of the Promise kicks in), I get this error:

:x:[ERROR] Uncaught (in promise) 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.

Again, this is only on my local machine running wrangler dev (not in “local” mode, so from my understanding the requests are proxied by wrangler to an actual Cloudflare data center nearby).

I’m happy it’s working fine in production, but the above error worries me that it could stop working at any moment if the production environment suddenly decides to switch over to a more restrictive mode.

Can someone from Cloudflare please chime in and let me know which of the above outcomes can be relied upon (i.e. always erroring or always working)?

Thanks,
Nick

No, this is not safe outside of a Durable Object. The reason it works in production is probably because requests are fanning out across enough machines that the sharing logic in the worker isn’t kicking in. If your traffic level were to increase sufficiently, this would break.

In theory, the cache lock should prevent this from happening. If you’re observing this occurring, it might be worth a support request.

1 Like

Acknowledged. Thanks @harris for the quick response.

Cheers,
Nick

Hey @harris,

Would you mind giving more context on why it wouldn’t work?

We are experiencing something similar here and I thought sharing promises would be a good idea (and we actually do that on our origin). So I would like to understand better why it would break (and yes, we do have a ton of traffic).

Best,

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.