Skip to content

Commit c66c4b9

Browse files
fix(devices): remove advisory install leases from startup
1 parent 65eebc8 commit c66c4b9

6 files changed

Lines changed: 49 additions & 109 deletions

File tree

‎apps/server/src/device/DeviceService.ts‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { claimLocalDeviceTool } from "./deviceToolMaintenance.ts";
21
/**
32
* Device discovery, per-thread device sessions, and the state stream clients
43
* render the Device panel from.
@@ -1112,11 +1111,7 @@ export const make = Effect.gen(function* () {
11121111
return {
11131112
...service,
11141113
agentCli: resolveNodeExecutable("Device automation").pipe(
1115-
Effect.flatMap((nodePath) =>
1116-
claimLocalDeviceTool(config.baseDir, nodePath, "agent").pipe(
1117-
Effect.andThen(ensureAgentDevice(config.baseDir)),
1118-
),
1119-
),
1114+
Effect.andThen(ensureAgentDevice(config.baseDir)),
11201115
Effect.provideService(FileSystem.FileSystem, fs),
11211116
Effect.provideService(Path.Path, path),
11221117
Effect.provideService(ProcessRunner.ProcessRunner, runner),

‎apps/server/src/device/LocalDeviceHost.ts‎

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { claimLocalDeviceTool, pruneLocalDeviceTools } from "./deviceToolMaintenance.ts";
1+
import { pruneLocalDeviceTools } from "./deviceToolMaintenance.ts";
22
import { deviceToolInstallMessage } from "@t3tools/contracts";
33
/**
44
* The device host that is this machine.
@@ -579,14 +579,6 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
579579
const inventory = yield* summary;
580580
yield* onPhase("installing", deviceToolInstallMessage("device hub", inventory.tools?.hub));
581581
}
582-
yield* claimLocalDeviceTool(config.baseDir, nodePath, "hub").pipe(
583-
Effect.provideService(Path.Path, path),
584-
Effect.provideService(ProcessRunner.ProcessRunner, runner),
585-
Effect.mapError(
586-
(cause) =>
587-
new DeviceHost.DeviceHostError({ hostId, step: "reserving device hub install", cause }),
588-
),
589-
);
590582
const hubTool = yield* ensureDeviceHub(config.baseDir).pipe(
591583
Effect.provideService(FileSystem.FileSystem, fs),
592584
Effect.provideService(Path.Path, path),
@@ -653,18 +645,6 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
653645
deviceToolInstallMessage("agent tools", inventory.tools?.agent),
654646
);
655647
}
656-
yield* claimLocalDeviceTool(config.baseDir, running.hub.nodePath, "agent").pipe(
657-
Effect.provideService(Path.Path, path),
658-
Effect.provideService(ProcessRunner.ProcessRunner, runner),
659-
Effect.mapError(
660-
(cause) =>
661-
new DeviceHost.DeviceHostError({
662-
hostId,
663-
step: "reserving agent tools install",
664-
cause,
665-
}),
666-
),
667-
);
668648
const agentTool = yield* ensureAgentDevice(config.baseDir).pipe(
669649
Effect.provideService(FileSystem.FileSystem, fs),
670650
Effect.provideService(Path.Path, path),

‎apps/server/src/device/deviceToolMaintenance.test.ts‎

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,12 @@ import * as NodeOS from "node:os";
99
import * as NodePath from "node:path";
1010
import * as NodeChildProcess from "node:child_process";
1111
import * as NodeUtil from "node:util";
12-
import {
13-
claimLocalDeviceTool,
14-
pruneLocalDeviceTools,
15-
deviceToolMaintenanceScript,
16-
} from "./deviceToolMaintenance.ts";
12+
import { pruneLocalDeviceTools, deviceToolMaintenanceScript } from "./deviceToolMaintenance.ts";
1713

1814
const exec = NodeUtil.promisify(NodeChildProcess.execFile);
1915

2016
describe.each([false, true])("device tool cleanup, flat=%s", (flat) => {
21-
it("keeps current, previous, active, incomplete and legacy installs, pruning only unused managed versions", async () => {
17+
it("keeps current, previous, active and incomplete installs, pruning unused completed versions", async () => {
2218
const root = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-tool-cleanup-"));
2319
const name = "expo-device-hub";
2420
const directory = (version: string) =>
@@ -40,21 +36,42 @@ describe.each([false, true])("device tool cleanup, flat=%s", (flat) => {
4036
`
4137
(async () => {
4238
const root = ${JSON.stringify(root)};
43-
// Simulate a dead owner whose numeric PID was reused by this live test process.
44-
maintenanceFs.mkdirSync(maintenancePath.join(root, '.maintenance-lock'));
45-
maintenanceFs.writeFileSync(maintenancePath.join(root, '.maintenance-lock', 'stale-owner.json'), JSON.stringify({ pid: ${process.pid}, identity: 'previous-process-start' }));
46-
await claimTool(root, '${name}', '0.1.0', ${process.pid});
47-
maintenanceFs.writeFileSync(maintenancePath.join(root, '.users', '${name}@0.1.0', '${process.pid}'), 'previous-process-start');
48-
await claimTool(root, '${name}', '0.2.0', ${process.pid});
49-
await claimTool(root, '${name}', '0.4.0', 2147483647);
5039
await pruneTools(root, [['${name}', '0.6.0']], ${flat});
5140
})().catch(error => { console.error(error); process.exitCode = 1; });`;
52-
await exec(process.execPath, ["-e", script]);
41+
await exec(process.execPath, [
42+
"-e",
43+
script,
44+
NodePath.join(directory("0.2.0"), "active-helper.cjs"),
45+
]);
5346
await expect(NodeFSP.stat(directory("0.1.0"))).rejects.toThrow();
54-
for (const version of ["0.2.0", "0.3.0", "0.4.0", "0.5.0", "0.6.0"])
47+
await expect(NodeFSP.stat(directory("0.3.0"))).rejects.toThrow();
48+
for (const version of ["0.2.0", "0.4.0", "0.5.0", "0.6.0"])
5549
expect((await NodeFSP.stat(directory(version))).isDirectory()).toBe(true);
56-
// Keeping the prior install does not retain dead lease files forever.
57-
expect(await NodeFSP.readdir(NodePath.join(root, ".users", name + "@0.4.0"))).toEqual([]);
50+
} finally {
51+
await NodeFSP.rm(root, { recursive: true, force: true });
52+
}
53+
});
54+
55+
it("keeps every install when the process scan fails", async () => {
56+
const root = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-tool-scan-"));
57+
try {
58+
for (const version of ["0.1.0", "0.2.0", "0.3.0"]) {
59+
const dir = flat
60+
? NodePath.join(root, `expo-device-hub@${version}`)
61+
: NodePath.join(root, "expo-device-hub", version);
62+
await NodeFSP.mkdir(dir, { recursive: true });
63+
await NodeFSP.writeFile(NodePath.join(dir, ".install-complete"), version);
64+
}
65+
await exec(process.execPath, [
66+
"-e",
67+
deviceToolMaintenanceScript +
68+
`
69+
require('node:child_process').spawnSync = () => ({ status: 1, stdout: '' });
70+
pruneTools(${JSON.stringify(root)}, [['expo-device-hub', '0.3.0']], ${flat}).catch(() => process.exitCode = 1);
71+
`,
72+
]);
73+
const parent = flat ? root : NodePath.join(root, "expo-device-hub");
74+
expect((await NodeFSP.readdir(parent)).length).toBe(3);
5875
} finally {
5976
await NodeFSP.rm(root, { recursive: true, force: true });
6077
}
@@ -92,10 +109,7 @@ it.effect("maintenance failures retain safe context and the original process res
92109
stdoutInvalidUtf8: false,
93110
stderrInvalidUtf8: false,
94111
};
95-
for (const [operation, run] of [
96-
["claim", claimLocalDeviceTool],
97-
["prune", pruneLocalDeviceTools],
98-
] as const) {
112+
for (const [operation, run] of [["prune", pruneLocalDeviceTools]] as const) {
99113
const error = yield* run("/tools", process.execPath, "hub").pipe(
100114
Effect.provideService(ProcessRunner.ProcessRunner, { run: () => Effect.succeed(output) }),
101115
Effect.flip,

‎apps/server/src/device/deviceToolMaintenance.ts‎

Lines changed: 9 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,12 @@ import * as Path from "effect/Path";
55
import * as ProcessRunner from "../processRunner.ts";
66
import { AGENT_DEVICE_VERSION, DEVICE_HUB_VERSION } from "./DeviceToolchain.ts";
77

8-
/** Shared with the SSH bootstrap. Leases live outside installs, which npm replaces atomically. */
8+
/** Shared with the SSH bootstrap. Cleanup runs only after successful startup. */
99
export const deviceToolMaintenanceScript = String.raw`
1010
const maintenanceFs = require('node:fs');
1111
const maintenancePath = require('node:path');
12-
const maintenanceIdentity = pid => {
13-
const result = process.platform === 'win32'
14-
? require('node:child_process').spawnSync('powershell.exe', ['-NoProfile', '-NonInteractive', '-Command', '(Get-Process -Id ' + pid + ').StartTime.ToUniversalTime().Ticks'], { encoding: 'utf8', timeout: 10000 })
15-
: require('node:child_process').spawnSync('ps', ['-p', String(pid), '-o', 'lstart='], { encoding: 'utf8', timeout: 10000, env: { ...process.env, TZ: 'UTC', LC_ALL: 'C' } });
16-
return result.status === 0 ? result.stdout.trim() || null : null;
17-
};
18-
const maintenanceAlive = (pid, identity) => {
19-
try {
20-
process.kill(pid, 0);
21-
const current = identity ? maintenanceIdentity(pid) : null;
22-
return !identity || !current || identity === current;
23-
}
12+
const maintenanceAlive = pid => {
13+
try { process.kill(pid, 0); return true; }
2414
catch (error) { return error.code !== 'ESRCH'; }
2515
};
2616
async function withToolMaintenance(root, operation) {
@@ -29,7 +19,7 @@ async function withToolMaintenance(root, operation) {
2919
const nonce = require('node:crypto').randomUUID();
3020
const ownerFile = process.pid + '.' + nonce + '.json';
3121
const candidate = lock + '.' + nonce;
32-
const holder = { pid: process.pid, identity: maintenanceIdentity(process.pid) };
22+
const holder = { pid: process.pid };
3323
const deadline = Date.now() + 30000;
3424
const removeEmptyLock = () => {
3525
try { maintenanceFs.rmdirSync(lock); }
@@ -52,7 +42,7 @@ async function withToolMaintenance(root, operation) {
5242
const previousFile = maintenancePath.join(lock, files[0]);
5343
let previous;
5444
try { previous = JSON.parse(maintenanceFs.readFileSync(previousFile, 'utf8')); } catch {}
55-
if (Number.isSafeInteger(previous?.pid) && previous.pid > 0 && !maintenanceAlive(previous.pid, previous.identity)) {
45+
if (Number.isSafeInteger(previous?.pid) && previous.pid > 0 && !maintenanceAlive(previous.pid)) {
5646
// The unique filename belongs only to that owner. Never unlink a replacement owner's file.
5747
try { maintenanceFs.unlinkSync(previousFile); } catch (error) { if (error.code !== 'ENOENT') throw error; }
5848
}
@@ -72,17 +62,9 @@ async function withToolMaintenance(root, operation) {
7262
maintenanceFs.rmSync(candidate, { recursive: true, force: true });
7363
}
7464
}
75-
function claimTool(root, name, version, pid) {
76-
if (!Number.isSafeInteger(pid) || pid <= 0) throw Error("Missing device tool process identity.");
77-
return withToolMaintenance(root, () => {
78-
const users = maintenancePath.join(root, '.users', name + '@' + version);
79-
maintenanceFs.mkdirSync(users, { recursive: true });
80-
maintenanceFs.writeFileSync(maintenancePath.join(users, String(pid)), maintenanceIdentity(pid) || '');
81-
});
82-
}
8365
function pruneTools(root, specs, flat) {
8466
return withToolMaintenance(root, () => {
85-
// Also protect helpers launched by older T3 releases that predate usage records.
67+
// Keep installs used by any running helper, including older T3 releases.
8668
const scan = process.platform === 'win32'
8769
? require('node:child_process').spawnSync('powershell.exe', ['-NoProfile', '-NonInteractive', '-Command', 'Get-CimInstance Win32_Process | Select-Object -ExpandProperty CommandLine'], { encoding: 'utf8', timeout: 10000 })
8870
: require('node:child_process').spawnSync('ps', ['-ax', '-o', 'command='], { encoding: 'utf8', timeout: 10000 });
@@ -106,20 +88,8 @@ function pruneTools(root, specs, flat) {
10688
if (!completed.some(value => value.version === required)) continue;
10789
const previous = completed.filter(value => value.version !== required).sort((a, b) => b.modified - a.modified || b.version.localeCompare(a.version, 'en', { numeric: true }))[0]?.version;
10890
for (const { version, directory } of completed) {
109-
const users = maintenancePath.join(root, '.users', name + '@' + version);
110-
// Older releases did not register users. Never guess whether one of those installs is still in use.
111-
if (!maintenanceFs.existsSync(users)) continue;
112-
const leases = maintenanceFs.readdirSync(users);
113-
let inUse = false;
114-
for (const lease of leases) {
115-
const file = maintenancePath.join(users, lease);
116-
const identity = maintenanceFs.readFileSync(file, 'utf8');
117-
if (!/^[1-9][0-9]*$/.test(lease) || maintenanceAlive(Number(lease), identity)) inUse = true;
118-
else maintenanceFs.rmSync(file, { force: true });
119-
}
120-
if (version === required || version === previous || scan.stdout.includes(directory + maintenancePath.sep) || inUse) continue;
91+
if (version === required || version === previous || scan.stdout.includes(directory + maintenancePath.sep)) continue;
12192
maintenanceFs.rmSync(directory, { recursive: true, force: true });
122-
maintenanceFs.rmSync(users, { recursive: true, force: true });
12393
}
12494
}
12595
});
@@ -129,7 +99,7 @@ function pruneTools(root, specs, flat) {
12999
class DeviceToolMaintenanceError extends Schema.TaggedError<DeviceToolMaintenanceError>()(
130100
"DeviceToolMaintenanceError",
131101
{
132-
operation: Schema.Literals(["claim", "prune"]),
102+
operation: Schema.Literal("prune"),
133103
tool: Schema.Literals(["hub", "agent"]),
134104
exitCode: Schema.NullOr(Schema.Int),
135105
cause: Schema.Defect(),
@@ -143,7 +113,7 @@ class DeviceToolMaintenanceError extends Schema.TaggedError<DeviceToolMaintenanc
143113
const runMaintenance = Effect.fn("DeviceToolchain.maintenance")(function* (
144114
nodePath: string,
145115
script: string,
146-
operation: "claim" | "prune",
116+
operation: "prune",
147117
tool: "hub" | "agent",
148118
) {
149119
const runner = yield* ProcessRunner.ProcessRunner;
@@ -163,24 +133,6 @@ const runMaintenance = Effect.fn("DeviceToolchain.maintenance")(function* (
163133
);
164134
});
165135

166-
export const claimLocalDeviceTool = Effect.fn("DeviceToolchain.claim")(function* (
167-
baseDir: string,
168-
nodePath: string,
169-
tool: "hub" | "agent",
170-
) {
171-
const path = yield* Path.Path;
172-
const [name, version] =
173-
tool === "hub"
174-
? ["expo-device-hub", DEVICE_HUB_VERSION]
175-
: ["agent-device", AGENT_DEVICE_VERSION];
176-
yield* runMaintenance(
177-
nodePath,
178-
`claimTool(${JSON.stringify(path.join(baseDir, "tools"))}, ${JSON.stringify(name)}, ${JSON.stringify(version)}, ${process.pid})`,
179-
"claim",
180-
tool,
181-
);
182-
});
183-
184136
export const pruneLocalDeviceTools = Effect.fn("DeviceToolchain.prune")(function* (
185137
baseDir: string,
186138
nodePath: string,

‎apps/server/src/device/sshDeviceScript.test.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ else { const child=spawn(process.execPath,[path.join(path.dirname(process.argv[1
125125
);
126126
await NodeFSP.mkdir(NodePath.join(root, "hosts/one"), { recursive: true });
127127
await NodeFSP.writeFile(NodePath.join(root, "hosts/one/fail-start-once"), "");
128+
// Unavailable advisory bookkeeping must not prevent either helper from starting.
129+
await NodeFSP.writeFile(NodePath.join(root, "tools/.maintenance-lock"), "blocked");
130+
await NodeFSP.writeFile(NodePath.join(root, "tools/.users"), "unwritable lease directory");
128131
try {
129132
const [manual, concurrent] = await Promise.all([
130133
invoke("one", "start"),

‎apps/server/src/device/sshDeviceScript.ts‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@ async function install(name, version, entry) {
164164
}
165165
if (!ios && !android) throw Error(platforms.map(p => p.reason).join(' '));
166166
fs.mkdirSync(state, { recursive: true, mode: 0o700 });
167-
await claimTool(path.join(root, 'tools'), 'expo-device-hub', hubVersion, process.pid);
168167
const hubEntry = await install('expo-device-hub', hubVersion, 'dist/server/cli.mjs');
169168
let hub = read(hubFile);
170169
if (!hub || hub.owner !== owner || hub.entryPath !== hubEntry || !await healthy(hub.port, '/readyz')) {
@@ -193,10 +192,8 @@ async function install(name, version, entry) {
193192
if (attempt === 4) throw Error('Device hub exited before becoming ready. See ' + path.join(state, 'hub.log'));
194193
}
195194
}
196-
await claimTool(path.join(root, 'tools'), 'expo-device-hub', hubVersion, hub.pid);
197195
let agentResult = {};
198196
if (mode === 'agent-start') {
199-
await claimTool(path.join(root, 'tools'), 'agent-device', agentVersion, process.pid);
200197
const agentEntry = await install('agent-device', agentVersion, 'bin/agent-device.mjs');
201198
const previousAgent = read(agentFile)?.entryPath;
202199
let daemon = read(daemonFile);
@@ -215,7 +212,6 @@ async function install(name, version, entry) {
215212
}
216213
if (!daemon || !await healthy(daemon.httpPort, '/health')) throw Error('agent-device daemon did not become ready in ' + state);
217214
write(agentFile, { entryPath: agentEntry });
218-
await claimTool(path.join(root, 'tools'), 'agent-device', agentVersion, daemon.pid);
219215
agentResult = { daemonPort: daemon.httpPort, token: daemon.token, entryPath: agentEntry };
220216
}
221217
const vendor = path.resolve(path.dirname(hubEntry), '../../vendor/serve-sim/dist');

0 commit comments

Comments
 (0)