Skip to content

Using an AsyncLocalStorage snapshot across requests #4155

@vicb

Description

@vicb

Repro: https://github.com/vicb/als-snapshot

When an AsyncLocalStorage.snapshot() is used in a different request as the one it was created in, the following error is generated:

[wrangler:inf] GET / 302 Found (6ms)
using snapshot
[wrangler:err] 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: SpanParent)
    at Object.apply ([...]/strip-cf-connecting-ip-header.js:9:18)
    at null.<anonymous> ([...]/src/index.ts:31:39)
    at Object.fetch ([...]/src/index.ts:31:12)
    at fetchDispatcher ([...]/middleware-loader.entry.ts:54:17)

The issue happens when using composable cache in Next with the cloudflare adapter, see opennextjs/opennextjs-cloudflare#666 - Thanks @conico974 for figuring the issue out and submitting a patch.

I discussed this offline with @jasnell and he asked me to create the issue.

Not sure if this is something that we can fix or working as intended.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions