Skip to content

Commit

Permalink
Patch v8 to support promise cross-context resolution
Browse files Browse the repository at this point in the history
```
export default {
  async fetch(req, env, ctx) {
    if (globalThis.resolve === undefined) {

      {
        const { promise, resolve } = Promise.withResolvers();
        setTimeout(resolve, 1000);
        ctx.waitUntil(promise);
      }

      // This is our first request. We will create a new promise and resolver
      // pair and store it in a global. This request will then wait for the
      // promise to be resolved before continuing.
      const { promise, resolve } = Promise.withResolvers();
      globalThis.resolve = resolve;
      globalThis.promise = promise;
      const ab = AbortSignal.abort();
      console.log(ab.aborted);
      promise.then(() => {
        // This will be run within the correct IoContext now...
        try {
          console.log('test1', ab.aborted);
        } catch (err) {
          // We would get here if the IoContext was incorrect because
          // of the call to ab.aborted
          console.log(err.message);
        }
      });
    } else {
      // This is our second request. We will resolve the promise created in the
      // first request.
      console.log('....');
      globalThis.resolve();
      globalThis.resolve = undefined;
      console.log('test2');
    }

    return new Response("Hello World\n");
  }
};

```
  • Loading branch information
jasnell committed Sep 18, 2024
1 parent 1736ca2 commit 264825f
Show file tree
Hide file tree
Showing 13 changed files with 1,191 additions and 4 deletions.
1 change: 1 addition & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ http_archive(
"//:patches/v8/0016-Revert-TracedReference-deref-API-removal.patch",
"//:patches/v8/0017-Revert-heap-Add-masm-specific-unwinding-annotations-.patch",
"//:patches/v8/0018-Update-illegal-invocation-error-message-in-v8.patch",
"//:patches/v8/0019-Implement-cross-request-context-promise-resolve-hand.patch",
],
strip_prefix = "v8-12.9.202.13",
url = "https://github.com/v8/v8/archive/refs/tags/12.9.202.13.tar.gz",
Expand Down

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions src/workerd/api/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -548,3 +548,9 @@ wd_test(
args = ["--experimental"],
data = ["tests/new-module-registry-test.js"],
)

wd_test(
src = "tests/cross-context-promise-test.wd-test",
args = ["--experimental"],
data = ["tests/cross-context-promise-test.js"],
)
Loading

0 comments on commit 264825f

Please sign in to comment.