-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Description
Note
The pull request "fix(cloudflare): Send correct events in local development" was created by @JPeer264 but did not reference an issue. Therefore this issue was created for better visibility in external tools like Linear.
When using wrangler dev there is an isolation scope spawned as in production, but there is only one for the worker and the durable object (DO) (while in production these two get 1 each). With local development this could be a problem with the flush. The RPC is done inside the worker, which means at the time the DO is getting called the initial worker async local storage (ALS) is overwritten by the DO. That leads to a wrong client when the worker flushes after all calls are done. Here a little mermaid graph to showcase this a little better:
sequenceDiagram
participant W as Worker
participant ALS as AsyncLocalStorage
participant DO as Durable Object
participant S as Sentry Proxy
W->>ALS: Initialize ALS context (Worker client)
W->>DO: stub.fetch(request)
DO->>ALS: Initialize ALS context (DO client)
Note over ALS: ALS now holds DO client<br/>(single-threaded wrangler dev<br/>overwrites Worker context)
DO->>S: flush() ✅ (DO client correct)
DO-->>W: Response
W->>ALS: getIsolationScope().getClient()
Note over W,ALS: ❌ Returns DO client instead<br/>of Worker client
W->>S: flush() ❌ (wrong client)
Note over W: Worker events not flushed<br/>or flushed to wrong client
As we already have the client available inside the flush function (as we pass it through), we can directly call it if it is available.