fix(server): survive macOS setTypeOfService EINVAL #8389
main fromrefs/pull/8389/head·opened View on GitHubNode 24’s bundled Undici calls socket.setTypeOfService() on every HTTP/1 write to apply best-effort QoS. On macOS the kernel rejects that option on reused sockets with EINVAL, and because the call happens inside a socket event handler the throw is uncaught and kills the whole server process with exit 1.
On one nightly install (0.0.36-nightly, Node v24.15.0) this fired 52 times between Aug 17 and Aug 24. The desktop backend child restarts on crash, but the final burst on Aug 24 left the server down until the app was relaunched by hand.
node:net:683
throw new ErrnoException(err, 'setTypeOfService');
^
Error: setTypeOfService EINVAL
at Socket.setTypeOfService (node:net:683:13)
at writeH1 (node:internal/deps/undici/undici:7836:16)
at Object.write (node:internal/deps/undici/undici:7632:18)
at _resume (node:internal/deps/undici/undici:9460:54)
errno: -22, code: 'EINVAL', syscall: 'setTypeOfService'
The fix wraps Socket.prototype.setTypeOfService on darwin at the server entrypoint: an EINVAL from the QoS hint is swallowed (the request itself is still valid), and every other failure still propagates. Guard installation is darwin-only and no-ops if the method is absent.
Verified with vp test run apps/server/src/bin.test.ts (19 passed, including two new cases covering the swallow and the propagate paths), tsgo --noEmit on apps/server (exit 0), plus targeted lint and vp fmt --check.
Model: Claude Opus 5 (1M context). Harness: Claude Code.
Note
Medium Risk Runtime prototype patching on the Node net stack affects all outbound HTTP on macOS; EINVAL is narrowed but the change sits on a critical networking path.
Overview
Prevents the server process from crashing on macOS when Node 24’s Undici calls socket.setTypeOfService() during HTTP/1 writes and the kernel returns EINVAL on reused sockets.
At CLI startup (darwin only), the entrypoint patches Socket.prototype.setTypeOfService via a new guardSetTypeOfService helper that ignores EINVAL and still rethrows other errors (e.g. EPERM). Guard installation runs through installNodeNetworkCompatibility before Command.run.
Adds two unit tests in bin.test.ts for the swallow and propagate paths.
Note
Guard Socket.setTypeOfService against EINVAL on macOS#
- Adds
guardSetTypeOfServicein bin.ts, a higher-order wrapper that catchesEINVALerrors fromsetTypeOfServiceand returns the socket instead of throwing; non-EINVAL errors are rethrown - Adds
installNodeNetworkCompatibility, an Effect that mutatesNodeNet.Socket.prototype.setTypeOfServicewith the guard only ondarwinplatforms, then runs it beforeCommand.runin the module entrypoint - Adds tests in bin.test.ts covering both the EINVAL-swallowing and non-EINVAL-rethrow paths
- Behavioral Change: on macOS,
Socket.prototype.setTypeOfServiceis monkey-patched at startup; EINVAL errors that previously propagated are now silently ignored
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
Approvability#
Verdict: Not approved
Macroscope's review found this PR not approvable — The PR changes production networking behavior on macOS by monkey-patching Node socket behavior globally. It also adds a new file-level static-analysis suppression directive, so the change warrants human review.
Notes:
- Diff unchanged. Approvability was decided on eligibility alone.
I confirmed this crash on T3 Code
0.0.39-nightly.20260902.1257on Apple Silicon macOS.Runtime details:
- macOS
27.0build26A5425a - Architecture
arm64 - Electron
43.4.1 - Node
24.18.1 - Bundled Undici
7.29.0 - Backend launch flag
ELECTRON_RUN_AS_NODE=1
server-child.logshowed the exact uncaughtsetTypeOfService EINVALstack. The backend exited with code1and restarted about once per minute. The desktop parent remained alive.I cherry-picked commit
9a89b52ffonto currentmain. The cherry-pick needed one import conflict resolution inapps/server/src/bin.test.ts.Verification results:
vp test run apps/server/src/bin.test.ts: 19 of 19 tests passed.- The server typecheck passed.
- Targeted lint passed for
bin.tsandbin.test.ts. - An unsigned arm64 package built as
0.0.39-pr.8389.
I ran the packaged backend against an isolated snapshot of the same database and settings. The patched backend remained stable for one hour. The unpatched nightly crashed about once per minute. No
setTypeOfService EINVALerrors appeared after the patch.This PR fixes the crash in my environment.
- macOS
juliusmarminge added labelsmacroscope-review
Still crashing on
0.0.45-nightly.20261002.2572(Node v24.21.0, macOS 27.0.1 GA): 41 backend deaths in 2 days, samesetTypeOfService EINVALstack from undiciwriteH1. Details in https://github.com/pingdotgg/t3code/issues/9264#issuecomment-5953582731.Could this be rebased onto main? It now conflicts. It might also be worth adding
Closes #9264so the PR and the issue are linked.Still crashing on the latest nightly,
0.0.46-nightly.20261005.2667(Electron 44.4.2, Node 24.21.0, undici 7.29.1), macOS 27.0.1.114 backend deaths since 2026-09-25, 14 of them today, all with the same
setTypeOfService EINVALfrom undiciwriteH1reported in #9264. Each one kills every running Claude turn mid-task. When the session resumes, Claude Code puts a synthetic "No response requested." into the transcript, so the agent thinks it stopped by itself and tells the user so.@MaWoo96 could you rebase this onto main? And could a maintainer vouch for it so it gets a review? It's the only fix for #9264 so far.
Note
Written by
claude-opus-5-5on behalf of MariaHi! We are cleaning up open PRs, and this one appears to have been created with an older model (
opus 5). If this change is really important, we recommend rebuilding the PR with a newer model if possible.maria-rcks closed this
Sign in to join this conversation.
Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID:
dde3bcd1-bae2-4013-b274-074ef7e33c50You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
Comment `@coderabbitai help` to get the list of available commands.