Skip to content

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

Merged
codebytere merged 4 commits into
43-x-yfrom
trop/43-x-y-bp-perf-don-t-interrupt-the-uv-loop-for-i-o-changes-made-inside-uv_run--1786969331677
Aug 17, 2026
Merged

codebytere merged 4 commits into
43-x-yfrom
trop/43-x-y-bp-perf-don-t-interrupt-the-uv-loop-for-i-o-changes-made-inside-uv_run--1786969331677

Conversation

@trop

@trop trop Bot commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

Backport of #52862

See that PR for details.

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

trop Bot and others added 4 commits August 17, 2026 12:22
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.

Co-authored-by: Shelley Vohr <[email protected]>
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

Co-authored-by: Shelley Vohr <[email protected]>
…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_.

Co-authored-by: Shelley Vohr <[email protected]>
Co-authored-by: PatchUp <73610968+patchup[bot]@users.noreply.github.com>
@trop
trop Bot requested a review from a team as a code owner August 17, 2026 12:22
@trop
trop Bot requested a review from codebytere August 17, 2026 12:22
@trop trop Bot added 43-x-y backport This is a backport PR semver/patch backwards-compatible bug fixes labels Aug 17, 2026
@codebytere
codebytere enabled auto-merge (squash) August 17, 2026 13:18
@codebytere
codebytere merged commit 077ace5 into 43-x-y Aug 17, 2026
151 of 153 checks passed
@codebytere
codebytere deleted the trop/43-x-y-bp-perf-don-t-interrupt-the-uv-loop-for-i-o-changes-made-inside-uv_run--1786969331677 branch August 17, 2026 17:32
@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

43-x-y backport This is a backport PR semver/patch backwards-compatible bug fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant