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(device-link): 区分文件锁竞争与初始化失败

Dash8243a527950e
diff --git a/apps/desktop/src/main/device-link/__tests__/ownership.test.ts b/apps/desktop/src/main/device-link/__tests__/ownership.test.ts
index 1a2b03e21f..ca6c2e0fd5 100644
--- a/apps/desktop/src/main/device-link/__tests__/ownership.test.ts
+++ b/apps/desktop/src/main/device-link/__tests__/ownership.test.ts
@@ -11,6 +11,7 @@ import Database from 'better-sqlite3';
import {
DeviceLinkOwnershipArbiter,
createSqliteExclusiveFileLock,
+ isSqliteLockContention,
type OwnershipLock,
} from '../ownership';
@@ -170,6 +171,24 @@ describe('DeviceLinkOwnershipArbiter', () => {
}
});
+ it('锁初始化失败(非竞争)→ 不假装他人持有,下一轮再试', () => {
+ const lock: OwnershipLock = {
+ tryAcquire() {
+ throw Object.assign(new Error('disk I/O error'), { code: 'SQLITE_IOERR' });
+ },
+ release() {},
+ isHeld() {
+ return false;
+ },
+ };
+ const { arbiter, onAcquire, onStandbyChanged } = makeArbiter({ lock });
+ arbiter.tick();
+ expect(arbiter.isOwner()).toBe(false);
+ expect(arbiter.isStandby()).toBe(false);
+ expect(onAcquire).not.toHaveBeenCalled();
+ expect(onStandbyChanged).not.toHaveBeenCalled();
+ });
+
it('失去持有权、接管和 stop 都会翻转待命状态', () => {
const lock = memoryLock();
const { arbiter, onStandbyChanged } = makeArbiter({ lock });
@@ -231,6 +250,35 @@ describe('createSqliteExclusiveFileLock', () => {
}
});
+ it('SQLITE_BUSY 视为竞争;其它 sqlite 错误向上抛', () => {
+ const { dir, lockPath } = openTempLock();
+ try {
+ const busy = createSqliteExclusiveFileLock(lockPath, () => {
+ throw Object.assign(new Error('database is locked'), { code: 'SQLITE_BUSY' });
+ });
+ expect(busy.tryAcquire()).toBe(false);
+
+ const io = createSqliteExclusiveFileLock(lockPath, () => {
+ throw Object.assign(new Error('disk I/O error'), { code: 'SQLITE_IOERR' });
+ });
+ expect(() => io.tryAcquire()).toThrow(/disk I\/O error/);
+ } finally {
+ rmSync(dir, { recursive: true, force: true });
+ }
+ });
+
+ it('isSqliteLockContention 只认忙/锁,不把 IO 错误当占用', () => {
+ expect(
+ isSqliteLockContention(
+ Object.assign(new Error('database is locked'), { code: 'SQLITE_BUSY' }),
+ ),
+ ).toBe(true);
+ expect(
+ isSqliteLockContention(Object.assign(new Error('disk I/O error'), { code: 'SQLITE_IOERR' })),
+ ).toBe(false);
+ expect(isSqliteLockContention(new Error('database is locked'))).toBe(true);
+ });
+
it('双仲裁器共享同一文件锁:任意时刻至多一个持有者', () => {
const { dir, lockPath } = openTempLock();
const lockA = openLock(lockPath);
diff --git a/apps/desktop/src/main/device-link/ownership.ts b/apps/desktop/src/main/device-link/ownership.ts
index 65ab2a055b..dca9f7fbbc 100644
--- a/apps/desktop/src/main/device-link/ownership.ts
+++ b/apps/desktop/src/main/device-link/ownership.ts
@@ -18,6 +18,11 @@
*
* 锁文件按 userId 隔离,不打开聊天库。历史表 device_link_ownership 不再读写。
*
+ * 混版本不回写旧租约:旧进程只认那张表,新进程只认文件锁,两边互相看不见。
+ * 同 userData 的 official+dev 若一个仍走心跳、一个已换文件锁,会双连 relay
+ * (4409)。同版本之间由文件锁仲裁;跨版本请用 --isolated,或先退出旧进程。
+ * 不双写旧表 —— 那会重新绑上聊天库,并让「谁是持有者」出现两套判据。
+ *
* 分层:本模块是纯 main 侧业务逻辑,锁依赖注入(可用内存实现单测),
* packages/device-link 保持纯传输层不感知仲裁。
*/
@@ -33,6 +38,29 @@ const log = createLogger('device-link-ownership');
/** 被动实例重试抢锁的间隔。崩溃后接管延迟由这一拍决定,不是过期窗口。 */
export const DEFAULT_LOCK_RETRY_MS = 500;
+function sqliteErrorCode(err: unknown): string {
+ if (!err || typeof err !== 'object' || !('code' in err)) return '';
+ return String((err as { code: unknown }).code);
+}
+
+/** SQLITE_BUSY / LOCKED 是同伴持锁;其它错误不能当成「有人占用」。 */
+export function isSqliteLockContention(err: unknown): boolean {
+ const code = sqliteErrorCode(err);
+ if (
+ code === 'SQLITE_BUSY' ||
+ code === 'SQLITE_LOCKED' ||
+ code === 'SQLITE_BUSY_SNAPSHOT' ||
+ code === 'SQLITE_BUSY_RECOVERY' ||
+ code === 'SQLITE_BUSY_TIMEOUT' ||
+ code === '5' ||
+ code === '6'
+ ) {
+ return true;
+ }
+ const message = err instanceof Error ? err.message : String(err);
+ return /database is locked|SQLITE_BUSY|SQLITE_LOCKED/i.test(message);
+}
+
export interface OwnershipLock {
/** 非阻塞:拿到返回 true,已被他人持有返回 false */
tryAcquire(): boolean;
@@ -211,9 +239,11 @@ export function createSqliteExclusiveFileLock(
handle.exec('CREATE TABLE IF NOT EXISTS ownership_lock (id INTEGER PRIMARY KEY NOT NULL)');
db = handle;
return true;
- } catch {
+ } catch (err) {
if (handle) closeQuietly(handle);
- return false;
+ if (isSqliteLockContention(err)) return false;
+ log.warn('ownership lock initialize failed', err);
+ throw err;
}
},
release() {