Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(desktop): check process identity before the self-pid lock shortcut

Yuewei-LLe5f128b230af
diff --git a/apps/desktop/src/main/device-link/__tests__/crossProcessLock.test.ts b/apps/desktop/src/main/device-link/__tests__/crossProcessLock.test.ts
index 05ca7a4c15..c5a2c4aceb 100644
--- a/apps/desktop/src/main/device-link/__tests__/crossProcessLock.test.ts
+++ b/apps/desktop/src/main/device-link/__tests__/crossProcessLock.test.ts
@@ -188,6 +188,31 @@ describe('接管陈旧锁', () => {
}
}, 15_000);
+ it('reclaims a self-pid lock whose recorded identity was from a reused pid', async () => {
+ // A crashed instance left a lock naming our pid, but with the dead
+ // instance's process identity. After the OS reuses the pid for us, the
+ // identity no longer matches: the exact-identity comparison must win over
+ // the self-pid shortcut, or strict callers stay permanently busy.
+ const lock = path.join(dir, 'lock');
+ await fsp.writeFile(
+ lock,
+ JSON.stringify({
+ pid: process.pid,
+ startedAt: 1,
+ processStartIdentity: 'start-ms:0',
+ nonce: '00000000-0000-4000-8000-000000000001',
+ state: 'held',
+ }),
+ 'utf8',
+ );
+ const old = new Date(Date.now() - 60_000);
+ await fsp.utimes(lock, old, old);
+
+ await expect(
+ withCrossProcessLock(lock, { label: 'test', waitMs: 1_000 }, async (s) => s),
+ ).resolves.toEqual({ held: true });
+ }, 15_000);
+
it('retries a transient release deletion failure', async () => {
const lock = path.join(dir, 'lock');
const originalRm = fsp.rm;
diff --git a/apps/desktop/src/main/device-link/crossProcessLock.ts b/apps/desktop/src/main/device-link/crossProcessLock.ts
index a95fdd560f..d6c90edb7c 100644
--- a/apps/desktop/src/main/device-link/crossProcessLock.ts
+++ b/apps/desktop/src/main/device-link/crossProcessLock.ts
@@ -683,14 +683,20 @@ async function isRecordOwnerActive(
}
const currentIdentity = await getProcessIdentity(record.pid);
if (!currentIdentity) return true;
- // The current process is always the live holder of a lock it wrote: its start
- // time cannot be compared against the lock's creation time reliably (a
- // long-lived worker can predate the lock while a short-lived one starts after
- // it), so a record naming our own pid is never a PID-reuse takeover target.
- if (record.pid === process.pid) return true;
+ // Exact process identity is the strongest signal and must be checked before
+ // the self-pid shortcut: after a crash + PID reuse the OS may hand our pid to
+ // this process while the lock still records the dead instance's identity, so
+ // a mismatching identity on our own pid is a reused-pid takeover target (and
+ // skipping the comparison would leave strict callers permanently busy).
if (record.processStartIdentity) {
return processIdentitiesMatch(record.processStartIdentity, currentIdentity);
}
+ // No recorded identity: the current process is always the live holder of a
+ // lock it wrote — its start time cannot be compared against the lock's
+ // creation time reliably (a long-lived worker can predate the lock while a
+ // short-lived one starts after it), so a record naming our own pid is never
+ // a PID-reuse takeover target without an identity to disprove it.
+ if (record.pid === process.pid) return true;
// Legacy record without a processStartIdentity: kill(pid, 0) succeeded, so
// the pid is alive. We cannot distinguish a still-running original holder
// from a reused pid, and a live holder must never be squeezed out of its lock