The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 23:15 UTC
fix(server): a macOS setTypeOfService EINVAL no longer kills the backend (#20414)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>
Victor Vogel committed
Oct 20, 2026 at 00:06 UTC
6ae65e129cb918e00e7b91afe794c82542bb2458
3 files changed
+168
apps/server/src/bin.ts
+3
@@ -14,6 +14,9 @@ if (
14
})
15
) {
16
const command = process.argv[2];
17
+ // Guard sockets before either dispatch path can load outbound-capable modules.
18
+ const { ignoreMacosTypeOfServiceEinval } = await import("./outboundHttp.ts");
19
+ ignoreMacosTypeOfServiceEinval();
20
if (command === "acp-mcp-bridge" || command === "acp-mcp-call") {
21
const { runAcpMcpCliFastPath } = await import("./mcp/AcpMcpStdioBridge.ts");
22
await runAcpMcpCliFastPath(command, process.argv.slice(3));
apps/server/src/outboundHttp.test.ts
+140
@@ -2,9 +2,13 @@
2
import * as NodeHttp2 from "node:http2";
3
import * as NodeNet from "node:net";
4
import * as NodeTls from "node:tls";
5
+import * as NodeURL from "node:url";
6
7
+import * as NodeServices from "@effect/platform-node/NodeServices";
8
import { it } from "@effect/vitest";
9
import * as Effect from "effect/Effect";
10
+import * as Stream from "effect/Stream";
11
+import { ChildProcess, ChildProcessSpawner } from "effect/process";
12
import { FetchHttpClient, HttpClient } from "effect/http";
13
import * as Undici from "undici";
14
import { afterAll, afterEach, beforeAll, describe, expect } from "vite-plus/test";
@@ -12,6 +16,7 @@ import { afterAll, afterEach, beforeAll, describe, expect } from "vite-plus/test
16
import {
17
builtinFetchUsesHttp2,
18
extendConnectionAttemptTimeout,
19
+ ignoreMacosTypeOfServiceEinval,
20
installHttp1FetchDispatcher,
21
} from "./outboundHttp.ts";
22
@@ -97,6 +102,141 @@ describe("installHttp1FetchDispatcher", () => {
102
);
103
});
104
105
+const runNode = Effect.fnUntraced(function* (args: Array<string>) {
106
+ const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
107
+ const child = yield* spawner.spawn(ChildProcess.make(process.execPath, args));
108
+ return yield* Effect.all(
109
+ {
110
+ stdout: child.stdout.pipe(Stream.decodeText(), Stream.mkString),
111
+ stderr: child.stderr.pipe(Stream.decodeText(), Stream.mkString),
112
+ exitCode: child.exitCode.pipe(Effect.map(Number)),
113
+ },
114
+ { concurrency: "unbounded" },
115
+ );
116
+}, Effect.scoped);
117
+
118
+describe("ignoreMacosTypeOfServiceEinval", () => {
119
+ const errnoError = (code: string) =>
120
+ Object.assign(new Error(`setTypeOfService ${code}`), { code, syscall: "setTypeOfService" });
121
+
122
+ it("swallows only an Error with code EINVAL on macOS", () => {
123
+ let error: unknown = errnoError("EINVAL");
124
+ const prototype = {
125
+ setTypeOfService(this: NodeNet.Socket, _typeOfService: number): NodeNet.Socket {
126
+ throw error;
127
+ },
128
+ };
129
+ ignoreMacosTypeOfServiceEinval("darwin", prototype);
130
+
131
+ const socket = new NodeNet.Socket();
132
+ expect(prototype.setTypeOfService.call(socket, 0)).toBe(socket);
133
+
134
+ error = errnoError("EPERM");
135
+ expect(() => prototype.setTypeOfService.call(socket, 0)).toThrow(error);
136
+
137
+ error = { code: "EINVAL" };
138
+ expect(() => prototype.setTypeOfService.call(socket, 0)).toThrow();
139
+ });
140
+
141
+ it("changes nothing off macOS or on a runtime without setTypeOfService", () => {
142
+ const error = errnoError("EINVAL");
143
+ const prototype = {
144
+ setTypeOfService() {
145
+ throw error;
146
+ },
147
+ };
148
+ const original = prototype.setTypeOfService;
149
+ ignoreMacosTypeOfServiceEinval("linux", prototype);
150
+ ignoreMacosTypeOfServiceEinval("win32", prototype);
151
+ expect(prototype.setTypeOfService).toBe(original);
152
+ expect(() => prototype.setTypeOfService()).toThrow(error);
153
+
154
+ const withoutTypeOfService = {};
155
+ ignoreMacosTypeOfServiceEinval("darwin", withoutTypeOfService);
156
+ expect(withoutTypeOfService).not.toHaveProperty("setTypeOfService");
157
+ });
158
+
159
+ // Bundled undici 7 calls this API from a socket callback. Keep the real fetch
160
+ // in a child so an escaped EINVAL exits that process instead of breaking Vitest.
161
+ it.effect.runIf(
162
+ typeof (NodeNet.Socket.prototype as { setTypeOfService?: unknown }).setTypeOfService ===
163
+ "function" && process.versions.undici?.startsWith("7.") === true,
164
+ )("keeps built-in fetch working when setTypeOfService throws EINVAL", () =>
165
+ Effect.gen(function* () {
166
+ const child = yield* runNode([
167
+ "--input-type=module",
168
+ "--eval",
169
+ `
170
+import * as NodeNet from "node:net";
171
+import * as NodeTls from "node:tls";
172
+import { ignoreMacosTypeOfServiceEinval } from ${JSON.stringify(new URL("./outboundHttp.ts", import.meta.url).href)};
173
+
174
+NodeTls.setDefaultCACertificates([${JSON.stringify(TEST_CERT)}]);
175
+let calls = 0;
176
+NodeNet.Socket.prototype.setTypeOfService = () => {
177
+ calls++;
178
+ throw Object.assign(new Error("setTypeOfService EINVAL"), { code: "EINVAL" });
179
+};
180
+ignoreMacosTypeOfServiceEinval("darwin");
181
+console.log(await (await fetch(${JSON.stringify(origin)})).text());
182
+if (calls === 0) throw new Error("fetch did not call setTypeOfService");
183
+`,
184
+ ]);
185
+ expect(child.exitCode, child.stderr).toBe(0);
186
+ expect(child.stdout.trim()).toBe("1.1");
187
+ }).pipe(Effect.provide(NodeServices.layer)),
188
+ );
189
+
190
+ it.effect("guards ordinary CLI and both ACP fast paths before their imports run", () =>
191
+ Effect.gen(function* () {
192
+ const dispatchModules = ["mcp/AcpMcpStdioBridge.ts", "cli/consoleStdin.ts", "binCli.ts"];
193
+ // Load the real entrypoint, but replace dispatch modules to avoid starting
194
+ // the app. Probe at import time, when those modules could first open sockets.
195
+ const preload = `
196
+import * as NodeModule from "node:module";
197
+import * as NodeNet from "node:net";
198
+
199
+Object.defineProperty(process, "platform", { value: "darwin" });
200
+NodeNet.Socket.prototype.setTypeOfService = () => {
201
+ throw Object.assign(new Error("setTypeOfService EINVAL"), { code: "EINVAL" });
202
+};
203
+NodeModule.registerHooks({
204
+ load(url, context, nextLoad) {
205
+ const path = ${JSON.stringify(dispatchModules)}.find((path) => url.endsWith("/src/" + path));
206
+ if (path === undefined) return nextLoad(url, context);
207
+ NodeNet.Socket.prototype.setTypeOfService.call(new NodeNet.Socket(), 0);
208
+ console.log(path);
209
+ return {
210
+ format: "module",
211
+ source: "export const runAcpMcpCliFastPath = async () => {}; export const attachConsoleStdin = () => {}; export const runCli = () => {};",
212
+ shortCircuit: true,
213
+ };
214
+ },
215
+});
216
+`;
217
+ const children = yield* Effect.forEach(
218
+ ["--version", "acp-mcp-bridge", "acp-mcp-call"],
219
+ (command) =>
220
+ runNode([
221
+ "--import",
222
+ `data:text/javascript,${encodeURIComponent(preload)}`,
223
+ NodeURL.fileURLToPath(new URL("./bin.ts", import.meta.url)),
224
+ command,
225
+ ]),
226
+ );
227
+ expect(
228
+ children.map((child) => child.exitCode),
229
+ children.map((child) => child.stderr).join("\n"),
230
+ ).toEqual([0, 0, 0]);
231
+ expect(children.map((child) => child.stdout.trim().split("\n"))).toEqual([
232
+ ["cli/consoleStdin.ts", "binCli.ts"],
233
+ ["mcp/AcpMcpStdioBridge.ts"],
234
+ ["mcp/AcpMcpStdioBridge.ts"],
235
+ ]);
236
+ }).pipe(Effect.provide(NodeServices.layer)),
237
+ );
238
+});
239
+
240
describe("extendConnectionAttemptTimeout", () => {
241
it("raises a default below the floor to 1000 ms", () => {
242
NodeNet.setDefaultAutoSelectFamilyAttemptTimeout(50);
apps/server/src/outboundHttp.ts
+25
@@ -43,6 +43,31 @@ export function keepOutboundFetchOnHttp1(): void {
43
if (builtinFetchUsesHttp2(process.versions.undici)) installHttp1FetchDispatcher();
44
}
45
46
+// @types/node does not declare setTypeOfService yet.
47
+type TypeOfServiceSocket = {
48
+ setTypeOfService?: (this: NodeNet.Socket, typeOfService: number) => NodeNet.Socket;
49
+};
50
+
51
+// Bundled undici calls this from a socket callback without catching. On macOS,
52
+// EINVAL after a peer reset must not exit the server over an optional QoS hint.
53
+export function ignoreMacosTypeOfServiceEinval(
54
+ // oxlint-disable-next-line t3code/no-global-process-runtime -- Runs before ACP dispatch without loading the Effect graph.
55
+ platform = process.platform,
56
+ socketPrototype: TypeOfServiceSocket = NodeNet.Socket.prototype as TypeOfServiceSocket,
57
+): void {
58
+ if (platform !== "darwin") return;
59
+ const setTypeOfService = socketPrototype.setTypeOfService;
60
+ if (typeof setTypeOfService !== "function") return;
61
+ socketPrototype.setTypeOfService = function (...args) {
62
+ try {
63
+ return setTypeOfService.apply(this, args);
64
+ } catch (error) {
65
+ if (error instanceof Error && "code" in error && error.code === "EINVAL") return this;
66
+ throw error;
67
+ }
68
+ };
69
+}
70
+
71
/**
72
* Node's 250 ms Happy Eyeballs attempt timeout abandons a working-but-slow IPv4
73
* connect when IPv6 is advertised but unroutable, which broke the ACP registry