Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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])
})
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 5 additions & 1 deletion packages/tauri-app/src-tauri/src/client_state/cross_host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,12 @@ fn process_start_identity(pid: u32) -> Option<String> {

#[cfg(any(target_os = "macos", windows))]
fn command_value(command: &str, args: &[&str]) -> Option<String> {
// 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)
Expand All @@ -885,7 +890,6 @@ fn command_value(command: &str, args: &[&str]) -> Option<String> {
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),
Expand Down
Loading