Skip to content

Commit 9efd6fa

Browse files
irvinebroqueToriLindsay
authored andcommitted
Add docs for 'cannot perform I/O on behalf of a different request (#16844)
* Add docs for 'cannot perform I/O on behalf of a different request refs cloudflare/workerd#2715 * Apply suggestions from code review Co-authored-by: ToriLindsay <[email protected]> --------- Co-authored-by: ToriLindsay <[email protected]>
1 parent 0612f54 commit 9efd6fa

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

src/content/docs/workers/observability/errors.mdx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,49 @@ export default {
132132
};
133133
```
134134

135+
136+
### Cannot perform I/O on behalf of a different request
137+
138+
```
139+
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.
140+
```
141+
142+
This error occurs when you attempt to share input/output (I/O) objects (such as streams, requests, or responses) created by one invocation of your Worker in the context of a different invocation.
143+
144+
In Cloudflare Workers, each invocation is handled independently and has its own execution context. This design ensures optimal performance and security by isolating requests from one another. When you try to share I/O objects between different invocations, you break this isolation. Since these objects are tied to the specific request they were created in, accessing them from another request's handler is not allowed and leads to the error.
145+
146+
This error is most commonly caused by attempting to cache an I/O object, like a [Request](/workers/runtime-apis/request/) in global scope, and then access it in a subsequent request. For example, if you create a Worker and run the following code in local development, and make two requests to your Worker in quick succession, you can reproduce this error:
147+
148+
```js
149+
let cachedResponse = null;
150+
151+
export default {
152+
async fetch(request, env, ctx) {
153+
if (cachedResponse) { return cachedResponse; }
154+
cachedResponse = new Response('Hello, world!');
155+
await new Promise(resolve => setTimeout(resolve, 5000)); // Sleep for 5s to demonstrate this particular error case
156+
return cachedResponse;
157+
}
158+
};
159+
```
160+
161+
You can fix this by instead storing only the data in global scope, rather than the I/O object itself:
162+
163+
```js
164+
let cachedData = null;
165+
166+
export default {
167+
async fetch(request, env, ctx) {
168+
if (cachedData) { return new Response(cachedData); }
169+
const response = new Response('Hello, world!');
170+
cachedData = await response.text();
171+
return new Response(cachedData, response);
172+
}
173+
};
174+
```
175+
176+
If you need to share state across requests, consider using [Durable Objects](/durable-objects/). If you need to cache data across requests, consider using [Workers KV](/kv/).
177+
135178
## Errors on Worker upload
136179

137180
These errors occur when a Worker is uploaded or modified.

0 commit comments

Comments
 (0)