Repository navigation
fix: run promise continuations promptly when native code settles a promise outside a task - #54296
Conversation
…omise outside a task The browser process runs microtasks at kExplicit policy: Node.js performs a checkpoint when it leaves a libuv callback and MicrotasksRunner performs one after every task on the UI thread. gin_helper::Promise's SettleScope also tried to perform one when a promise was settled, but it did so while its own v8::MicrotasksScope was still on the stack, which makes PerformCheckpoint a no-op, so a promise settled from anywhere that is not a task never had its continuations run until some unrelated task came along. X11 replies are dispatched straight from the GLib source rather than from a task, so on Linux every clipboard.read()/readText()/has() call (which wait for a GetSelectionOwner reply) resolved only when the next timer or IPC message happened to wake the main loop: 4-30 s per call in an otherwise idle app. Post an empty task when a promise is settled on the UI thread outside a task so that MicrotasksRunner's checkpoint follows immediately, and add a testing hook that settles a promise from a native event source callback on each platform to cover it.
|
Release Notes Persisted
|
|
I was unable to backport this PR to "44-x-y" cleanly; |
|
I was unable to backport this PR to "43-x-y" cleanly; |
|
I was unable to backport this PR to "42-x-y" cleanly; |
|
I have automatically backported this PR to "45-x-y", please check out #54307 |
|
@MarshallOfSound has manually backported this PR to "44-x-y", please check out #54308 |
|
@MarshallOfSound has manually backported this PR to "43-x-y", please check out #54309 |
|
@MarshallOfSound has manually backported this PR to "42-x-y", please check out #54310 |
…omise outside a task (43-x-y) (#54309) fix: run promise continuations promptly when native code settles a promise outside a task (#54296) The browser process runs microtasks at kExplicit policy: Node.js performs a checkpoint when it leaves a libuv callback and MicrotasksRunner performs one after every task on the UI thread. gin_helper::Promise's SettleScope also tried to perform one when a promise was settled, but it did so while its own v8::MicrotasksScope was still on the stack, which makes PerformCheckpoint a no-op, so a promise settled from anywhere that is not a task never had its continuations run until some unrelated task came along. X11 replies are dispatched straight from the GLib source rather than from a task, so on Linux every clipboard.read()/readText()/has() call (which wait for a GetSelectionOwner reply) resolved only when the next timer or IPC message happened to wake the main loop: 4-30 s per call in an otherwise idle app. Post an empty task when a promise is settled on the UI thread outside a task so that MicrotasksRunner's checkpoint follows immediately, and add a testing hook that settles a promise from a native event source callback on each platform to cover it. (cherry picked from commit d8832a9)
…omise outside a task (42-x-y) (#54310) fix: run promise continuations promptly when native code settles a promise outside a task (#54296) The browser process runs microtasks at kExplicit policy: Node.js performs a checkpoint when it leaves a libuv callback and MicrotasksRunner performs one after every task on the UI thread. gin_helper::Promise's SettleScope also tried to perform one when a promise was settled, but it did so while its own v8::MicrotasksScope was still on the stack, which makes PerformCheckpoint a no-op, so a promise settled from anywhere that is not a task never had its continuations run until some unrelated task came along. X11 replies are dispatched straight from the GLib source rather than from a task, so on Linux every clipboard.read()/readText()/has() call (which wait for a GetSelectionOwner reply) resolved only when the next timer or IPC message happened to wake the main loop: 4-30 s per call in an otherwise idle app. Post an empty task when a promise is settled on the UI thread outside a task so that MicrotasksRunner's checkpoint follows immediately, and add a testing hook that settles a promise from a native event source callback on each platform to cover it. (cherry picked from commit d8832a9)
…omise outside a task (44-x-y) (#54308) fix: run promise continuations promptly when native code settles a promise outside a task (#54296) The browser process runs microtasks at kExplicit policy: Node.js performs a checkpoint when it leaves a libuv callback and MicrotasksRunner performs one after every task on the UI thread. gin_helper::Promise's SettleScope also tried to perform one when a promise was settled, but it did so while its own v8::MicrotasksScope was still on the stack, which makes PerformCheckpoint a no-op, so a promise settled from anywhere that is not a task never had its continuations run until some unrelated task came along. X11 replies are dispatched straight from the GLib source rather than from a task, so on Linux every clipboard.read()/readText()/has() call (which wait for a GetSelectionOwner reply) resolved only when the next timer or IPC message happened to wake the main loop: 4-30 s per call in an otherwise idle app. Post an empty task when a promise is settled on the UI thread outside a task so that MicrotasksRunner's checkpoint follows immediately, and add a testing hook that settles a promise from a native event source callback on each platform to cover it. (cherry picked from commit d8832a9)
Description of Change
Before: in the main process, a
gin_helper::Promisesettled from somewhere that is not a task (an X11 reply dispatched from the GLib source, a CFRunLoop timer, a Win32 message callback) left its continuations queued until some unrelated task ran.SettleScopecallsPerformCheckpoint()while its ownv8::MicrotasksScopeis still open, which V8 treats as a no-op. On Linux everyclipboard.read()/readText()/has()waits for aGetSelectionOwnerreply, so each call took 1 to 30 s to resolve in an otherwise idle app, andapi-clipboard.spec.tsran for 45 min on the ASan job in #54264.After: when a promise is settled on the UI thread outside a task,
SettleScopeposts an empty task, soMicrotasksRunner's checkpoint runs straight away. A bare app loopingclipboard.write()/read()/readText()/has()under Xvfb goes from ~16 s per iteration to ~4 ms.shell/common/gin_helper/promise.cc: the fix (and drops the dead checkpoint call)electron_common_testing.settlePromiseOutsideTask(): settles a promise from ag_idle/CFRunLoopTimer/SetTimercallback and then posts a task; the newnode.spec.tstest checks the continuation ran before that task, and fails without the fixChecklist
npm testpassesRelease Notes
Notes: Fixed
clipboard.read(),readText()andhas()resolving seconds late on Linux while the app was idle.