Skip to content

Commit 14779d1

Browse files
radroidclaude
andcommitted
fix(connection): tolerate a slow-but-alive local backend + heap headroom
Under host memory pressure the local ("This device") backend answers slowly, and the reconnect supervisor abandoned each attempt at a fixed 15s budget and then backed off — repeatedly giving up on a briefly-slow-but-alive backend (issue #21). Client (packages/client-runtime/src/connection/supervisor.ts): - Classify a setup timeout that reached the backend (stage opening/synchronizing) as "slow, not dead": the next attempt gets a 45s establishment budget and a fast 1s retry instead of climbing the exponential ladder. Timeouts that never reached the backend keep the existing 15s budget + exponential backoff. - Bound the tolerant path to MAX_TOLERANT_TIMEOUTS (3) consecutive slow timeouts, then fall back to normal backoff — the stages are reported before the socket is confirmed open, so a genuinely unreachable endpoint must not loop on 45s attempts forever. - Add a 30s background liveness probe (monitorConnectedLease) so a wedged-but-open socket is detected without waiting for foreground activation; the foreground probe was refactored into a shared helper that also handles a relay credentials-change arriving mid-probe. Desktop (apps/desktop/src/backend/DesktopBackendConfiguration.ts): - Give the backend --max-old-space-size=4096 heap headroom (appended to NODE_OPTIONS for the Electron-run-as-node primary, passed as a V8 execArg for WSL). Server-side WS keep-alive/idleTimeout is deferred (the Effect beta transport exposes no idle option and the RPC client pinger already provides client->server liveness) — documented in the PR. Co-Authored-By: Claude Opus 4.8 <[email protected]>
1 parent 6dc983f commit 14779d1

4 files changed

Lines changed: 555 additions & 47 deletions

File tree

‎apps/desktop/src/backend/DesktopBackendConfiguration.test.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ describe("DesktopBackendConfiguration", () => {
135135
assert.equal(first.cwd, environment.backendCwd);
136136
assert.equal(first.captureOutput, true);
137137
assert.equal(first.env.ELECTRON_RUN_AS_NODE, "1");
138+
// Heap headroom for the primary backend (issue #21). extendEnv:true means
139+
// this config value wins over an inherited process.env NODE_OPTIONS.
140+
assert.include(first.env.NODE_OPTIONS ?? "", "--max-old-space-size=4096");
138141
assert.isUndefined(first.env.T3CODE_PORT);
139142
assert.isUndefined(first.env.T3CODE_MODE);
140143
assert.isUndefined(first.env.T3CODE_DESKTOP_LAN_HOST);
@@ -152,6 +155,43 @@ describe("DesktopBackendConfiguration", () => {
152155
),
153156
);
154157

158+
it.effect("resolvePrimary sets a bare heap flag when NODE_OPTIONS is unset", () => {
159+
const previous = process.env.NODE_OPTIONS;
160+
delete process.env.NODE_OPTIONS;
161+
return withHarness(
162+
Effect.gen(function* () {
163+
const configuration = yield* DesktopBackendConfiguration.DesktopBackendConfiguration;
164+
const config = yield* configuration.resolvePrimary;
165+
assert.equal(config.env.NODE_OPTIONS, "--max-old-space-size=4096");
166+
}),
167+
).pipe(Effect.ensuring(Effect.sync(() => restoreEnv("NODE_OPTIONS", previous))));
168+
});
169+
170+
it.effect("resolvePrimary appends heap headroom to an existing NODE_OPTIONS", () => {
171+
const previous = process.env.NODE_OPTIONS;
172+
process.env.NODE_OPTIONS = "--enable-source-maps";
173+
return withHarness(
174+
Effect.gen(function* () {
175+
const configuration = yield* DesktopBackendConfiguration.DesktopBackendConfiguration;
176+
const config = yield* configuration.resolvePrimary;
177+
assert.equal(config.env.NODE_OPTIONS, "--enable-source-maps --max-old-space-size=4096");
178+
}),
179+
).pipe(Effect.ensuring(Effect.sync(() => restoreEnv("NODE_OPTIONS", previous))));
180+
});
181+
182+
it.effect("resolvePrimary preserves an explicit NODE_OPTIONS heap setting", () => {
183+
const previous = process.env.NODE_OPTIONS;
184+
process.env.NODE_OPTIONS = "--max-old-space-size=1024";
185+
return withHarness(
186+
Effect.gen(function* () {
187+
const configuration = yield* DesktopBackendConfiguration.DesktopBackendConfiguration;
188+
const config = yield* configuration.resolvePrimary;
189+
// An operator override wins; we do not append a second, conflicting flag.
190+
assert.equal(config.env.NODE_OPTIONS, "--max-old-space-size=1024");
191+
}),
192+
).pipe(Effect.ensuring(Effect.sync(() => restoreEnv("NODE_OPTIONS", previous))));
193+
});
194+
155195
it.effect("resolveWsl reuses the primary's bootstrap token", () =>
156196
withHarness(
157197
Effect.gen(function* () {
@@ -279,6 +319,8 @@ describe("DesktopBackendConfiguration", () => {
279319
"env",
280320
"PATH=/home/test user's/.nvm/versions/node/v22.0.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/test user/bin:/opt/test's tools/bin:/usr/bin:/bin",
281321
nodePath,
322+
// Heap headroom, passed as a V8 execArg before the entry script (issue #21).
323+
"--max-old-space-size=4096",
282324
linuxEntryPath,
283325
"--bootstrap-fd",
284326
"0",

‎apps/desktop/src/backend/DesktopBackendConfiguration.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,30 @@ const WSL_FORWARDED_ENV_NAMES = ["OPENAI_API_KEY", "ANTHROPIC_API_KEY"] as const
9494

9595
const WSL_SERVER_SYSTEM_PATH = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin";
9696

97+
// Give the backend a predictable V8 old-space ceiling instead of inheriting the
98+
// default. This is headroom for transient heap spikes (large diffs, big file
99+
// reads) so a memory-constrained host is less likely to push the backend into a
100+
// GC-thrash / stall while it is busy — part of the robustness work for issue
101+
// #21. It is a ceiling, not a reservation, so it does not raise steady-state
102+
// memory use (the backend's normal RSS is well under this).
103+
const DESKTOP_BACKEND_MAX_OLD_SPACE_MB = 4096;
104+
const DESKTOP_BACKEND_MAX_OLD_SPACE_FLAG = `--max-old-space-size=${DESKTOP_BACKEND_MAX_OLD_SPACE_MB}`;
105+
106+
// Append our heap-headroom flag to any NODE_OPTIONS already exported (by the
107+
// user or the dev-runner) rather than replacing it. If an explicit
108+
// --max-old-space-size is already present we leave it untouched so an operator
109+
// can override the ceiling. Electron-run-as-node honours the NODE_OPTIONS
110+
// safelist, which includes --max-old-space-size.
111+
const backendNodeOptions = (existing: string | undefined): string => {
112+
const trimmed = existing?.trim() ?? "";
113+
if (trimmed.includes("--max-old-space-size")) {
114+
return trimmed;
115+
}
116+
return trimmed.length > 0
117+
? `${trimmed} ${DESKTOP_BACKEND_MAX_OLD_SPACE_FLAG}`
118+
: DESKTOP_BACKEND_MAX_OLD_SPACE_FLAG;
119+
};
120+
97121
const backendChildEnvPatch = (): Record<string, string | undefined> =>
98122
Object.fromEntries(DESKTOP_BACKEND_ENV_NAMES.map((name) => [name, undefined]));
99123

@@ -356,6 +380,7 @@ const resolvePrimaryStartConfig = Effect.fn("desktop.backendConfiguration.resolv
356380
env: {
357381
...backendChildEnvPatch(),
358382
ELECTRON_RUN_AS_NODE: "1",
383+
NODE_OPTIONS: backendNodeOptions(process.env.NODE_OPTIONS),
359384
},
360385
// Primary wants process.env (PATH, dev-runner's T3CODE_HOME, etc.).
361386
extendEnv: true,
@@ -546,6 +571,10 @@ const resolveWslStartConfig = Effect.fn("desktop.backendConfiguration.resolveWsl
546571
"env",
547572
`PATH=${launchPath}`,
548573
preflight.nodePath,
574+
// Match the primary backend's heap headroom. WSL runs plain node, so the
575+
// flag is passed as a V8 execArg (before the entry script) rather than via
576+
// NODE_OPTIONS.
577+
DESKTOP_BACKEND_MAX_OLD_SPACE_FLAG,
549578
preflight.linuxEntryPath,
550579
"--bootstrap-fd",
551580
"0",

0 commit comments

Comments
 (0)