Skip to content

perf: don't interrupt the uv loop for I/O changes made inside uv_run() - #52862

Merged
codebytere merged 4 commits into
mainfrom
uv-loop-interrupt-in-run
Aug 17, 2026
Merged

codebytere merged 4 commits into
mainfrom
uv-loop-interrupt-in-run

Conversation

@codebytere

@codebytere codebytere commented Aug 16, 2026 •

Copy link
Copy Markdown
Member

Description of Change

Electron drives Node's libuv loop from two threads: the UI thread runs uv_run(UV_RUN_NOWAIT), and an embed thread polls the loop's backend fd and wakes the UI thread when something is ready. Our libuv patch (feat_add_uv_loop_interrupt_on_io_change_option_to_uv_loop_configure.patch, from libuv/libuv#3308) interrupts that poll whenever a handle or watcher is started, so a timer started from a Chromium task gets noticed even when the embed thread is already asleep on an older timeout.

The interrupt also fired for handles started while uv_run() itself was executing, which Node does on almost every tick for its immediate and timer handles. No thread is polling at that point, and the embed thread re-reads uv_backend_timeout() after uv_run() returns anyway, so each of those interrupts was a wasted syscall on the UI thread plus a spurious wakeup that scheduled an extra, empty loop run. This regenerates the patch with a uv_loop_interrupt_suspend() / uv_loop_interrupt_resume() pair (a depth in the loop's private fields, loop thread only) and has UvRunOnce wrap its uv_run() in them, since that is the one place we know the embed thread is parked on embed_sem_. uv_async_send() is never suppressed, and a uv_run() issued from anywhere else (a native module pumping the loop from a Chromium task, say) keeps interrupting as before. The patch's test-embed.c had also stopped compiling against current libuv; that is fixed, with a new case where a timer started while interrupts are suspended must still fire on time without an interrupt being written.

Measured on Linux with a testing build and an idle app running setInterval(() => {}, 10) in the main process, over a 5 s strace window:

  • main-thread eventfd writes: 468 before, 0 after
  • embed-thread wakeups of the UI thread: 913 before, 459 after (one loop run per tick instead of two)
  • timer/immediate ordering and setTimeout / setImmediate / fs.readFile / TCP round-trip latency unchanged; node-spec, api-app-spec, api-utility-process-spec and the message loop parts of chromium-spec pass locally
  • macOS and Windows go through the same gate; relying on CI there

Checklist

Release Notes

Notes: Reduced idle main-process CPU wakeups caused by Node.js timers and immediates.

@codebytere codebytere added semver/patch backwards-compatible bug fixes target/42-x-y PR should also be added to the "42-x-y" branch. target/43-x-y PR should also be added to the "43-x-y" branch. labels Aug 16, 2026
@codebytere
codebytere requested a review from a team as a code owner August 16, 2026 14:34
@codebytere codebytere added the target/44-x-y PR should also be added to the "44-x-y" branch. label Aug 16, 2026
@electron-cation electron-cation Bot added the new-pr 🌱 PR opened recently label Aug 16, 2026
@codebytere
codebytere requested a review from deepak1556 August 16, 2026 14:35

@MarshallOfSound MarshallOfSound left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On re-reading I think this might stall if a native node module calls uv_run itself at the wrong time (which is allowed but weird)

Workshopping a way to keep the win rn

codebytere and others added 4 commits August 17, 2026 06:15
Electron configures Node's libuv loop with UV_LOOP_INTERRUPT_ON_IO_CHANGE
so that starting a timer/handle/watcher from a Chromium task wakes the
embed thread, which may be blocked polling the backend fd with a stale
timeout. The interrupt also fired for changes made while uv_run() itself
was executing (e.g. Node toggling its immediate idle handle or timer
handle on most ticks inside UvRunOnce), where nobody is polling: each of
those cost a write()/kevent()/PostQueuedCompletionStatus on the main
thread and made the embed thread's next poll return immediately,
scheduling an extra empty UvRunOnce task per tick.

Track uv_run() nesting in the loop's private internal fields and skip
the I/O-change interrupt while it is non-zero. Once uv_run() returns the
embedder re-reads uv_backend_timeout(), which already accounts for the
change. uv_async_send() is unaffected. On Linux an idle app with a 10 ms
setInterval goes from ~94 eventfd writes/s on the main thread and two
UI-thread wakeups per tick to zero writes and one wakeup per tick, with
identical timer/immediate ordering and latency.

Also repairs the patch's test-embed.c (it no longer compiled against the
current MAKE_VALGRIND_HAPPY signature) and adds a case that starts a
timer from inside uv_run(UV_RUN_NOWAIT), asserting the external poller
still fires it on time and, on Linux, that no interrupt was written.
libuv/libuv#3308 is closed, so the sentence suggesting the gated variant
be proposed there no longer applies. Description-only change.

No-Verification-Needed: patch description text only
…nside uv_run()

Keying the gate on "uv_run() is on the stack" also covered a foreign
nested uv_run() issued from a Chromium task (a native module pumping the
loop), where the embed thread is polling with a stale timeout and does
need the wakeup. Replace the in_run counter with an embedder-controlled
uv_loop_interrupt_suspend()/resume() depth and wrap only UvRunOnce's
uv_run() in it, since that is where the embed thread is known to be
parked on embed_sem_.
@codebytere
codebytere force-pushed the uv-loop-interrupt-in-run branch from 79f1b1e to d8faf5f Compare August 17, 2026 06:16
@codebytere codebytere removed the new-pr 🌱 PR opened recently label Aug 17, 2026
@electron-cation electron-cation Bot added the new-pr 🌱 PR opened recently label Aug 17, 2026
@codebytere
codebytere merged commit 0ff4f64 into main Aug 17, 2026
81 checks passed
@codebytere
codebytere deleted the uv-loop-interrupt-in-run branch August 17, 2026 12:22
@release-clerk

release-clerk Bot commented Aug 17, 2026

Copy link
Copy Markdown

Release Notes Persisted

Reduced idle main-process CPU wakeups caused by Node.js timers and immediates.

@codebytere codebytere removed the new-pr 🌱 PR opened recently label Aug 17, 2026
@trop

trop Bot commented Aug 17, 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/42-x-y and removed target/42-x-y PR should also be added to the "42-x-y" branch. labels Aug 17, 2026
@trop

trop Bot commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

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

@trop trop Bot added in-flight/43-x-y and removed target/43-x-y PR should also be added to the "43-x-y" branch. labels Aug 17, 2026
@trop

trop Bot commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

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

@trop trop Bot added in-flight/44-x-y and removed target/44-x-y PR should also be added to the "44-x-y" branch. labels Aug 17, 2026
@trop

trop Bot commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

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

@trop trop Bot added in-flight/42-x-y merged/44-x-y PR was merged to the "44-x-y" branch. and removed needs-manual-bp/42-x-y in-flight/44-x-y labels Aug 17, 2026
codebytere added a commit that referenced this pull request Aug 17, 2026
#52907)

perf: don't interrupt the uv loop for I/O changes made inside uv_run() (#52862)

* perf: don't interrupt the uv loop for I/O changes made inside uv_run()

Electron configures Node's libuv loop with UV_LOOP_INTERRUPT_ON_IO_CHANGE
so that starting a timer/handle/watcher from a Chromium task wakes the
embed thread, which may be blocked polling the backend fd with a stale
timeout. The interrupt also fired for changes made while uv_run() itself
was executing (e.g. Node toggling its immediate idle handle or timer
handle on most ticks inside UvRunOnce), where nobody is polling: each of
those cost a write()/kevent()/PostQueuedCompletionStatus on the main
thread and made the embed thread's next poll return immediately,
scheduling an extra empty UvRunOnce task per tick.

Track uv_run() nesting in the loop's private internal fields and skip
the I/O-change interrupt while it is non-zero. Once uv_run() returns the
embedder re-reads uv_backend_timeout(), which already accounts for the
change. uv_async_send() is unaffected. On Linux an idle app with a 10 ms
setInterval goes from ~94 eventfd writes/s on the main thread and two
UI-thread wakeups per tick to zero writes and one wakeup per tick, with
identical timer/immediate ordering and latency.

Also repairs the patch's test-embed.c (it no longer compiled against the
current MAKE_VALGRIND_HAPPY signature) and adds a case that starts a
timer from inside uv_run(UV_RUN_NOWAIT), asserting the external poller
still fires it on time and, on Linux, that no interrupt was written.

* chore: drop upstreaming note from patch description

libuv/libuv#3308 is closed, so the sentence suggesting the gated variant
be proposed there no longer applies. Description-only change.

No-Verification-Needed: patch description text only

* refactor: suspend the io-change interrupt from UvRunOnce instead of inside uv_run()

Keying the gate on "uv_run() is on the stack" also covered a foreign
nested uv_run() issued from a Chromium task (a native module pumping the
loop), where the embed thread is polling with a stale timeout and does
need the wakeup. Replace the in_run counter with an embedder-controlled
uv_loop_interrupt_suspend()/resume() depth and wrap only UvRunOnce's
uv_run() in it, since that is where the embed thread is known to be
parked on embed_sem_.

* chore: update patches

---------


(cherry picked from commit 0ff4f64)

Co-authored-by: PatchUp <73610968+patchup[bot]@users.noreply.github.com>
@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 Aug 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

45-x-y in-flight/42-x-y 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. semver/patch backwards-compatible bug fixes

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

3 participants