Allow SnowLuma to discover and restore accounts that were already active before startup. Avoid reporting sessions ready before they can serve requests, preserve account changes, and keep discovery failures observable. Add regression coverage for delayed startup, recovery, and process isolation. Refs #305 Refs #307
231 lines
7.6 KiB
TypeScript
231 lines
7.6 KiB
TypeScript
import { createLogger, type Logger } from '@snowluma/common/logger';
|
|
import { EventEmitter } from 'events';
|
|
import type { HookProcessBaseInfo } from './injector';
|
|
|
|
const DEFAULT_INTERVAL_MS = 1500;
|
|
const MIN_INTERVAL_MS = 250;
|
|
|
|
export type PipeWatcherDeps = {
|
|
/** Native: list QQ.exe processes currently running. May be async and may
|
|
* return `null` (UNKNOWN) when enumeration timed out / failed — in which
|
|
* case this tick keeps the prior process+pipe state instead of treating the
|
|
* missing data as "everything disappeared" (issue #158). */
|
|
listProcesses: () => HookProcessBaseInfo[] | null | Promise<HookProcessBaseInfo[] | null>;
|
|
/** Native: PIDs that currently have a live SnowLuma named pipe. The current
|
|
* process snapshot lets Linux inspect each target user's runtime directory. */
|
|
listLivePipes: (processes: readonly HookProcessBaseInfo[]) => Promise<Set<number>>;
|
|
/** Polling interval in ms. Defaults to 1500, floored to 250. */
|
|
intervalMs?: number;
|
|
log?: Logger;
|
|
};
|
|
|
|
/**
|
|
* PipeWatcher — the only module that polls the OS for QQ.exe processes
|
|
* and live SnowLuma named pipes. Subscribers consume diffs as events.
|
|
*
|
|
* Lifecycle: start() runs one tick synchronously, then schedules a poll
|
|
* every `intervalMs`. tickNow() drains a single tick on demand. wake()
|
|
* pulls the next scheduled tick forward to the next event-loop turn.
|
|
*
|
|
* Emitted events:
|
|
* 'process-discovered' (info: HookProcessBaseInfo) — a QQ.exe appeared
|
|
* 'process-gone' (pid: number) — a QQ.exe disappeared
|
|
* 'pipe-up' (pid: number) — named pipe came up
|
|
* 'pipe-down' (pid: number) — named pipe dropped
|
|
* 'tick' () — fired after every poll
|
|
*
|
|
* Within a single tick the order is: process-discovered, pipe-up,
|
|
* pipe-down, process-gone, tick. That ordering lets HookManager create
|
|
* a session before it gets a pipe event and run disconnect logic before
|
|
* dispose logic when QQ.exe is killed with a live pipe.
|
|
*/
|
|
export class PipeWatcher extends EventEmitter {
|
|
private readonly listProcesses: PipeWatcherDeps['listProcesses'];
|
|
private readonly listLivePipes: PipeWatcherDeps['listLivePipes'];
|
|
private readonly intervalMs: number;
|
|
private readonly log: Logger;
|
|
|
|
private livePipes = new Set<number>();
|
|
private knownPids = new Set<number>();
|
|
private timer: NodeJS.Timeout | null = null;
|
|
private ticking = false;
|
|
private pendingWake = false;
|
|
private startPromise: Promise<void> | null = null;
|
|
private stopped = false;
|
|
|
|
constructor(deps: PipeWatcherDeps) {
|
|
super();
|
|
this.listProcesses = deps.listProcesses;
|
|
this.listLivePipes = deps.listLivePipes;
|
|
this.intervalMs = Math.max(MIN_INTERVAL_MS, deps.intervalMs ?? DEFAULT_INTERVAL_MS);
|
|
this.log = deps.log ?? createLogger('PipeWatcher');
|
|
}
|
|
|
|
/** Was this PID's pipe live as of the last completed tick? */
|
|
isPipeLive(pid: number): boolean {
|
|
return this.livePipes.has(pid);
|
|
}
|
|
|
|
/** Was this PID alive as of the last completed tick? */
|
|
isProcessAlive(pid: number): boolean {
|
|
return this.knownPids.has(pid);
|
|
}
|
|
|
|
/**
|
|
* Run an initial tick (so isPipeLive/isProcessAlive return real data)
|
|
* and begin the polling loop. Idempotent.
|
|
*/
|
|
start(): Promise<void> {
|
|
if (this.startPromise) return this.startPromise;
|
|
this.startPromise = (async () => {
|
|
try {
|
|
await this.tickOnce();
|
|
} finally {
|
|
this.scheduleNext(this.intervalMs);
|
|
}
|
|
})();
|
|
return this.startPromise;
|
|
}
|
|
|
|
/** Pull the next scheduled tick forward to the next event-loop turn. */
|
|
wake(): void {
|
|
if (this.stopped) return;
|
|
this.scheduleNext(0);
|
|
}
|
|
|
|
/** Force a single tick now. Resolves after diff events have been emitted. */
|
|
tickNow(): Promise<void> {
|
|
return this.tickOnce();
|
|
}
|
|
|
|
stop(): void {
|
|
this.stopped = true;
|
|
if (this.timer) {
|
|
clearTimeout(this.timer);
|
|
this.timer = null;
|
|
}
|
|
}
|
|
|
|
dispose(): void {
|
|
this.stop();
|
|
this.removeAllListeners();
|
|
}
|
|
|
|
private scheduleNext(delayMs: number): void {
|
|
if (this.stopped) return;
|
|
if (this.timer) {
|
|
clearTimeout(this.timer);
|
|
this.timer = null;
|
|
}
|
|
this.timer = setTimeout(() => {
|
|
this.timer = null;
|
|
void this.tickAndReschedule();
|
|
}, Math.max(0, delayMs));
|
|
}
|
|
|
|
private async tickAndReschedule(): Promise<void> {
|
|
try {
|
|
await this.tickOnce();
|
|
} finally {
|
|
this.scheduleNext(this.intervalMs);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Read processes + live pipes, emit diff events. If another tick is
|
|
* already running, mark a follow-up tick so we don't lose a wake().
|
|
*/
|
|
private async tickOnce(): Promise<void> {
|
|
if (this.ticking) {
|
|
this.pendingWake = true;
|
|
return;
|
|
}
|
|
this.ticking = true;
|
|
try {
|
|
let processes: HookProcessBaseInfo[] | null;
|
|
try {
|
|
processes = await this.listProcesses();
|
|
} catch (error) {
|
|
this.log.warn('listProcesses failed: %s', errMsg(error));
|
|
processes = null;
|
|
}
|
|
// UNKNOWN (timeout / failure): keep the prior process + pipe snapshot.
|
|
// Treating missing data as "everything disappeared" would fire
|
|
// process-gone for every live PID and dispose healthy sessions — the
|
|
// freeze-then-teardown footgun behind issue #158. Still emit 'tick' so
|
|
// the manager's per-tick reconnect retries keep running off last-known
|
|
// live pipes.
|
|
if (processes === null) {
|
|
this.emit('tick');
|
|
return;
|
|
}
|
|
let livePipes: Set<number> | null;
|
|
try {
|
|
livePipes = await this.listLivePipes(processes);
|
|
} catch (error) {
|
|
this.log.warn('listLivePipes failed: %s', errMsg(error));
|
|
livePipes = null;
|
|
}
|
|
|
|
const newKnownPids = new Set<number>();
|
|
for (const proc of processes) newKnownPids.add(proc.pid);
|
|
|
|
// 1. Newly discovered processes.
|
|
for (const proc of processes) {
|
|
if (!this.knownPids.has(proc.pid)) {
|
|
this.emit('process-discovered', proc);
|
|
}
|
|
}
|
|
|
|
// 2. Pipes that came up. Ignore pipes for processes we don't know
|
|
// about (would race against an out-of-order tick).
|
|
const newLivePipes = new Set<number>();
|
|
if (livePipes === null) {
|
|
// Keep the last known pipe state for processes that still exist, while
|
|
// allowing confirmed process exits to complete their normal teardown.
|
|
for (const pid of this.livePipes) {
|
|
if (newKnownPids.has(pid)) newLivePipes.add(pid);
|
|
}
|
|
} else {
|
|
for (const pid of livePipes) {
|
|
if (!newKnownPids.has(pid)) continue;
|
|
newLivePipes.add(pid);
|
|
if (!this.livePipes.has(pid)) {
|
|
this.emit('pipe-up', pid);
|
|
}
|
|
}
|
|
}
|
|
|
|
// 3. Pipes that dropped. Emit pipe-down BEFORE process-gone so
|
|
// subscribers can run disconnect logic before dispose logic.
|
|
for (const pid of this.livePipes) {
|
|
if (!newLivePipes.has(pid)) {
|
|
this.emit('pipe-down', pid);
|
|
}
|
|
}
|
|
|
|
// 4. Processes that disappeared.
|
|
for (const pid of this.knownPids) {
|
|
if (!newKnownPids.has(pid)) {
|
|
this.emit('process-gone', pid);
|
|
}
|
|
}
|
|
|
|
this.knownPids = newKnownPids;
|
|
this.livePipes = newLivePipes;
|
|
this.emit('tick');
|
|
} finally {
|
|
this.ticking = false;
|
|
if (this.pendingWake) {
|
|
this.pendingWake = false;
|
|
// Defer to break sync recursion if a listener calls wake().
|
|
setImmediate(() => { void this.tickOnce(); });
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
function errMsg(value: unknown): string {
|
|
return value instanceof Error ? value.message : String(value);
|
|
}
|