Skip to content

fix(core): gracefully handle runtimes that omit AsyncLocalStorage.enterWith() (fixes #2055)#2062

Open
octo-patch wants to merge 1 commit intobrowserbase:mainfrom
octo-patch:fix/issue-2055-flowlogger-enterWith-runtime-compat
Open

fix(core): gracefully handle runtimes that omit AsyncLocalStorage.enterWith() (fixes #2055)#2062
octo-patch wants to merge 1 commit intobrowserbase:mainfrom
octo-patch:fix/issue-2055-flowlogger-enterWith-runtime-compat

Conversation

@octo-patch
Copy link
Copy Markdown
Contributor

@octo-patch octo-patch commented Apr 28, 2026

Fixes #2055

Problem

Since v3.2.0, FlowLogger.init() calls AsyncLocalStorage.enterWith() to set the session-wide logging context. Cloudflare Workers intentionally omits enterWith() from their AsyncLocalStorage implementation — calling it throws asyncLocalStorage.enterWith() is not implemented, which crashes Stagehand immediately during init() before any browser interaction occurs.

Solution

Wrap the enterWith() call in a try/catch. If the runtime does not support enterWith(), we fall through silently. The FlowLoggerContext is still created and returned, so it can be stored by the caller (this.flowLoggerContext in v3.ts). All other ALS usage inside FlowLogger already uses loggerContext.run() (in runWithLogging(), withContext(), logCdpEvent(), etc.), which is supported everywhere — so the logging pipeline continues to work through the explicit-context path on runtimes without enterWith().

Testing

Added a unit test that simulates a runtime without enterWith() (by temporarily setting AsyncLocalStorage.prototype.enterWith = undefined) and asserts that FlowLogger.init() completes without throwing and returns a valid context.

✓ FlowLogger.init() does not throw when enterWith() is not implemented (e.g. Cloudflare Workers)

All existing flowlogger unit tests continue to pass.


Summary by cubic

Fixes #2055. Prevents crashes on runtimes that omit AsyncLocalStorage.enterWith() (e.g., Cloudflare Workers) by safely initializing FlowLogger. Logging continues via loggerContext.run() paths.

  • Bug Fixes
    • Wrap enterWith() in try/catch in FlowLogger.init(); fall through when missing.
    • Add a unit test that simulates missing enterWith() and verifies init succeeds and returns a valid FlowLoggerContext.

Written for commit 7211ad5. Summary will update on new commits. Review in cubic

…erWith() (fixes browserbase#2055)

Cloudflare Workers intentionally omit enterWith() from their AsyncLocalStorage
implementation because it mutates context across the entire remaining async
chain, which is unsafe under concurrent requests. Calling it throws immediately,
crashing Stagehand during init() before any browser work begins.

Wrap the enterWith() call in a try/catch so the init() path stays safe on any
runtime. The returned FlowLoggerContext is still valid; callers that need ALS
propagation use loggerContext.run() via runWithLogging() and withContext(),
which are already runtime-safe.

Adds a unit test that simulates the missing enterWith() and asserts that
FlowLogger.init() completes without throwing and returns a valid context.

Co-Authored-By: Octopus <liyuan851277048@icloud.com>
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Apr 28, 2026

⚠️ No Changeset found

Latest commit: 7211ad5

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions
Copy link
Copy Markdown
Contributor

This PR is from an external contributor and must be approved by a stagehand team member with write access before CI can run.
Approving the latest commit mirrors it into an internal PR owned by the approver.
If new commits are pushed later, the internal PR stays open but is marked stale until someone approves the latest external commit and refreshes it.

@github-actions github-actions Bot added external-contributor Tracks PRs mirrored from external contributor forks. external-contributor:awaiting-approval Waiting for a stagehand team member to approve the latest external commit. labels Apr 28, 2026
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 2 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.
Architecture diagram
sequenceDiagram
    participant S as Stagehand / Caller
    participant FL as FlowLogger
    participant ALS as AsyncLocalStorage
    participant Bus as Event Bus

    Note over S, ALS: Initialization Flow (v3.ts)

    S->>FL: init(sessionId, eventBus)
    FL->>FL: Create FlowLoggerContext (ctx)

    rect rgb(240, 240, 240)
        Note right of FL: NEW: Guard against restricted runtimes
        alt Runtime supports enterWith() (e.g. Node.js)
            FL->>ALS: enterWith(ctx)
            ALS-->>FL: Context globally set for current execution
        else Runtime omits enterWith() (e.g. Cloudflare Workers)
            FL->>ALS: enterWith(ctx)
            ALS-->>FL: THROW: "not implemented"
            FL->>FL: Catch and suppress error
        end
    end

    FL-->>S: Return FlowLoggerContext

    Note over S, ALS: Execution Flow (Post-Init)

    S->>FL: runWithLogging(ctx, task)
    Note right of FL: Explicit context path works even if enterWith() failed
    FL->>ALS: run(ctx, task)
    ALS->>Bus: Emit logging events
    ALS-->>FL: Result
    FL-->>S: Result
Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external-contributor:awaiting-approval Waiting for a stagehand team member to approve the latest external commit. external-contributor Tracks PRs mirrored from external contributor forks.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FlowLogger breaks on Cloudflare Workers: enterWith() is not supported

1 participant