From e917720d9d9ff7e58e04b1cfbea46effcb5d6ba2 Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Mon, 11 May 2026 23:14:27 +0800 Subject: [PATCH] fix linux hook stale socket detection --- packages/core/src/hook/qq-hook-client.ts | 64 ++++++++++++++++++++-- packages/core/tests/qq-hook-client.test.ts | 44 +++++++++++++++ 2 files changed, 103 insertions(+), 5 deletions(-) create mode 100644 packages/core/tests/qq-hook-client.test.ts diff --git a/packages/core/src/hook/qq-hook-client.ts b/packages/core/src/hook/qq-hook-client.ts index 795b67fb..9844a750 100644 --- a/packages/core/src/hook/qq-hook-client.ts +++ b/packages/core/src/hook/qq-hook-client.ts @@ -9,6 +9,7 @@ export const PIPE_VERSION = 1; export const HEADER_SIZE = 40; export const DEFAULT_ACK_TIMEOUT_MS = 5000; export const DEFAULT_REPLY_TIMEOUT_MS = 30000; +const DEFAULT_PIPE_PROBE_TIMEOUT_MS = 250; export enum PipeOp { hello = 1, @@ -102,6 +103,63 @@ function mojoPipeName(pid: number, suffix: string): string { return path.join(linuxRuntimeDir(), `mojo.${pid}.${suffix}.sock`); } +type LinuxPipeProbe = (socketPath: string) => Promise; + +async function isConnectableUnixSocket(socketPath: string): Promise { + try { + const stat = await fs.stat(socketPath); + if (!stat.isSocket()) return false; + } catch { + return false; + } + + return new Promise((resolve) => { + let socket: net.Socket; + try { + socket = net.createConnection(socketPath); + } catch { + resolve(false); + return; + } + let done = false; + let timer: NodeJS.Timeout; + const finish = (ok: boolean) => { + if (done) return; + done = true; + clearTimeout(timer); + socket.removeAllListeners(); + socket.destroy(); + resolve(ok); + }; + timer = setTimeout(() => finish(false), DEFAULT_PIPE_PROBE_TIMEOUT_MS); + socket.once('connect', () => finish(true)); + socket.once('error', () => finish(false)); + }); +} + +export async function listLiveLinuxPipePids( + runtimeDir = linuxRuntimeDir(), + probe: LinuxPipeProbe = isConnectableUnixSocket, +): Promise> { + const result = new Set(); + let names: string[]; + try { + names = await fs.readdir(runtimeDir); + } catch { + return result; + } + + await Promise.all(names.map(async (name) => { + const m = /^mojo\.(\d+)\.control\.sock$/.exec(name); + if (!m) return; + const pid = Number(m[1]); + if (!Number.isInteger(pid) || pid <= 0) return; + if (await probe(path.join(runtimeDir, name))) result.add(pid); + })); + + return result; +} + function toBuffer(body: Buffer | Uint8Array | string | null | undefined): Buffer { if (Buffer.isBuffer(body)) return body; if (body instanceof Uint8Array) return Buffer.from(body); @@ -312,11 +370,7 @@ export class QqHookClient extends EventEmitter { if (m) result.add(Number(m[1])); } } else { - const names = await fs.readdir(linuxRuntimeDir()); - for (const name of names) { - const m = /^mojo\.(\d+)\.control\.sock$/.exec(name); - if (m) result.add(Number(m[1])); - } + return await listLiveLinuxPipePids(); } } catch { /* directory missing or inaccessible — treat as no live pipes */ diff --git a/packages/core/tests/qq-hook-client.test.ts b/packages/core/tests/qq-hook-client.test.ts new file mode 100644 index 00000000..3034e522 --- /dev/null +++ b/packages/core/tests/qq-hook-client.test.ts @@ -0,0 +1,44 @@ +import { mkdtemp, rm, writeFile } from 'fs/promises'; +import os from 'os'; +import path from 'path'; +import { afterEach, describe, expect, it, vi } from 'vitest'; +import { listLiveLinuxPipePids } from '../src/hook/qq-hook-client'; + +let tmpDir: string | null = null; + +async function makeRuntimeDir(): Promise { + tmpDir = await mkdtemp(path.join(os.tmpdir(), 'snowluma-hook-')); + return tmpDir; +} + +afterEach(async () => { + if (!tmpDir) return; + await rm(tmpDir, { recursive: true, force: true }); + tmpDir = null; +}); + +describe('listLiveLinuxPipePids', () => { + it('ignores stale-looking control socket names when the probe rejects them', async () => { + const runtimeDir = await makeRuntimeDir(); + await writeFile(path.join(runtimeDir, 'mojo.55.control.sock'), ''); + await writeFile(path.join(runtimeDir, 'mojo.55.recv.sock'), ''); + + const pids = await listLiveLinuxPipePids(runtimeDir, async () => false); + + expect([...pids]).toEqual([]); + }); + + it('returns only connectable control sockets', async () => { + const runtimeDir = await makeRuntimeDir(); + await writeFile(path.join(runtimeDir, 'mojo.55.control.sock'), ''); + await writeFile(path.join(runtimeDir, 'mojo.56.control.sock'), ''); + await writeFile(path.join(runtimeDir, 'mojo.56.recv.sock'), ''); + await writeFile(path.join(runtimeDir, 'mojo.not-a-pid.control.sock'), ''); + + const probe = vi.fn(async (socketPath: string) => socketPath.endsWith('mojo.56.control.sock')); + const pids = await listLiveLinuxPipePids(runtimeDir, probe); + + expect([...pids]).toEqual([56]); + expect(probe).toHaveBeenCalledTimes(2); + }); +});