Skip to content

The agent that grows with you

fix(update): repair-lock reclaim removes only the file it judged dead

Tekniumce4deb6a13b0
diff --git a/apps/desktop/electron/desktop-installation.test.ts b/apps/desktop/electron/desktop-installation.test.ts
index 1e2f3de8a01..f4c32c1d03e 100644
--- a/apps/desktop/electron/desktop-installation.test.ts
+++ b/apps/desktop/electron/desktop-installation.test.ts
@@ -4,9 +4,10 @@ import fs from 'node:fs'
import os from 'node:os'
import path from 'node:path'
-import { test } from 'vitest'
+import { test, vi } from 'vitest'
import { loadOrCreateInstallationId, parseInstallationId, sshOwnershipId } from './desktop-installation'
+import { formatCreateTime, processCreateTimeSync } from './update-marker'
const ID_A = '11111111-1111-4111-8111-111111111111'
const ID_B = '22222222-2222-4222-8222-222222222222'
@@ -125,3 +126,45 @@ test('a repair lock whose holder died is reclaimed instead of bricking launch',
fs.writeFileSync(`${filePath}.repair.lock`, '')
assert.equal(loadOrCreateInstallationId(filePath, () => ID_B), ID_B)
}))
+
+// Review G5: reclaiming a dead repair lock by read/compare/unlink-by-name could
+// delete the fresh lock a peer created after reclaiming the same dead one (the
+// peer swaps the file between our compare and our unlink). The reclaim must
+// only ever remove the file it judged dead.
+test('reclaiming a dead repair lock never deletes a lock a peer created meanwhile', () =>
+ withTempDir(directory => {
+ const filePath = path.join(directory, 'installation.json')
+ const repairPath = `${filePath}.repair.lock`
+ const gone = spawnSync(process.execPath, ['-p', 'process.pid'], { encoding: 'utf8' })
+ const peerBody = `${process.pid}\nct:${formatCreateTime(processCreateTimeSync(process.pid)!)}\n`
+ fs.writeFileSync(repairPath, `${gone.stdout.trim()}\n`)
+
+ const realRead = fs.readFileSync
+ let lockReads = 0
+
+ const spy = vi.spyOn(fs, 'readFileSync').mockImplementation(((target: any, ...rest: any[]) => {
+ const value = (realRead as any)(target, ...rest)
+
+ // The second read of the lock (the old compare, or the read of our own
+ // rename claim): a peer reclaims the dead lock and takes it, live.
+ if (String(target).startsWith(repairPath) && ++lockReads === 2) {
+ fs.rmSync(repairPath, { force: true })
+ fs.writeFileSync(repairPath, peerBody, { flag: 'wx' })
+ }
+
+ return value
+ }) as any)
+
+ try {
+ assert.throws(() => loadOrCreateInstallationId(filePath, () => ID_A), /Could not repair/)
+ } finally {
+ spy.mockRestore()
+ }
+
+ assert.equal(fs.readFileSync(repairPath, 'utf8'), peerBody, "the peer's live lock survives")
+ assert.deepEqual(
+ fs.readdirSync(directory).filter(name => name.endsWith('.reclaim')),
+ [],
+ 'no rename claim is left behind'
+ )
+ }))
diff --git a/apps/desktop/electron/desktop-installation.ts b/apps/desktop/electron/desktop-installation.ts
index 661277946c3..0a2421d0948 100644
--- a/apps/desktop/electron/desktop-installation.ts
+++ b/apps/desktop/electron/desktop-installation.ts
@@ -78,20 +78,59 @@ function reclaimDeadRepairLock(repairPath: string, emptyPolls: number): boolean
return false
}
+ // Rename-then-verify: a read/compare/unlink by name could delete the fresh
+ // lock a peer created after reclaiming the same dead one. The rename is the
+ // atomic claim (one reclaimer wins it); the moved file is unlinked only if it
+ // still holds the bytes judged dead, else it goes back under the name.
+ const claim = `${repairPath}.${process.pid}.${crypto.randomBytes(4).toString('hex')}.reclaim`
+
try {
- // Compare-and-delete: only the bytes judged dead.
- if (fs.readFileSync(repairPath).equals(raw)) {
- fs.unlinkSync(repairPath)
+ fs.renameSync(repairPath, claim)
+ } catch {
+ return false
+ }
- return true
- }
+ let moved: Buffer | null = null
+
+ try {
+ moved = fs.readFileSync(claim)
} catch {
- void 0
+ moved = null
+ }
+
+ if (moved?.equals(raw)) {
+ dropReclaimClaim(claim)
+
+ return true
}
+ try {
+ fs.linkSync(claim, repairPath) // never over a lock created meanwhile
+ } catch (error: any) {
+ if (error?.code !== 'EEXIST') {
+ // No hard links on this filesystem: move it back (EEXIST = a newer lock
+ // already holds the name, and its holder proceeds).
+ try {
+ fs.renameSync(claim, repairPath)
+ } catch {
+ void 0
+ }
+ }
+ }
+
+ dropReclaimClaim(claim)
+
return false
}
+function dropReclaimClaim(claim: string) {
+ try {
+ fs.rmSync(claim, { force: true })
+ } catch {
+ void 0 // a leftover `.reclaim` sibling is inert: nothing reads it
+ }
+}
+
/** Count consecutive empty-lock polls, then reclaim a dead lock or back off. */
function waitOnContendedRepairLock(repairPath: string, emptyPolls: number): number {
let polls: number
diff --git a/apps/desktop/src/global.d.ts b/apps/desktop/src/global.d.ts
index e544fe3f4dc..946af79ee6c 100644
--- a/apps/desktop/src/global.d.ts
+++ b/apps/desktop/src/global.d.ts
@@ -1404,7 +1404,7 @@ export interface DesktopBootProgress {
updateHold?: DesktopUpdateHold | null
}
-/** What holds the install while the boot is blocked (electron/main.ts UpdateHoldWire). */
+/** What holds the install while the boot is blocked (electron/update-hold-wiring.ts UpdateHoldWire). */
export interface DesktopUpdateHold {
holdId: string
/** `held`: a leftover process holds the checkout lock; `busy`/`error`: ownership could not be verified. */