From 4a49c55775459a2d2b6557365b06991d83029acf Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Mon, 18 May 2026 13:07:10 +0800 Subject: [PATCH] fix(hook): refresh pipe snapshot before verifying unload took MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit After native unload returns, the verification check if (pipeWatcher.isPipeLive(pid)) { ... unload-failed message ... } read PipeWatcher's cached snapshot, which is updated every 1500ms. On Windows clicking "Unload" in WebUI almost always tripped this: the native unload succeeded and the kernel pipe object was gone, but the watcher's cached "live" state from the previous tick was still fresh, so the session reported "DLL卸载失败:命名管道仍然存在" even though the DLL was successfully unloaded. The user then sees the status flip back to connecting and waits for a real (cached) refresh. PipeWatcher already exposes tickNow() for exactly this situation — the load path doesn't need it because injection's verification runs async via onPipeUp, but unload verifies inline. HookSessionDeps.pipeWatcher gains an optional `tickNow` and unloadInternal awaits it before reading isPipeLive. Existing test mocks that pass only `{ isPipeLive }` keep working (optional chain is a no-op there). New regression test covers the production scenario — stale-true cache flipped to false by tickNow. --- packages/core/src/hook/hook-session.ts | 15 ++++++++++--- packages/core/tests/hook-session.test.ts | 28 ++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) 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', () => {