Skip to content

The agent that grows with you

test(desktop): never leave the e2e remote gateway orphaned

kshitijk4poore19efba7f0c0
diff --git a/apps/desktop/e2e/peer-device-reset.spec.ts b/apps/desktop/e2e/peer-device-reset.spec.ts
index 9855b837ff8..f068eeff95e 100644
--- a/apps/desktop/e2e/peer-device-reset.spec.ts
+++ b/apps/desktop/e2e/peer-device-reset.spec.ts
@@ -85,28 +85,37 @@ async function freePort(): Promise<number> {
})
}
-async function stopRemote(child: ChildProcess | undefined): Promise<void> {
- if (!child?.pid || child.exitCode !== null || child.signalCode !== null) {
- return
- }
-
- const exited = new Promise<void>(resolve => child.once('exit', () => resolve()))
+function remoteAlive(child: ChildProcess): boolean {
+ return Boolean(child.pid) && child.exitCode === null && child.signalCode === null
+}
- const stop = (signal: NodeJS.Signals) => {
+// The remote runs in its own process group so its Python children die with it.
+// ESRCH (group already gone) must not abort the rest of teardown.
+function signalRemote(child: ChildProcess, signal: NodeJS.Signals): void {
+ try {
if (process.platform === 'win32') {
child.kill(signal)
} else {
process.kill(-child.pid!, signal)
}
+ } catch {
+ /* already exited */
+ }
+}
+
+async function stopRemote(child: ChildProcess | undefined): Promise<void> {
+ if (!child || !remoteAlive(child)) {
+ return
}
- stop('SIGTERM')
+ const exited = new Promise<void>(resolve => child.once('exit', () => resolve()))
+ signalRemote(child, 'SIGTERM')
let timer: ReturnType<typeof setTimeout> | undefined
await Promise.race([exited, new Promise<void>(resolve => { timer = setTimeout(resolve, 5_000) })])
clearTimeout(timer)
- if (child.exitCode === null && child.signalCode === null) {
- stop('SIGKILL')
+ if (remoteAlive(child)) {
+ signalRemote(child, 'SIGKILL')
await exited
}
}
@@ -162,6 +171,15 @@ const peerTest = test.extend<{ gateways: { app: ElectronApplication; source: Pag
env: { ...isolatedEnv(remote), HERMES_DASHBOARD_SESSION_TOKEN: REMOTE_TOKEN },
stdio: ['ignore', 'pipe', 'pipe'],
})
+ // Fixture setup shares the test budget; a timeout while still awaiting
+ // readiness abandons this function before `finally`, and a detached
+ // child outlives the worker. Reap it on process exit regardless.
+ const spawned = child
+ process.once('exit', () => {
+ if (remoteAlive(spawned)) {
+ signalRemote(spawned, 'SIGKILL')
+ }
+ })
child.stdout?.on('data', chunk => { remoteLog += chunk.toString() })
child.stderr?.on('data', chunk => { remoteLog += chunk.toString() })
let spawnError: Error | undefined