diff --git a/packages/core/src/hook/hook-session.ts b/packages/core/src/hook/hook-session.ts index a8f7f16b..d8c183ec 100644 --- a/packages/core/src/hook/hook-session.ts +++ b/packages/core/src/hook/hook-session.ts @@ -14,8 +14,13 @@ export type HookSessionDeps = { }; makeClient: (pid: number) => QqHookClient; /** Sync fast-path check used by load() to skip re-injection when a - * prior SnowLuma run left a working pipe behind. */ - pipeWatcher: { isPipeLive: (pid: number) => boolean }; + * prior SnowLuma run left a working pipe behind. `tickNow` forces a + * fresh poll — used after unload to dodge the up-to-1500ms cache + * staleness that would otherwise false-flag a successful unload. */ + pipeWatcher: { + isPipeLive: (pid: number) => boolean; + tickNow?: () => Promise; + }; /** Sink for parsed packets. Called with the BridgeManager-shaped * PacketInfo for every packet received while logged in. If omitted, * packets are dropped (useful in unit tests that don't care). */ @@ -254,7 +259,11 @@ export class HookSession extends EventEmitter { this._uin = '0'; // Verify the unload took: if the pipe is still up the DLL is still - // resident. Drop back to 'connecting' and let the watcher reconnect. + // resident. The cached snapshot is up to 1500ms stale and we just + // changed the world, so force a fresh poll first — otherwise even + // a successful unload reads as "pipe still live" until the next + // background tick runs and we'd report a spurious failure. + await this.pipeWatcher.tickNow?.(); if (this.pipeWatcher.isPipeLive(this.pid)) { this._error = 'DLL卸载失败:命名管道仍然存在,watcher将自动重连'; this.setStatus('connecting', this._error); diff --git a/packages/core/tests/hook-session.test.ts b/packages/core/tests/hook-session.test.ts index f6c24c5c..26127d27 100644 --- a/packages/core/tests/hook-session.test.ts +++ b/packages/core/tests/hook-session.test.ts @@ -254,6 +254,34 @@ describe('HookSession — unload', () => { expect(info.status).toBe('connecting'); expect(info.error).toContain('命名管道仍然存在'); }); + + it('forces a fresh tick before verifying so a stale snapshot does not false-flag a successful unload', async () => { + // Reproduces the Windows-WebUI bug: PipeWatcher's cached snapshot is + // up to 1500ms stale, so right after a successful native unload the + // pre-fix code would read isPipeLive() == true and report failure + // even though the DLL is gone. With the fix, tickNow() refreshes + // the snapshot first and verification correctly sees the pipe down. + const pid = 5555; + let pipeLive = true; // initial cached state: pipe is live + const tickNow = vi.fn(async () => { pipeLive = false; }); // OS poll says: gone now + + const session = new HookSession(pid, { + injector: { + inject: vi.fn(() => ({ method: 'loadModuleManual' as const, handle: DUMMY_HANDLE })), + unload: vi.fn(), + }, + makeClient: () => new FakeClient() as unknown as QqHookClient, + pipeWatcher: { isPipeLive: () => pipeLive, tickNow }, + }); + await session.load(); + pipeLive = true; // ensure the pre-check snapshot says "live" + + const info = await session.unload(); + + expect(tickNow).toHaveBeenCalledOnce(); + expect(info.status).toBe('available'); + expect(info.error).toBe(''); + }); }); describe('HookSession — serialization', () => {