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: pages/cloudflare/troubleshooting.mdx
+46Lines changed: 46 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -40,3 +40,49 @@ You may want to modify how Wrangler resolves multiple exports, such that when yo
40
40
WRANGLER_BUILD_CONDITIONS=""
41
41
WRANGLER_BUILD_PLATFORM="node"
42
42
```
43
+
44
+
### `Error: Cannot perform I/O on behalf of a different request.`
45
+
46
+
Some DB clients (i.e. [`postgres`](https://www.npmjs.com/package/postgres)) create a connection to the DB server when they are first instantiated and re-use it for later requests.
47
+
This programming model is not compatible with the Workers runtime where a connection can not be re-used in a different request.
48
+
49
+
The following error is generated in such a case:
50
+
51
+
```text
52
+
⨯ 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. (I/O type: Writable)
53
+
```
54
+
55
+
To solve this, you should create the DB client inside a request context and not keep a global DB client.
56
+
57
+
A global client would not work:
58
+
59
+
```ts
60
+
// src/lib/db.ts
61
+
importpostgresfrom"postgres";
62
+
63
+
// `client` is global.
64
+
// As the connection would be shared across requests, it fails on worker
0 commit comments