Skip to content

The agent that grows with you

fix(desktop): let the relaunch waiter remove its stage dir

emozilla90292fe02168
diff --git a/apps/desktop/electron/updater/relaunch-waiter-lifecycle.test.ts b/apps/desktop/electron/updater/relaunch-waiter-lifecycle.test.ts
index 9fada2ebd24..c0b0454e0e9 100644
--- a/apps/desktop/electron/updater/relaunch-waiter-lifecycle.test.ts
+++ b/apps/desktop/electron/updater/relaunch-waiter-lifecycle.test.ts
@@ -43,13 +43,13 @@ for (const mode of [
const cleanupError: Error = new Error('fixture staging cleanup refused')
const start: SpawnWaiter = (command: string, args: string[], options: Parameters<SpawnWaiter>[2]): ChildProcess => {
- stage = options.cwd
+ stage = path.dirname(args[args.indexOf('-ReadyFile') + 1])
const readyFile: string = path.join(stage, 'ready.txt')
assert.equal(
command,
path.win32.join(process.env.SystemRoot || process.env.SYSTEMROOT || 'C:\\Windows', 'System32', 'cmd.exe')
)
- assert.deepEqual(options, { cwd: stage, detached: false, stdio: 'ignore', windowsHide: true })
+ assert.deepEqual(options, { cwd: os.tmpdir(), detached: false, stdio: 'ignore', windowsHide: true })
assert.notEqual(stage, path.dirname(scriptPath))
assert.deepEqual(fs.readFileSync(path.join(stage, 'update-relaunch-waiter.ps1')), fs.readFileSync(scriptPath))
assert.deepEqual(args, [
diff --git a/apps/desktop/electron/updater/relaunch-waiter-spawn.windows-live.test.ts b/apps/desktop/electron/updater/relaunch-waiter-spawn.windows-live.test.ts
index 849bd2327ed..3e634bc0537 100644
--- a/apps/desktop/electron/updater/relaunch-waiter-spawn.windows-live.test.ts
+++ b/apps/desktop/electron/updater/relaunch-waiter-spawn.windows-live.test.ts
@@ -37,7 +37,7 @@ it.runIf(process.platform === 'win32')(
)
it.runIf(process.platform === 'win32').each([false, true])(
- 'a real hidden PowerShell waiter survives parent exit unless cancelled (cancel=%s)',
+ 'a real hidden PowerShell waiter survives parent exit unless cancelled and leaves no stage dir (cancel=%s)',
async cancel => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'waiter native '))
const script = path.join(root, 'waiter.ps1')
@@ -49,12 +49,16 @@ it.runIf(process.platform === 'win32').each([false, true])(
`
param([int]$ProcessId, [long]$ProcessStartTimeMs, [string]$IdentityName, [string]$ReadyFile, [int]$TimeoutSeconds)
Set-Content -LiteralPath ($IdentityName + '.pid') -Value $PID
+Set-Content -LiteralPath ($IdentityName + '.stage') -Value $PSScriptRoot
Set-Content -LiteralPath $ReadyFile -Value 'ready'
$deadline = (Get-Date).AddSeconds(20)
while (Get-Process -Id $ProcessId -ErrorAction SilentlyContinue) {
if ((Get-Date) -gt $deadline) { exit 3 }
Start-Sleep -Milliseconds 100
}
+# The same exit cleanup as update-relaunch-waiter.ps1.
+Set-Location $env:TEMP
+Remove-Item -LiteralPath $PSScriptRoot -Recurse -Force -ErrorAction SilentlyContinue
Set-Content -LiteralPath $IdentityName -Value 'parent exited'
`
)
@@ -100,6 +104,10 @@ const { startRelaunchWaiter } = require(${JSON.stringify(bundle)});
await expect.poll(() => fs.existsSync(completed), { timeout: 10_000 }).toBe(true)
expect(fs.readFileSync(completed, 'utf8').trim()).toBe('parent exited')
}
+
+ const stage = fs.readFileSync(completed + '.stage', 'utf8').trim()
+ expect(path.basename(stage)).toMatch(/^hermes-relaunch-/)
+ expect(fs.existsSync(stage), `${stage} is left behind`).toBe(false)
} finally {
if (fs.existsSync(completed + '.pid')) {
const workerPid = Number(fs.readFileSync(completed + '.pid', 'utf8').trim())
diff --git a/apps/desktop/electron/updater/relaunch-waiter.ts b/apps/desktop/electron/updater/relaunch-waiter.ts
index 92554d7655c..9809ff475d8 100644
--- a/apps/desktop/electron/updater/relaunch-waiter.ts
+++ b/apps/desktop/electron/updater/relaunch-waiter.ts
@@ -210,7 +210,9 @@ export async function startRelaunchWaiter(
detached: wrapped.detached,
stdio: 'ignore',
windowsHide: true,
- cwd: staging.stageDir
+ // A working directory pins its folder: keep it out of the package, which
+ // the swap replaces, and out of the stage dir, which the script deletes.
+ cwd: os.tmpdir()
})
} catch (error) {
log(`[updates] relaunch waiter: spawning ${POWERSHELL_PATH} threw: ${errorText(error)}`)