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): Windows 最终 takeover 后 EBUSY 重试发布锁

yanff4e56d5cceb
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 d43461aa0d..5d68474a29 100644
--- a/apps/desktop/src/main/device-link/__tests__/crossProcessLock.test.ts
+++ b/apps/desktop/src/main/device-link/__tests__/crossProcessLock.test.ts
@@ -724,6 +724,46 @@ describe('接管陈旧锁', () => {
}
});
+ it('retries publication after the final takeover when Windows still reports the path busy', async () => {
+ const lock = path.join(dir, 'lock');
+ await writeStaleStrictLock(lock);
+ const originalRename = fsp.rename;
+ const originalLink = fsp.link;
+ let takeovers = 0;
+ let busyPublishes = 0;
+ const renameSpy = vi.spyOn(fsp, 'rename').mockImplementation((async (
+ from: unknown,
+ to: unknown,
+ ) => {
+ const result = await (originalRename as (...args: unknown[]) => Promise<unknown>)(from, to);
+ if (from === lock && typeof to === 'string' && to.startsWith(`${lock}.reclaim-`)) {
+ takeovers += 1;
+ if (takeovers < 3) await writeStaleStrictLock(lock);
+ }
+ return result;
+ }) as typeof fsp.rename);
+ const linkSpy = vi.spyOn(fsp, 'link').mockImplementation((async (
+ from: unknown,
+ to: unknown,
+ ) => {
+ if (to === lock && takeovers === 3 && busyPublishes < 1) {
+ busyPublishes += 1;
+ throw Object.assign(new Error('resource busy'), { code: 'EBUSY' });
+ }
+ return (originalLink as (...args: unknown[]) => Promise<unknown>)(from, to);
+ }) as typeof fsp.link);
+ try {
+ await expect(
+ withCrossProcessLock(lock, { label: 'final-takeover-busy', waitMs: 500 }, async (s) => s),
+ ).resolves.toEqual({ held: true });
+ expect(takeovers).toBe(3);
+ expect(busyPublishes).toBe(1);
+ } finally {
+ renameSpy.mockRestore();
+ linkSpy.mockRestore();
+ }
+ });
+
it('recovers an own reclaim gate after rename and fsync both fail', async () => {
const lock = path.join(dir, 'lock');
const originalOpen = fsp.open;
diff --git a/apps/desktop/src/main/device-link/crossProcessLock.ts b/apps/desktop/src/main/device-link/crossProcessLock.ts
index 4bac351eef..e0162f7ff0 100644
--- a/apps/desktop/src/main/device-link/crossProcessLock.ts
+++ b/apps/desktop/src/main/device-link/crossProcessLock.ts
@@ -274,10 +274,23 @@ export async function withSecurityBoundaryLock<T>(
break;
}
+ if (publishingAfterTakeover && Date.now() >= deadline) {
+ reason = 'busy';
+ break;
+ }
if (
publishingAfterTakeover
- && (Date.now() >= deadline || takeovers >= MAX_TAKEOVERS)
+ && takeovers >= MAX_TAKEOVERS
+ && (code === 'EBUSY' || code === 'EPERM' || code === 'EACCES')
+ && Date.now() < deadline
) {
+ // Windows can keep a just-quarantined path busy for a tick. The final
+ // takeover already spent its reclaim budget; retry publication only.
+ reason = 'busy';
+ await sleep(LOCK_RETRY_MS);
+ continue;
+ }
+ if (publishingAfterTakeover && takeovers >= MAX_TAKEOVERS) {
reason = 'busy';
break;
}