Skip to content

fix(ext/node): drain all ready connections in poll_tcp_handle to prevent accept starvation - #32943

Merged
bartlomieju merged 2 commits into
mainfrom
fix/node-net-accept-starvation
Mar 24, 2026
Merged

bartlomieju merged 2 commits into
mainfrom
fix/node-net-accept-starvation

Conversation

@bartlomieju

Copy link
Copy Markdown
Member

Summary

  • Fix node:net accept starvation under high concurrency when mixed with Deno-native APIs (Deno.listenTls(), Deno.connect())
  • poll_tcp_handle now drains all ready connections from poll_accept in a loop before firing callbacks, instead of accepting only one at a time
  • Fires the connection callback once per accepted stream (matching libuv semantics), with a safety check that stops if the callback doesn't call uv_accept

Root cause

Under high concurrent load, poll_tcp_handle previously accepted one connection, fired the JS callback, and moved on. The callback's async work (e.g., Deno.listenTls) created pending tokio ops processed in Phase 2 of the event loop, delaying the next Phase 4 (I/O) iteration. Since tokio's reactor only re-polls epoll when the event loop returns Pending, connections that arrived in the same epoll batch were invisible until the next reactor turn — creating a feedback loop where accept throughput degraded to zero.

Closes #32941

Test plan

  • New test tcp_batch_accept_concurrent — opens 8 concurrent connections before ticking the event loop, verifies all 8 are accepted in a single tick
  • All 59 uv_compat tests pass (58 existing + 1 new)
  • ./x fmt and ./x lint pass

🤖 Generated with Claude Code

…ent accept starvation

Previously, poll_tcp_handle accepted only one connection per call, then
fired the JS callback. Under high concurrency with mixed node:net and
Deno-native I/O, tokio's reactor only re-polls epoll when the event
loop returns Pending. Connections that arrived in the same epoll batch
were invisible until the next reactor turn, but Phase 2 (pending ops
from Deno.listenTls/connect) kept the loop busy, delaying Phase 4
(I/O) and creating a feedback loop where accept throughput degraded
to zero.

The fix drains all ready connections from poll_accept in a loop before
firing callbacks. Each accepted stream goes into internal_backlog, then
the connection callback fires once per entry (matching libuv semantics).
If the callback doesn't call uv_accept (backlog doesn't shrink), we
stop to avoid spinning.

Closes #32941

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Fixes node:net accept starvation in uv_compat under high concurrency by changing poll_tcp_handle to drain all ready accepts in one poll and then invoke the connection callback once per accepted stream (with a guard to stop if the callback doesn’t consume the backlog).

Changes:

  • Update poll_tcp_handle to batch/drain poll_accept results into an internal backlog before invoking callbacks.
  • Invoke the connection callback once per backlog entry, stopping when the backlog no longer shrinks (ie. uv_accept wasn’t called).
  • Add a regression test asserting multiple pre-tick connections are accepted in a single tick.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
libs/core/uv_compat/tcp.rs Batch-accept loop and per-connection callback firing logic to avoid accept starvation.
libs/core/uv_compat/tests.rs New regression test for batch-accept behavior under multiple pending connections.

Comment thread libs/core/uv_compat/tests.rs Resolved
Comment thread libs/core/uv_compat/tests.rs Resolved
Comment thread libs/core/uv_compat/tcp.rs Resolved
- Fix use-after-free: heap-allocate accepted client handles so they
  survive until uv_close processes them in the close phase
- Set any_work = true when connection callbacks consume backlog entries,
  ensuring run_io() correctly reports work for pre-existing backlogs
- Rename test to tcp_batch_accept (sequential connects, not concurrent)
- Fix clippy: use MaybeUninit::uninit() instead of zeroed().assume_init()
  to avoid UB from zeroing NonNull fields in uv_tcp_t

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@bartlomieju
bartlomieju merged commit bad6eae into main Mar 24, 2026
111 checks passed
@bartlomieju
bartlomieju deleted the fix/node-net-accept-starvation branch March 24, 2026 12:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

node:net listen socket gets cross-registered in both tokio and libuv epoll, causing accept stall

3 participants