Repository navigation
perf: don't interrupt the uv loop for I/O changes made inside uv_run() - #52862
Merged
Merged
Conversation
MarshallOfSound
approved these changes
Aug 16, 2026
VerteDinde
approved these changes
Aug 16, 2026
MarshallOfSound
requested changes
Aug 16, 2026
MarshallOfSound
left a comment
Member
There was a problem hiding this comment.
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
MarshallOfSound
approved these changes
Aug 17, 2026
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
force-pushed
the
uv-loop-interrupt-in-run
branch
from
August 17, 2026 06:16
79f1b1e to
d8faf5f
Compare
|
Release Notes Persisted
|
Contributor
|
I was unable to backport this PR to "42-x-y" cleanly; |
Contributor
|
I have automatically backported this PR to "43-x-y", please check out #52905 |
Contributor
|
I have automatically backported this PR to "44-x-y", please check out #52906 |
Contributor
|
@codebytere has manually backported this PR to "42-x-y", please check out #52907 |
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>
6 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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-readsuv_backend_timeout()afteruv_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 auv_loop_interrupt_suspend()/uv_loop_interrupt_resume()pair (a depth in the loop's private fields, loop thread only) and hasUvRunOncewrap itsuv_run()in them, since that is the one place we know the embed thread is parked onembed_sem_.uv_async_send()is never suppressed, and auv_run()issued from anywhere else (a native module pumping the loop from a Chromium task, say) keeps interrupting as before. The patch'stest-embed.chad 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 sstracewindow:setTimeout/setImmediate/fs.readFile/ TCP round-trip latency unchanged;node-spec,api-app-spec,api-utility-process-specand the message loop parts ofchromium-specpass locallyChecklist
npm testpassesRelease Notes
Notes: Reduced idle main-process CPU wakeups caused by Node.js timers and immediates.