-
Notifications
You must be signed in to change notification settings - Fork 304
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Patch v8 to support promise cross-context resolution #2719
Conversation
TODOs
|
patches/v8/0019-Implement-cross-request-context-promise-resolve-hand.patch
Outdated
Show resolved
Hide resolved
5ff9282
to
a8c8226
Compare
a8c8226
to
1418481
Compare
The workerd C++ code seems fine to me. I'd like to defer to @erikcorry for the V8 side as I don't feel like I understand it. Is there some way we can make it easier to review the patch? Maybe fork the v8 repo, commit it in your fork, and link the commit from here? |
1418481
to
70bc8e4
Compare
This comment was marked as resolved.
This comment was marked as resolved.
patches/v8/0019-Implement-cross-request-context-promise-resolve-hand.patch
Show resolved
Hide resolved
patches/v8/0019-Implement-cross-request-context-promise-resolve-hand.patch
Outdated
Show resolved
Hide resolved
patches/v8/0019-Implement-cross-request-context-promise-resolve-hand.patch
Show resolved
Hide resolved
70bc8e4
to
42bc8f6
Compare
CI is failing due to a transient issue downloading stuff.... |
patches/v8/0019-Implement-cross-request-context-promise-resolve-hand.patch
Outdated
Show resolved
Hide resolved
Here's a diff to the diff file, which uses Handles more, and raw Tagged pointers less. Feel free to use it, or I'll just do a separate change tomorrow (where I'll probably also unify the 0007 and 0019 patches to one file). |
d1853b2
to
fb461e6
Compare
78042db
to
58042d2
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not quite done reviewing but need to go to meetings, so here's my comments so far.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK I actually read through everything this time (except the V8 patch).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approving workerd code... trusting @erikcorry to review V8 code.
34c80e3
to
264825f
Compare
Rebased and squashed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
``` 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"); } }; ```
264825f
to
d501b6d
Compare
Draft for now until I add tests tests and more tests
Some details on how this works:
kj::Own<IoContext::WeakRef>
IoContext
becomes current/locked, thatIoContext
s promise tag is associated with thev8::Isolate
as the "current promise tag"Promise
is created, the Isolate's "current promise tag" is associated with the Promise in an internal field.Promise
is followed (.then(...), .catch(...), etc), we check to see if the current promise tag matches the promises context tag. If they are not the same, then the promise is a "cross-context promise" and we arrange for proper cross-context signaling.resolve(...)
orreject(...)
methods are called, and we determine that the promise is a cross-context promise, scheduling of the promise continuations will be deferred until after we re-enter the promise's originalIoContext
.IoContext
has been destroyed, we drop the continuations on the floor and emit a warning.