Skip to content

fix(relay): treat EPIPE/ECONNRESET write failures as the client leaving - #24209

Merged
OrcaWin merged 1 commit into
mainfrom
OrcaWin/node-rt-relay-epipe
Oct 1, 2026
Merged

OrcaWin merged 1 commit into
mainfrom
OrcaWin/node-rt-relay-epipe

Conversation

@OrcaWin

@OrcaWin OrcaWin commented Sep 30, 2026 •

Copy link
Copy Markdown
Collaborator
Files Added Deleted Net
Test 2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​154 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​154
Prod 2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​32 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​32

ELI5

When an SSH client's connection breaks, the relay should treat it as the client leaving, so the client can quickly reclaim its terminal. A failed write with a broken pipe or a connection reset was instead recorded as a local close. That held the terminal for 30 seconds instead of 250ms, so a quick reconnect was refused.

What Changed

  • New relay-socket-peer-close.ts: counts a close as peer-caused only when the write error, or socket.errored, is EPIPE or ECONNRESET. libuv maps the Windows named-pipe codes to these.
  • Kept local on purpose: ECANCELED, ECONNABORTED, and a bare ERR_STREAM_DESTROYED after our own destroy(). We never shorten the grace for a teardown we caused.
  • relay-reconnect-listener.ts: the socket write now detaches the client as peer-closed before settling, both when the write fails and when it writes to an already-errored socket.
  • No wire change; this is local close-cause classification only.

Why

