t3code icon indicating copy to clipboard operation
t3code copied to clipboard

[Bug]: Desktop startup waits on a hung readiness probe and on the local Codex probe

Open pedrosatin opened this issue 1 month ago • 1 comments

Before submitting

  • [x] I searched existing issues and did not find a duplicate.
  • [x] I included enough detail to reproduce or investigate the problem.

Area

apps/server

Steps to reproduce

  1. Build the server bundle from main (vp run build:bundle in apps/server).
  2. Start it the way the desktop app does: Electron as Node with the compile cache, --bootstrap-fd 3, and a desktop bootstrap envelope with shellEnvironmentPrepared: true. Use a T3 home that has already started once.
  3. Poll GET /.well-known/t3/environment like DesktopBackendManager.waitForHttpReady: every 100 ms, 1 s timeout per request.
  4. Record the time from spawn until the first 200.

The script I used is at the end.

Expected behavior

The desktop sees the backend as ready shortly after the server can answer.

Actual behavior

Two things delay readiness. The first accounts for about 375 ms of a 1.9 s startup here; the second depends on how codex resolves on PATH.

1. A request that arrives before the router is up never gets an answer. NodeHttpServer.layer calls listen() when the layer is built, but HttpRouter.serve only adds the "request" listener after every runtime layer is up. In between, guardHttpResponseWriteErrors is the only "request" listener, and it does not respond. The desktop polls from the moment it spawns the backend, so its first successful connect lands in that window and waits out the full 1 s probe timeout. Logging each probe attempt:

[t ms, finished at ms, result]
[5, 61, "ECONNREFUSED"], [1044, 2048, "TimeoutError"], [2053, 2081, 200]
[5, 57, "ECONNREFUSED"], [1034, 2038, "TimeoutError"], [2043, 2069, 200]

With a 3 s probe timeout the early request still hung for the whole 3 s, although the process was idle by then.

2. The local Codex probe blocks the layers built after CodexInstallation. makeCodexInstallation runs codex --version and codex app-server --help before it returns. makeCodexAdapterV2 and the provider checks start only after it ends. The two commands take 26 ms with the binary on PATH, 84 ms through a standard mise shim, and 432 ms through a wrapper that runs mise use -g first. A shim that hangs can use the probe's whole 5 s timeout. #17543 moved this probe off the startup path, but it measured layer construction rather than readiness. Because of problem 1, that change alone does not move desktop readiness at all (table below).

Readiness, ms from spawn to first 200. main @ 9c16146, codex binary on PATH, 20 interleaved rounds per variant after one warm-up each, same T3 home:

Variant Median p10 p90
main 1920 1915 1935
Early requests held until the router is up 1546 1511 1653
Codex probe in the background 1918 1914 1932
Both 1559 1462 1615

With a standard mise shim first on PATH: main 1920, early requests held 1560, both 1528. With the mise use -g wrapper, both was about 120 ms faster than the early-request fix alone.

The code path behind problem 1 does not depend on the platform, but I measured only on Linux. The size of problem 2 depends on how codex resolves on PATH.

Impact

Minor bug or occasional failure

Version or commit

main @ 9c161462c9

Environment

Omarchy (Arch Linux), kernel 7.2.3-arch1-3, Intel Core Ultra 5 125H, 16 GB. Electron 44.4.5 / Node 24.21.0 from the installed t3code-nightly-bin 0.1.1_nightly.20261017.3238. Codex CLI 0.163.0 through mise.

Logs or stack traces

Server trace for one startup on main @ 6a6ebb278d with the mise use -g wrapper, offsets from the first span:

  144.0 +137.7  makeCodexInstallation
  146.9 +134.1  CodexInstallation.runCommand
  288.6 +  0.1  makeCodexAdapterV2
  571.0 +  0.0  server.startup.http.wait
  583.9 +  0.0  server.startup.ready.publish
 1009.5 +  9.0  GET /.well-known/t3/environment
Readiness script
// node bench.mjs <t3-home> <rounds> <label=apps/server dist dir>...
import { spawn } from "node:child_process";
import { randomBytes } from "node:crypto";
import { setTimeout as sleep } from "node:timers/promises";
import net from "node:net";

const [homeDir, roundsArg, ...variantArgs] = process.argv.slice(2);
const variants = variantArgs.map((arg) => {
  const [label, distDir] = arg.split("=");
  return { label, distDir, samples: [] };
});
const electron = process.env.ELECTRON_BIN ?? "/usr/lib/t3code-nightly/t3code";
// compileCache.cjs: require("node:module").enableCompileCache(<dir>)
const compileCache = new URL("./compileCache.cjs", import.meta.url).pathname;

const freePort = () =>
  new Promise((resolve) => {
    const srv = net.createServer();
    srv.listen(0, "127.0.0.1", () => {
      const { port } = srv.address();
      srv.close(() => resolve(port));
    });
  });

const once = async (distDir) => {
  const port = await freePort();
  const started = performance.now();
  const child = spawn(electron, ["--require", compileCache, `${distDir}/bin.mjs`, "--bootstrap-fd", "3"], {
    env: { ...process.env, ELECTRON_RUN_AS_NODE: "1" },
    stdio: ["ignore", "ignore", "ignore", "pipe"],
  });
  child.stdio[3].end(
    `${JSON.stringify({
      mode: "desktop",
      noBrowser: true,
      port,
      t3Home: homeDir,
      host: "127.0.0.1",
      desktopBootstrapToken: randomBytes(16).toString("hex"),
      shellEnvironmentPrepared: true,
      tailscaleServeEnabled: false,
      tailscaleServePort: 443,
    })}\n`,
  );
  const exited = new Promise((r) => child.once("exit", r));
  let readyMs = null;
  while (performance.now() - started < 60_000) {
    const attemptStarted = performance.now();
    try {
      const res = await fetch(`http://127.0.0.1:${port}/.well-known/t3/environment`, {
        signal: AbortSignal.timeout(1000),
      });
      if (res.ok) {
        readyMs = performance.now() - started;
        break;
      }
    } catch {}
    await sleep(Math.max(0, 100 - (performance.now() - attemptStarted)));
  }
  child.kill("SIGTERM");
  await exited;
  return readyMs;
};

for (const v of variants) await once(v.distDir);
for (let round = 0; round < Number(roundsArg); round++) {
  for (const v of variants) {
    v.samples.push(await once(v.distDir));
    await sleep(300);
  }
}
for (const v of variants) {
  const s = [...v.samples].sort((a, b) => a - b);
  const q = (p) => Math.round(s[Math.min(s.length - 1, Math.floor(p * s.length))]);
  console.log(v.label, { median: q(0.5), p10: q(0.1), p90: q(0.9) });
}

Workaround

None.


Claude Opus 5.5 in Claude Code, run from T3 Code.

pedrosatin avatar Oct 18 '26 12:10 pedrosatin

[!NOTE] Grok responding on behalf of Julius.

Thanks for the readiness measurements. This is tracked by two open fix PRs: #21180 (answer requests that arrive before the router is up) and #21182 (move the local Codex probe off the startup path, replacing #17543). I linked both here, and #21182 will close this issue when it merges.

juliusmarminge avatar Oct 18 '26 12:10 juliusmarminge