diff --git a/packages/electron-app/electron/main/client-state-process-identity.test.ts b/packages/electron-app/electron/main/client-state-process-identity.test.ts new file mode 100644 index 0000000000..5bacb0447e --- /dev/null +++ b/packages/electron-app/electron/main/client-state-process-identity.test.ts @@ -0,0 +1,43 @@ +import assert from "node:assert/strict" +import childProcess from "node:child_process" +import { syncBuiltinESMExports } from "node:module" +import test, { type TestContext } from "node:test" +import { getProcessStartIdentity } from "./client-state-process-identity" + +const commandPlatform = process.platform === "win32" || process.platform === "darwin" +const prefix = process.platform === "darwin" ? "darwin" : "win32" +function probe(t: TestContext, result: (timeout: number, attempt: number) => { elapsed: number; value?: string }) { + let now = 0 + const timeouts: number[] = [] + t.mock.method(performance, "now", () => now) + t.mock.method(childProcess, "spawnSync", (...args: any[]) => { + const timeout = args[2].timeout as number + timeouts.push(timeout) + const reply = result(timeout, timeouts.length) + now += reply.elapsed + return { pid: 1, status: reply.value ? 0 : null, signal: reply.value ? null : "SIGTERM", + stdout: reply.value ?? "", stderr: "", output: [null, reply.value ?? "", ""], + error: reply.value ? undefined : Object.assign(new Error("probe unavailable"), { code: "ETIMEDOUT" }) } + }) + syncBuiltinESMExports() + t.after(() => { t.mock.restoreAll(); syncBuiltinESMExports() }) + return timeouts +} + +test("a slow cold identity helper succeeds inside the total lookup budget", { skip: !commandPlatform }, t => { + const timeouts = probe(t, timeout => timeout >= 6_000 ? { elapsed: 6_000, value: "birth" } : { elapsed: timeout }) + assert.equal(getProcessStartIdentity(process.pid), `${prefix}:birth`) + assert.deepEqual(timeouts, [10_000]) +}) + +test("a failed identity attempt leaves only its remaining budget for the retry", { skip: !commandPlatform }, t => { + const timeouts = probe(t, (_timeout, attempt) => attempt === 1 ? { elapsed: 4_000 } : { elapsed: 1_000, value: "birth" }) + assert.equal(getProcessStartIdentity(process.pid), `${prefix}:birth`) + assert.deepEqual(timeouts, [10_000, 6_000]) +}) + +test("identity budget exhaustion stays unavailable without starting another helper", { skip: !commandPlatform }, t => { + const timeouts = probe(t, timeout => ({ elapsed: timeout })) + assert.equal(getProcessStartIdentity(process.pid), undefined) + assert.deepEqual(timeouts, [10_000]) +}) diff --git a/packages/electron-app/electron/main/client-state-process-identity.ts b/packages/electron-app/electron/main/client-state-process-identity.ts index 752d5cddbd..c5922c803b 100644 --- a/packages/electron-app/electron/main/client-state-process-identity.ts +++ b/packages/electron-app/electron/main/client-state-process-identity.ts @@ -22,11 +22,15 @@ function readLinuxProcessStartIdentity(pid: number): string | undefined { } function readCommandIdentity(command: string, args: string[], prefix: string): string | undefined { + // One cold helper may use the existing two-attempt budget; retries share its remainder. + const deadline = performance.now() + 10_000 for (let attempt = 0; attempt < 2; attempt += 1) { + const remaining = Math.ceil(deadline - performance.now()) + if (remaining <= 0) break const result = spawnSync(command, args, { encoding: "utf8", windowsHide: true, - timeout: 5_000, + timeout: remaining, }) if (result.status === 0 && !result.error) { const value = result.stdout.trim() diff --git a/packages/tauri-app/src-tauri/src/client_state/cross_host.rs b/packages/tauri-app/src-tauri/src/client_state/cross_host.rs index 9b83e31898..b4aab970d1 100644 --- a/packages/tauri-app/src-tauri/src/client_state/cross_host.rs +++ b/packages/tauri-app/src-tauri/src/client_state/cross_host.rs @@ -871,7 +871,12 @@ fn process_start_identity(pid: u32) -> Option { #[cfg(any(target_os = "macos", windows))] fn command_value(command: &str, args: &[&str]) -> Option { + // Cold startup and both attempts share the existing total lookup budget. + let deadline = Instant::now() + Duration::from_secs(10); for _ in 0..2 { + if Instant::now() >= deadline { + break; + } let mut command = Command::new(command); command .args(args) @@ -885,7 +890,6 @@ fn command_value(command: &str, args: &[&str]) -> Option { let Ok(mut child) = command.spawn() else { continue; }; - let deadline = Instant::now() + Duration::from_secs(5); let status = loop { match child.try_wait() { Ok(Some(status)) => break Some(status),