You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/docs/workers/observability/errors.mdx
+43Lines changed: 43 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -132,6 +132,49 @@ export default {
132
132
}
133
133
```
134
134
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
+
exportdefault {
152
+
asyncfetch(request, env, ctx) {
153
+
if (cachedResponse) { return cachedResponse; }
154
+
cachedResponse =newResponse('Hello, world!');
155
+
awaitnewPromise(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
+
exportdefault {
167
+
asyncfetch(request, env, ctx) {
168
+
if (cachedData) { returnnewResponse(cachedData); }
169
+
constresponse=newResponse('Hello, world!');
170
+
cachedData =awaitresponse.text();
171
+
returnnewResponse(cachedData, response);
172
+
}
173
+
};
174
+
```
175
+
176
+
If you need to share state across requests, consider using [Durable Objects](/durable-objects/), or if you need to cache data across requests, consider using [Workers KV](/kv/).
177
+
135
178
## Errors on Worker upload
136
179
137
180
These errors occur when a Worker is uploaded or modified.
0 commit comments