This is a follow-up from the Windows SSH-host lanes (#24180). On arm64, the write error arrived before end-of-file.

Linked Issue

Part of the Node runtime plan (Phase 2). Stacked on #24180.

Visual Proof

N/A. There's no user interface change.

Testing

  • A real dispatcher over a Unix socket covers a kernel EPIPE, a reset before close, and our own destroy, which must stay local. Both peer cases fail without the fix.

  • The classifier has unit tests.

  • Not tested: a real Windows named pipe (these tests skip on win32).

  • Out of scope: the relay's stdout client still closes as local.

  • I manually tested these changes locally

  • Automated tests added/updated, or explained why not below

Agent skill upstream boundary

  • Not applicable, or this change follows docs/reference/agent-skill-sharing-upstream-boundary.md and copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

@greptile-apps

greptile-apps Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

RetriggerConfidence Score: 5/5

[Medium risk] Changes how the relay handles client disconnections.

The PR appears safe to merge, though the Windows named-pipe write path remains without integration coverage.

Findings

  1. P2 Windows write path remains untested ▶

Summary

The PR classifies EPIPE and ECONNRESET socket write failures as peer disconnects so a reconnecting client does not wait through the full PTY owner grace period. It adds classifier and Unix-socket integration tests; the previously noted Windows named-pipe integration gap remains.

Diagram
%%{init: {'theme': 'neutral'}}%%
flowchart LR
  A[Relay socket write] --> B{EPIPE or ECONNRESET?}
  B -->|Yes| C[Detach as peer-closed]
  B -->|No| D[Settle write normally]
  C --> E[Settle failed write]
Loading

Reviews (3) · Last reviewed commit: "fix(relay): detach a socket client as pe..."


// Why: on Windows named pipes a relay write can fail with EPIPE/ECONNRESET before EOF is read. The
// writer then closed the client as 'local', holding its PTY owner for the full grace.
describe.skipIf(process.platform === 'win32')('reconnect listener peer write error', () => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Windows write path remains untested

This integration suite skips Windows, although Windows named-pipe write failures are the case this change aims to fix. The classifier unit tests cannot check how a named-pipe write callback and socket close are ordered, so a regression on the target platform could pass. A Windows named-pipe integration case would close that coverage gap.

Knowledge Base Used: Relay protocol and transport

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

@pullfrog pullfrog Bot 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.

Important

On Windows a named-pipe write that libuv accepts and that fails at IOCP completion reaches the callback as err.code === 'EOF' (ERROR_BROKEN_PIPE → UV_EOF), which this classifier does not treat as peer departure. The client then still closes as 'local' in the very interleaving this PR targets. Evidence and a suggested direction are in the inline comment.

Reviewed changes

  • relay-socket-peer-close.ts (new): isRelaySocketPeerClosed reports peer departure only for EPIPE/ECONNRESET on the write error or socket.errored, deliberately excluding teardown the relay causes (ECANCELED, ECONNABORTED, a bare ERR_STREAM_DESTROYED).
  • relay-reconnect-listener.ts: a peer-caused write failure now detaches the socket client as peer-closed before the write settles, so the 250ms grace floor applies instead of the 30s hold.
  • Tests: new classifier unit tests, a real-dispatcher write-error test over a Unix socket (EPIPE, reset-before-close, own-destroy stays local), and the peer-end test rebuilt on a real dispatcher rather than a cast stub.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

// ERROR_BAD_PIPE) into EPIPE and ERROR_NETNAME_DELETED/WSAECONNRESET into ECONNRESET, and both
// mean the other end went away. ECANCELED and ECONNABORTED can come from our own close, so a
// teardown we started would clamp a live owner's grace (see pty-consumer-owner-admission.ts).
const PEER_CLOSED_SOCKET_ERROR_CODES: ReadonlySet<string> = new Set(['EPIPE', 'ECONNRESET'])

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.

On Windows, a named-pipe write that libuv accepts and that later fails at IOCP completion reaches the write callback as err.code === 'EOF' (ERROR_BROKEN_PIPE → UV_EOF); the analogous ERROR_NO_DATA completion is 'EAGAIN'. Neither is in this set, so the detach-first path is skipped and the client still closes as 'local' in exactly the interleaving this PR targets — and the tests, which all skip win32, cannot see it.

Technical details
# Windows async pipe-write completions are not EPIPE

## Affected sites
- `src/relay/relay-socket-peer-close.ts:7` — the accepted set omits `EOF`/`EAGAIN`
- `src/relay/relay-reconnect-listener.ts:103-108` — the write callback skips `detachSocketClient` for those codes, so `onSettled({ok:false})` closes the client as `local`

## Evidence (verified against libuv v1.x and Node 24's bundled libuv 1.52.1)
- Sync `uv_write` path (`src/win/stream.c` uses `uv_translate_write_sys_error`): `ERROR_BROKEN_PIPE`/`ERROR_NO_DATA` → `UV_EPIPE`. This is the case the comment describes, and it is why the manual test likely passed.
- Async IOCP completion (`src/win/pipe.c`, `uv__process_pipe_write_req`) uses `uv_translate_sys_error(GET_REQ_ERROR(req))`, and `GET_REQ_ERROR` = `pRtlNtStatusToDosError(status)`. `STATUS_PIPE_BROKEN` → `ERROR_BROKEN_PIPE` (109) → `UV_EOF`; a status converted to `ERROR_NO_DATA` → `UV_EAGAIN`.
- `uv_err_name(UV_EOF)` is `"EOF"`, so Node's write callback receives `err.code === 'EOF'`. libuv maintainers note both outcomes are nondeterministic (libuv#4548).

## Required outcome
- A queued-then-failed Windows pipe write must still be classified as peer departure, so `detachClient(..., 'peer-closed')` runs before the writer's `'local'` close.

## Suggested approach
- Add `'EOF'` (unambiguous here: a write-completion EOF means the pipe broke). `'EAGAIN'` is the mis-mapped `ERROR_NO_DATA` completion but also signals would-block, so treat it separately; `socket.readableEnded` is another peer-departure signal to consider.
- Note the two peer tests are `describe.skipIf(... === 'win32')`, so this path is never exercised on the platform it targets.

## Open questions for the human
- Was the Windows manual test on a connection with a pending write (backpressure), or only the synchronous hard-fail path?

@pullfrog

pullfrog Bot commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

Pullfrog couldn't load your Pullfrog secrets on this run. The key is still stored — the runner just couldn't fetch it, so this is a transient failure on our side, not a missing key.

To fix: re-run the job. If it keeps happening, tell us in Discord.

Model settings → · Setup docs → · Ask in Discord →

Pullfrog  | Rerun failed job ➔ | View workflow run | via Pullfrog | 𝕏

…with EPIPE or ECONNRESET

On the Windows arm64 lane a relay write to a named-pipe client could fail with EPIPE or ECONNRESET
before EOF arrived. The dispatcher's writer then closed the client as 'local', so its PTY owner kept
the full 30s grace instead of the 250ms peer-closed floor and a quick reconnect was refused.

The reconnect listener now detaches the client as peer-closed before settling a write that failed
with EPIPE/ECONNRESET, or a write attempted after a peer reset destroyed the socket (so
ERR_STREAM_DESTROYED counts only via socket.errored). ECANCELED, ECONNABORTED and a plain
destroy() stay 'local', since our own teardown can raise them.
@OrcaWin
OrcaWin force-pushed the OrcaWin/node-rt-relay-epipe branch from 3ff605b to 234cc8c Compare October 1, 2026 11:05
@OrcaWin
OrcaWin requested a review from Jinwoo-H as a code owner October 1, 2026 11:05
@OrcaWin
OrcaWin changed the base branch from OrcaWin/node-rt-p2-winhost to main October 1, 2026 11:07
@OrcaWin OrcaWin closed this Oct 1, 2026
@OrcaWin OrcaWin reopened this Oct 1, 2026
@OrcaWin
OrcaWin merged commit 2e3bd20 into main Oct 1, 2026
64 of 92 checks passed
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.

1 participant