fix linux hook stale socket detection
This commit is contained in:
@@ -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<boolean>;
|
||||
|
||||
async function isConnectableUnixSocket(socketPath: string): Promise<boolean> {
|
||||
try {
|
||||
const stat = await fs.stat(socketPath);
|
||||
if (!stat.isSocket()) return false;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
|
||||
return new Promise<boolean>((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<Set<number>> {
|
||||
const result = new Set<number>();
|
||||
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 */
|
||||
|
||||
@@ -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<string> {
|
||||
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);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user