Skip to content

node:net: Don't start reading a paused socket when it closes - #8502

Merged
gabivlj merged 1 commit into
mainfrom
gv/net-paused-close-read
Nov 18, 2026
Merged

gabivlj merged 1 commit into
mainfrom
gv/net-paused-close-read

Conversation

@gabivlj

@gabivlj gabivlj commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

When a socket's connection closes while nobody is reading it, because it is paused or stopped on backpressure, onConnectionClosed() ends the readable and calls read(0) so that 'end' fires, as in Node. That read(0) went through the socket's read() override, which starts the read loop whenever kBuffer is set, and workerd always sets it. So a paused socket started reading as soon as its connection closed.

startRead() already calls the Readable's own read(0) at EOF for this reason. onConnectionClosed() now does the same.

A read of a tunnel the service failed currently ends in EOF, so the stray read went unnoticed. Once capnproto's CONNECT adapter fails such reads with the service's error, a paused socket whose peer resets the connection is destroyed with "read ECONNRESET" from that read, rather than with "write ECONNRESET" from the write that observes the reset, as in Node. testNetConnectionResetWrite@all-autogates fails that way against that capnproto change, and passes with this one.

How did I get here
What the test does: it connects to a sidecar server that resets the connection, with allowHalfOpen: true. It calls socket.pause() before the connection opens, so nothing should ever read. Then it writes 'x' every 10 ms until an 'error' arrives, and expects that error to be Error: write ECONNRESET with syscall === 'write'.

How it breaks with the capnproto capnproto/capnproto#2931 change:

  1. The peer resets the connection, so the network service's connect() fails with "Connection reset by peer". kj's adapter then calls handleException.
  2. handleException now calls abortRead(e) and abortWrite(e) on the service's end of the tunnel.
  • The abortRead makes the Worker side's whenWriteDisconnected() resolve, and that's what resolves socket.closed.
  • This step isn't new: before, dropping the service's end did the same.
  1. In internal_net.ts, closed resolving calls onConnectionClosed(). The socket is paused and has no read loop running, so it calls push(null) and then this.read(0).
  2. this.read(0) goes through the socket's read() override. That override starts the read loop whenever kBuffer is set, and workerd always sets it. So the paused socket starts reading. I confirmed this with a stack trace: tryReadStart ← Socket.read ← onConnectionClosed ← closed.then.
  3. Where the two capnp versions differ:
  • Pinned capnp: that read gets EOF, so nothing visible happens. The next write fails with write ECONNRESET, and the test passes.
  • New capnp: that read fails with the reset (that's the abortWrite(e)). The read loop's catch destroys the socket with read ECONNRESET. That error arrives before any write's, so the test sees Error: read ECONNRESET instead of Error: write ECONNRESET and fails.

When a socket's connection closes while nobody is reading it, because
it is paused or stopped on backpressure, onConnectionClosed() ends the
readable and calls read(0) so that 'end' fires, as in Node. That
read(0) went through the socket's read() override, which starts the
read loop whenever kBuffer is set, and workerd always sets it. So a
paused socket started reading as soon as its connection closed.

startRead() already calls the Readable's own read(0) at EOF for this
reason. onConnectionClosed() now does the same.

A read of a tunnel the service failed currently ends in EOF, so the
stray read went unnoticed. Once capnproto's CONNECT adapter fails such
reads with the service's error, a paused socket whose peer resets the
connection is destroyed with "read ECONNRESET" from that read, rather
than with "write ECONNRESET" from the write that observes the reset,
as in Node. testNetConnectionResetWrite@all-autogates fails that way
against that capnproto change, and passes with this one.
@gabivlj
gabivlj requested review from a team as code owners November 16, 2026 22:24

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Approved — The one-file change is small and consistent with the existing startRead() EOF handling, and no issues were found.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

📋 Code reviewer: ✅ LGTM

No issues found.


Reviewed commit: af0211e4

@gabivlj
gabivlj merged commit 6ad4ed8 into main Nov 18, 2026
24 checks passed
@gabivlj
gabivlj deleted the gv/net-paused-close-read branch November 18, 2026 19:11
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.

2 participants