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): 远程桌面结束后直连空闲关闭不再导致应用崩溃

Dash037d5b926ff0
diff --git a/apps/desktop/src/main/device-link/__tests__/remoteInvokeSendGuard.test.ts b/apps/desktop/src/main/device-link/__tests__/remoteInvokeSendGuard.test.ts
index 5db1d6a444..dff5138859 100644
--- a/apps/desktop/src/main/device-link/__tests__/remoteInvokeSendGuard.test.ts
+++ b/apps/desktop/src/main/device-link/__tests__/remoteInvokeSendGuard.test.ts
@@ -13,6 +13,27 @@ const compiled = ts.transpileModule(declaration.getText(source).replace(/^export
compilerOptions: { target: ts.ScriptTarget.ES2022 },
}).outputText;
+type PeerInvoke = (peer: string, channel: string, args: unknown[]) => Promise<unknown>;
+
+function loadRemoteInvoke(overrides: Record<string, unknown>) {
+ const dependencies = {
+ DeviceLinkError, invokeWithClosedLinkRecovery,
+ assertNotStandby: () => {},
+ assertRemoteControlTargetEnabled: () => {},
+ openLinkCloseEpochs: new Map<string, number>(),
+ ensureOnlineForRequest: async () => {},
+ client: { invoke: vi.fn() },
+ isScopedPeer: () => false,
+ tryPeerInvoke: async () => null,
+ resolveRemoteInvokeTimeoutMs: () => undefined,
+ openRemoteLink: vi.fn(),
+ closeRemoteLink: vi.fn(),
+ responsivenessTracker: null,
+ ...overrides,
+ };
+ return new Function(...Object.keys(dependencies), compiled + '; return remoteInvoke;')(...Object.values(dependencies));
+}
+
it.each(['caller cancelled', 'target disabled', 'link closed', 'still valid'])(
'revalidates %s after the client queue and never reopens an explicitly closed link', async (change) => {
let enabled = true;
@@ -27,21 +48,12 @@ it.each(['caller cancelled', 'target disabled', 'link closed', 'still valid'])(
catch (error) { reject(error); }
};
}));
- const dependencies = {
- DeviceLinkError, invokeWithClosedLinkRecovery,
- assertNotStandby: () => {},
+ const remoteInvoke = loadRemoteInvoke({
assertRemoteControlTargetEnabled: () => { if (!enabled) throw new DeviceLinkError('REMOTE_DISABLED', 'disabled'); },
openLinkCloseEpochs: epochs,
- ensureOnlineForRequest: async () => {},
client: { invoke },
- isScopedPeer: () => false,
- tryPeerInvoke: async () => null,
- resolveRemoteInvokeTimeoutMs: () => undefined,
openRemoteLink: reopen,
- closeRemoteLink: vi.fn(),
- responsivenessTracker: null,
- };
- const remoteInvoke = new Function(...Object.keys(dependencies), compiled + '; return remoteInvoke;')(...Object.values(dependencies));
+ });
const cancelled = new Error('caller cancelled');
const pending = remoteInvoke('host', 'maker:send', [], {
preSend: () => { if (!current) throw cancelled; },
@@ -60,3 +72,31 @@ it.each(['caller cancelled', 'target disabled', 'link closed', 'still valid'])(
expect(reopen).not.toHaveBeenCalled();
},
);
+
+// The direct connection keeps this invoke and calls it later from timers (idle close),
+// where `void invoke(...).catch(() => {})` only absorbs rejections. A synchronous throw
+// there escaped as an uncaughtException and shut the app down.
+it('rejects instead of throwing when a stored direct-connection invoke outlives its caller', async () => {
+ let current = true;
+ let stored!: PeerInvoke;
+ const remoteInvoke = loadRemoteInvoke({
+ client: {
+ invoke: vi.fn<DeviceLinkClient['invoke']>(async (_peer, _payload, _timeout, options) => {
+ options?.preSend?.();
+ return { ok: true, result: null };
+ }),
+ },
+ tryPeerInvoke: async (_device: string, _channel: string, _args: unknown[], invoke: PeerInvoke) => {
+ stored = invoke;
+ return null;
+ },
+ });
+ const stopped = new Error('DESKTOP_STOPPED');
+ await remoteInvoke('host', 'remote-desktop', [], {
+ preSend: () => { if (!current) throw stopped; },
+ });
+ current = false;
+ let closing: Promise<unknown> | undefined;
+ expect(() => { closing = stored('host', 'file-peer', [{ action: 'close' }]); }).not.toThrow();
+ await expect(closing).rejects.toBe(stopped);
+});
diff --git a/apps/desktop/src/main/device-link/index.ts b/apps/desktop/src/main/device-link/index.ts
index 8af6acd5f3..2fe1205eee 100644
--- a/apps/desktop/src/main/device-link/index.ts
+++ b/apps/desktop/src/main/device-link/index.ts
@@ -1822,7 +1822,9 @@ export async function remoteInvoke(
if (!client) throw new Error('[DEVICE_LINK_NOT_CONNECTED] device-link client not initialized');
// 共享任务与供应商分享的对端是另一个账号:只走 relay,不尝试点对点直连。
if (!isScopedPeer(deviceId)) {
- const accelerated = await tryPeerInvoke(deviceId, channel, args, (peer, nextChannel, nextArgs) => {
+ // 直连会保存这个回调,之后在空闲关闭等定时器里调用(届时 preSend 可能已失效)。
+ // 必须是 async:失效时返回 rejected promise,不能同步抛出绕过调用方的 .catch。
+ const accelerated = await tryPeerInvoke(deviceId, channel, args, async (peer, nextChannel, nextArgs) => {
preSend();
return remoteInvoke(peer, nextChannel, nextArgs, { preSend });
});