Skip to content

fix: run promise continuations promptly when native code settles a promise outside a task - #54296

Merged
MarshallOfSound merged 1 commit into
mainfrom
fix/settle-promise-outside-task
Sep 24, 2026
Merged

MarshallOfSound merged 1 commit into
mainfrom
fix/settle-promise-outside-task

Conversation

@MarshallOfSound

Copy link
Copy Markdown
Member

Description of Change

Before: in the main process, a gin_helper::Promise settled 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. SettleScope calls PerformCheckpoint() while its own v8::MicrotasksScope is still open, which V8 treats as a no-op. On Linux every clipboard.read() / readText() / has() waits for a GetSelectionOwner reply, so each call took 1 to 30 s to resolve in an otherwise idle app, and api-clipboard.spec.ts ran for 45 min on the ASan job in #54264.

After: when a promise is settled on the UI thread outside a task, SettleScope posts an empty task, so MicrotasksRunner's checkpoint runs straight away. A bare app looping clipboard.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 a g_idle / CFRunLoopTimer / SetTimer callback and then posts a task; the new node.spec.ts test checks the continuation ran before that task, and fails without the fix

Checklist

Release Notes

Notes: Fixed clipboard.read(), readText() and has() resolving seconds late on Linux while the app was idle.

…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.
@MarshallOfSound MarshallOfSound added semver/patch backwards-compatible bug fixes target/43-x-y PR should also be added to the "43-x-y" branch. target/44-x-y PR should also be added to the "44-x-y" branch. target/45-x-y PR should also be added to the "45-x-y" branch. labels Sep 24, 2026
@MarshallOfSound MarshallOfSound self-assigned this Sep 24, 2026
@MarshallOfSound MarshallOfSound added the target/42-x-y PR should also be added to the "42-x-y" branch. label Sep 24, 2026
@MarshallOfSound
MarshallOfSound merged commit d8832a9 into main Sep 24, 2026
106 checks passed
@release-clerk

release-clerk Bot commented Sep 24, 2026

Copy link
Copy Markdown

Release Notes Persisted

Fixed clipboard.read(), readText() and has() resolving seconds late on Linux while the app was idle.

@MarshallOfSound
MarshallOfSound deleted the fix/settle-promise-outside-task branch September 24, 2026 18:42
@trop

trop Bot commented Sep 24, 2026

Copy link
Copy Markdown
Contributor

I was unable to backport this PR to "44-x-y" cleanly;
you will need to perform this backport manually.

@trop

trop Bot commented Sep 24, 2026

Copy link
Copy Markdown
Contributor

I was unable to backport this PR to "43-x-y" cleanly;
you will need to perform this backport manually.

@trop trop Bot removed the target/44-x-y PR should also be added to the "44-x-y" branch. label Sep 24, 2026
@trop

trop Bot commented Sep 24, 2026

Copy link
Copy Markdown
Contributor

I was unable to backport this PR to "42-x-y" cleanly;
you will need to perform this backport manually.

@trop trop Bot added needs-manual-bp/44-x-y and removed target/43-x-y PR should also be added to the "43-x-y" branch. labels Sep 24, 2026
@trop trop Bot added needs-manual-bp/43-x-y needs-manual-bp/42-x-y and removed target/42-x-y PR should also be added to the "42-x-y" branch. labels Sep 24, 2026
@trop

trop Bot commented Sep 24, 2026

Copy link
Copy Markdown
Contributor

I have automatically backported this PR to "45-x-y", please check out #54307

@trop

trop Bot commented Sep 24, 2026

Copy link
Copy Markdown
Contributor

@MarshallOfSound has manually backported this PR to "44-x-y", please check out #54308

@trop

trop Bot commented Sep 24, 2026

Copy link
Copy Markdown
Contributor

@MarshallOfSound has manually backported this PR to "43-x-y", please check out #54309

@trop

trop Bot commented Sep 24, 2026

Copy link
Copy Markdown
Contributor

@MarshallOfSound has manually backported this PR to "42-x-y", please check out #54310

MarshallOfSound added a commit that referenced this pull request Sep 24, 2026
…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)
@trop trop Bot added merged/43-x-y PR was merged to the "43-x-y" branch. and removed in-flight/43-x-y labels Sep 24, 2026
MarshallOfSound added a commit that referenced this pull request Sep 24, 2026
…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)
@trop trop Bot added merged/42-x-y PR was merged to the "42-x-y" branch. and removed in-flight/42-x-y labels Sep 24, 2026
MarshallOfSound added a commit that referenced this pull request Sep 24, 2026
…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)
@trop trop Bot added merged/44-x-y PR was merged to the "44-x-y" branch. and removed in-flight/44-x-y labels Sep 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

46-x-y merged/42-x-y PR was merged to the "42-x-y" branch. merged/43-x-y PR was merged to the "43-x-y" branch. merged/44-x-y PR was merged to the "44-x-y" branch. merged/45-x-y PR was merged to the "45-x-y" branch. semver/patch backwards-compatible bug fixes

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

2 participants