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(update): keep concurrent channel-change holds

guyongbc1d5eb73fb3
diff --git a/apps/desktop/src/main/__tests__/updateService.test.ts b/apps/desktop/src/main/__tests__/updateService.test.ts
index 6d7735afee..4465c5ad1d 100644
--- a/apps/desktop/src/main/__tests__/updateService.test.ts
+++ b/apps/desktop/src/main/__tests__/updateService.test.ts
@@ -857,6 +857,60 @@ describe('startup update relaunch safety', () => {
}
});
+ it('keeps a settings-page hold when a concurrent org-default write throws', async () => {
+ const service = await bootWithStagedPatch({ enabled: true });
+ let releaseProbe: ((busy: boolean) => void) | undefined;
+ const probeStarted = new Promise<void>((resolveStarted) => {
+ service.setUpdateAutoRelaunchBusyProbe(
+ () =>
+ new Promise<boolean>((resolveProbe) => {
+ resolveStarted();
+ releaseProbe = resolveProbe;
+ }),
+ );
+ });
+ let releaseWrite: (() => void) | undefined;
+ writeEnableBeta.mockImplementation(
+ () =>
+ new Promise<undefined>((resolve) => {
+ releaseWrite = () => resolve(undefined);
+ }),
+ );
+ tryEnableUncustomizedBetaAtomic.mockRejectedValue(new Error('lock timeout'));
+ try {
+ await probeStarted;
+ const setHandler = ipcHandlers.get('update-channel-settings-set');
+ expect(setHandler).toBeTypeOf('function');
+ const writePromise = setHandler?.({ sender: { id: 1 } }, { enableBeta: true });
+ await vi.waitFor(() => {
+ expect(releaseWrite).toBeTypeOf('function');
+ });
+ await expect(service.enableUncustomizedBetaChannel()).rejects.toThrow('lock timeout');
+ expect(fs.existsSync(path.join(TEST_USER_DATA, 'updates', 'patch-info.json'))).toBe(true);
+ releaseProbe?.(false);
+ await vi.waitFor(() => {
+ expect(service.getUpdateStatus()).toBe('ready');
+ });
+ expect(fs.existsSync(path.join(TEST_USER_DATA, 'updates', 'patch-info.json'))).toBe(true);
+ logInfo.mockClear();
+ ipcListeners.get('update-relaunch')?.({}, 'dark');
+ expect(logInfo.mock.calls.map((call) => String(call[0]))).toEqual(
+ expect.arrayContaining([
+ expect.stringContaining('executeRelaunch() aborted'),
+ ]),
+ );
+ expect(service.getUpdateStatus()).toBe('ready');
+ releaseWrite?.();
+ await writePromise;
+ await vi.waitFor(() => {
+ expect(service.getUpdateStatus()).toBe('idle');
+ });
+ expect(fs.existsSync(path.join(TEST_USER_DATA, 'updates', 'patch-info.json'))).toBe(false);
+ } finally {
+ service.stopUpdateService();
+ }
+ });
+
it('releases a pending hold when the org-default write throws', async () => {
const service = await bootWithStagedPatch({ enabled: true });
let releaseProbe: ((busy: boolean) => void) | undefined;
diff --git a/apps/desktop/src/main/updateService.ts b/apps/desktop/src/main/updateService.ts
index c7107ea633..5c48e77771 100644
--- a/apps/desktop/src/main/updateService.ts
+++ b/apps/desktop/src/main/updateService.ts
@@ -171,6 +171,10 @@ let autoRelaunchDecisionDepth = 0;
* readyFilePath,只比路径会把刚下好的新渠道补丁清掉。
*/
let deferredStagedPatch: { path?: string; epoch?: number } | undefined;
+/** 并发渠道写入各自持有一次 hold。失败只能放自己那次,不能清掉别人的保护。 */
+let pendingChannelChangeHolds = 0;
+/** 已经落盘成功的渠道切换。后续失败写入不能把这笔作废请求清掉。 */
+let committedChannelChangeInvalidation = false;
let lastAutoRelaunchBlockReason: AutoRelaunchBlockReason | null = null;
let lastBusyAtMs: number | null = null;
let lastResumeAtMs: number | null = null;
@@ -364,7 +368,7 @@ async function requestAutoRelaunch(
if (syncObservedUpdateChannel()) {
log.info('auto relaunch aborted — shared update channel changed');
- rememberDeferredStagedPatch();
+ markCommittedChannelChangeInvalidation();
return { accepted: false, blockReason: 'not-ready' };
}
@@ -657,11 +661,37 @@ function rememberDeferredStagedPatch(): void {
};
}
+function markCommittedChannelChangeInvalidation(): void {
+ committedChannelChangeInvalidation = true;
+ rememberDeferredStagedPatch();
+}
+
+function clearChannelChangeInvalidation(): void {
+ pendingChannelChangeHolds = 0;
+ committedChannelChangeInvalidation = false;
+ deferredStagedPatch = undefined;
+}
+
/** 先拦住 apply,不删 zip / patch-info。写入没成功时还能继续用这份补丁。 */
function holdStagedPatchForPendingChannelChange(): void {
+ pendingChannelChangeHolds += 1;
rememberDeferredStagedPatch();
}
+/** 只放掉本次未提交的 hold。已落盘的渠道切换仍要拦住旧补丁。 */
+function releasePendingChannelChangeHold(): void {
+ pendingChannelChangeHolds = Math.max(0, pendingChannelChangeHolds - 1);
+ if (pendingChannelChangeHolds === 0 && !committedChannelChangeInvalidation) {
+ deferredStagedPatch = undefined;
+ }
+}
+
+/** 本次写入已经改到盘上。后续失败的并发写入不能再放开 apply。 */
+function commitPendingChannelChange(): void {
+ pendingChannelChangeHolds = Math.max(0, pendingChannelChangeHolds - 1);
+ markCommittedChannelChangeInvalidation();
+}
+
function isCurrentPatchNewerThanDeferred(
stale: { path?: string; epoch?: number },
): boolean {
@@ -702,7 +732,7 @@ function flushDeferredStagedPatchClear(): void {
}
return;
}
- deferredStagedPatch = undefined;
+ clearChannelChangeInvalidation();
if (isCurrentPatchNewerThanDeferred(stale)) {
log.info('keeping newer staged patch after deferred channel-change clear');
if (stale.path && stale.path !== readyFilePath) {
@@ -719,7 +749,7 @@ function flushDeferredStagedPatchClear(): void {
*/
function discardUnappliedStagedPatchForChannelRelaunch(): void {
if (isUpdateApplyCommitted()) return;
- deferredStagedPatch = undefined;
+ clearChannelChangeInvalidation();
discardStagedPatchFiles();
}
@@ -1475,12 +1505,13 @@ export function initUpdateService(): void {
} catch (err) {
if (wasBeta !== next) {
observedEnableBeta = wasBeta;
- deferredStagedPatch = undefined;
+ releasePendingChannelChangeHold();
}
log.error('writeEnableBeta failed:', err);
throwIpcError('INTERNAL', 'failed to write update channel settings');
}
if (wasBeta !== next) {
+ commitPendingChannelChange();
clearStagedPatch();
}
return channelSettingsWire();
@@ -1498,12 +1529,13 @@ export function initUpdateService(): void {
} catch (err) {
if (wasBeta) {
observedEnableBeta = wasBeta;
- deferredStagedPatch = undefined;
+ releasePendingChannelChangeHold();
}
log.error('resetUpdateChannelSettings failed:', err);
throwIpcError('INTERNAL', 'failed to reset update channel settings');
}
if (wasBeta) {
+ commitPendingChannelChange();
clearStagedPatch();
}
return channelSettingsWire();
@@ -1706,17 +1738,18 @@ export async function enableUncustomizedBetaChannel(
const wrote = await tryEnableUncustomizedBetaAtomic(shouldWrite);
if (wrote && !wasBeta) {
observedEnableBeta = true;
+ commitPendingChannelChange();
clearStagedPatch();
broadcastChannelSettings();
return wrote;
}
if (!wasBeta) {
- deferredStagedPatch = undefined;
+ releasePendingChannelChangeHold();
}
return wrote;
} catch (err) {
if (!wasBeta) {
- deferredStagedPatch = undefined;
+ releasePendingChannelChangeHold();
}
throw err;
}