Repository navigation
cellb: lower await on in-browser sites, so AsyncLocalStorage keeps concurrent stores - #10
Merged
Merged
Conversation
…ncurrent stores A site's modules (createProjectSite with nodeCompat) now get their async functions lowered to generators after webrun-modules compiles them (lowerAwait, on acorn), as bundleWorkerForBrowser does with esbuild. The browser host then installs the polyfill's continuations for a site too: 16 objects answering at once each keep their own store, as on Node and in a bundle. Left native: async generators, async functions using super or new.target, top-level await; they see no store after an await rather than another event's. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SXJQA3LbggJHi5qZBpUKe8
… lowering otherwise In a realm with TC39 AsyncContext, AsyncLocalStorage is a layer over AsyncContext.Variable and Snapshot: the engine carries stores through native awaits, as in workerd, celld and Node, and installAsyncContext() patches nothing. asyncRoot() starts events in an AsyncContext.Snapshot. enterWith() has no AsyncContext counterpart and throws there. Without it the polyfill simulates AsyncContext as before: patched continuations plus lowered awaits. createProjectSite lowers by default only when the browser lacks AsyncContext (lowerAwait overrides); bundleWorkerForBrowser takes lowerAwait: false for such browsers. test/p18 runs the backend on an AsyncContext made of Node's engine-backed AsyncLocalStorage, with native awaits. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SXJQA3LbggJHi5qZBpUKe8
…ync-context cellb: node:async_hooks on native AsyncContext when the realm has it; lowering otherwise
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
On an in-browser site (
createProjectSite), modules were served with nativeawait. cellb'snode:async_hookspolyfill can only carry a store across anawaitthat goes throughPromise.prototype.then. As a result, the site fell back to run-at-a-time stores, and two objects answering at once could read each other'sAsyncLocalStoragestore. Bundles already avoided this, becausebundleWorkerForBrowserlowersawaitwith esbuild.Change
src/project/lower-await.ts(new, built on acorn): lowers async functions to generators driven by a small runner.await Xbecomes(yield X).for awaitbecomes a loop overyield it.next(). It closes the iterator on early exit and handles labels.Functions keep their
thisandarguments.Arrows forward
argumentsonly when they read them.Left as they are:
superornew.target;After an
await, these see no store rather than another event's.createProjectSite: withnodeCompat, runs webrun-modules' default transform and thenlowerAwait, for both the project's modules and loaded Workers.browser-host:installAsyncContext()now runs for every nodejs_compat project, sites included, not only bundles.gaps.md,browser-host.md, the polyfill's header comment and the flue-example README are updated. A site'snodeCompatmust match the project'snodejs_compatflag; this is now documented.Tests
test/p17-lower-await.test.ts: checks that the lowering keeps values, errors,this,argumentsand ordering, and coversfor await(async and sync iterables,break, labels, closing the iterator). With the polyfill installed, four concurrent runs each keep their own store.site-projects.spec: a new test runstest/async-context-worker.ts(16 objects at once, every kind of continuation) on a site.await.p09-celld-conformancealso occur on main.Generated by Claude Code