fix(bridge): preserve QQ runtime failures across session updates
Surface connection failures immediately to waiting requests and keep the original error visible until the session reconnects. Ignore late login updates from the failed connection.
This commit is contained in:
4 files changed
+269
-40
No files matched your search
@@ -88,6 +88,7 @@ export class HookSession extends EventEmitter {
|
||||
|
||||
private _status: HookProcessStatus = 'available';
|
||||
private _error = '';
|
||||
private runtimeFailure = '';
|
||||
private _uin = '0';
|
||||
private _method = '';
|
||||
private _name = '';
|
||||
@@ -500,6 +501,11 @@ export class HookSession extends EventEmitter {
|
||||
* defaults to the current `loggedIn`; callers that just tore the client
|
||||
* down pass the value captured *before* teardown (teardown clears it). */
|
||||
private applyStatus(wasLoggedIn: boolean = this.loggedIn, error = ''): void {
|
||||
if (this.runtimeFailure) {
|
||||
this.setStatus('error', this.runtimeFailure);
|
||||
return;
|
||||
}
|
||||
|
||||
this.setStatus(
|
||||
statusFor({
|
||||
injected: this.injected,
|
||||
@@ -559,7 +565,7 @@ export class HookSession extends EventEmitter {
|
||||
this.pid,
|
||||
renderParamsVerbose(this.toInfo()),
|
||||
]);
|
||||
this.tearDownClient();
|
||||
this.tearDownClient(true);
|
||||
this.applyStatus(wasLoggedIn);
|
||||
|
||||
try {
|
||||
@@ -607,6 +613,7 @@ export class HookSession extends EventEmitter {
|
||||
}
|
||||
|
||||
if (!this.client) {
|
||||
this.runtimeFailure = '';
|
||||
this.client = this.makeClient(this.pid);
|
||||
this.sender = new HookPacketClient(this.client, (healthy) =>
|
||||
this.setOutboundHealthy(healthy),
|
||||
@@ -623,6 +630,11 @@ export class HookSession extends EventEmitter {
|
||||
|
||||
try {
|
||||
await client.connectAll({ recv: true });
|
||||
|
||||
if (this.runtimeFailure) {
|
||||
throw new Error(this.runtimeFailure);
|
||||
}
|
||||
|
||||
this.connected = true;
|
||||
const loginState = client.getLoginState();
|
||||
|
||||
@@ -650,7 +662,7 @@ export class HookSession extends EventEmitter {
|
||||
this._error = errorMessage(error);
|
||||
// Drop the client so the next attempt builds a fresh socket pair.
|
||||
// A failed connect was never logged in → 'connecting' (or 'available').
|
||||
this.tearDownClient();
|
||||
this.tearDownClient(true);
|
||||
this.applyStatus(false, this._error);
|
||||
this.log.trace(() => [
|
||||
'hook_session_connect_terminal pid=%d outcome=failed reason=connect_failed error=%j state=%s elapsedMs=%d',
|
||||
@@ -664,11 +676,28 @@ export class HookSession extends EventEmitter {
|
||||
}
|
||||
|
||||
private bindClient(client: QqHookClient): void {
|
||||
client.on('runtimeError', (error) => {
|
||||
if (this.client !== client || this.disposed) {
|
||||
return;
|
||||
}
|
||||
|
||||
const wasLoggedIn = this.loggedIn;
|
||||
this.runtimeFailure = errorMessage(error);
|
||||
this.loggedIn = false;
|
||||
this.stopLoginReconcile();
|
||||
this.resetReceiveWatch();
|
||||
this.applyStatus(wasLoggedIn);
|
||||
this.log.error('QQ runtime unavailable: PID=%d err=%s', this.pid, this.runtimeFailure);
|
||||
|
||||
if (wasLoggedIn) {
|
||||
this.emit('disconnected', true);
|
||||
}
|
||||
});
|
||||
client.on('packet', (packet) => this.handlePacket(packet));
|
||||
client.on('loginState', (state) => this.handleLoginState(state));
|
||||
client.on('error', (error) => {
|
||||
const msg = errorMessage(error);
|
||||
this._error = msg;
|
||||
this._error = this.runtimeFailure || msg;
|
||||
this.log.warn('pipe error: PID=%d err=%s', this.pid, msg);
|
||||
});
|
||||
client.on('close', () => {
|
||||
@@ -698,7 +727,7 @@ export class HookSession extends EventEmitter {
|
||||
});
|
||||
}
|
||||
|
||||
private tearDownClient(): void {
|
||||
private tearDownClient(preserveRuntimeFailure = false): void {
|
||||
this.stopLoginReconcile();
|
||||
this.resetReceiveWatch();
|
||||
const client = this.client;
|
||||
@@ -714,6 +743,11 @@ export class HookSession extends EventEmitter {
|
||||
}
|
||||
|
||||
this.client = null;
|
||||
|
||||
if (!preserveRuntimeFailure) {
|
||||
this.runtimeFailure = '';
|
||||
}
|
||||
|
||||
this.sender = null;
|
||||
this.bound = false;
|
||||
this.connected = false;
|
||||
@@ -723,6 +757,10 @@ export class HookSession extends EventEmitter {
|
||||
}
|
||||
|
||||
private handleLoginState(state: QqHookLoginState): void {
|
||||
if (this.runtimeFailure) {
|
||||
return;
|
||||
}
|
||||
|
||||
const wasLoggedIn = this.loggedIn;
|
||||
const previousUin = this._uin;
|
||||
this._uin = state.uin || state.uinNumber.toString();
|
||||
@@ -800,7 +838,7 @@ export class HookSession extends EventEmitter {
|
||||
// reconciliation, but never synthesize online state from the detection.
|
||||
|
||||
private startLoginReconcile(): void {
|
||||
if (this.loginReconcileTimer || this.disposed) {
|
||||
if (this.loginReconcileTimer || this.disposed || this.runtimeFailure) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -829,7 +867,7 @@ export class HookSession extends EventEmitter {
|
||||
|
||||
const client = this.client;
|
||||
|
||||
if (this.disposed || !client || !this.connected || this.loggedIn) {
|
||||
if (this.disposed || !client || !this.connected || this.loggedIn || this.runtimeFailure) {
|
||||
this.stopLoginReconcile();
|
||||
return;
|
||||
}
|
||||
@@ -861,7 +899,13 @@ export class HookSession extends EventEmitter {
|
||||
}
|
||||
|
||||
// A reconnect owns a new client; an older read cannot supply its identity.
|
||||
if (this.client !== client || this.disposed || !this.connected || this.loggedIn) {
|
||||
if (
|
||||
this.client !== client ||
|
||||
this.disposed ||
|
||||
!this.connected ||
|
||||
this.loggedIn ||
|
||||
this.runtimeFailure
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -902,7 +946,8 @@ export class HookSession extends EventEmitter {
|
||||
this.client !== client ||
|
||||
this.disposed ||
|
||||
!this.connected ||
|
||||
this.loggedIn
|
||||
this.loggedIn ||
|
||||
this.runtimeFailure
|
||||
) {
|
||||
return;
|
||||
}
|
||||
@@ -911,7 +956,7 @@ export class HookSession extends EventEmitter {
|
||||
this.nextLoginIdentityObservationAt =
|
||||
Date.now() + LOGIN_IDENTITY_OBSERVE_INTERVAL_MS;
|
||||
} catch (error) {
|
||||
if (this.client !== client || this.disposed) {
|
||||
if (this.client !== client || this.disposed || this.runtimeFailure) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -563,6 +563,7 @@ export class QqHookClient extends EventEmitter {
|
||||
private recvHelloResolver: Deferred<QqHookHello> | null = null;
|
||||
private controlWriteChain: Promise<unknown> = Promise.resolve();
|
||||
private closed = false;
|
||||
private runtimeFailure: Error | null = null;
|
||||
private loginState: QqHookLoginState = { loggedIn: false, uin: '0', uinNumber: 0n };
|
||||
private readonly loginWaiters = new Set<Deferred<QqHookLoginState>>();
|
||||
|
||||
@@ -615,10 +616,7 @@ export class QqHookClient extends EventEmitter {
|
||||
|
||||
async waitForLogin({ timeoutMs = 0 } = {}): Promise<QqHookLoginState> {
|
||||
await this.connect();
|
||||
|
||||
if (this.closed) {
|
||||
throw new Error('qq_hook client is closed');
|
||||
}
|
||||
this.assertUsable();
|
||||
|
||||
if (this.loginState.loggedIn) {
|
||||
return this.getLoginState();
|
||||
@@ -692,9 +690,7 @@ export class QqHookClient extends EventEmitter {
|
||||
}
|
||||
|
||||
async connect(): Promise<QqHookHello> {
|
||||
if (this.closed) {
|
||||
throw new Error('qq_hook client is closed');
|
||||
}
|
||||
this.assertUsable();
|
||||
|
||||
if (this.controlSocket && this.controlHello) {
|
||||
return this.controlHello;
|
||||
@@ -714,9 +710,7 @@ export class QqHookClient extends EventEmitter {
|
||||
}
|
||||
|
||||
async startRecv(onPacket?: (packet: QqHookPacket) => void): Promise<QqHookHello> {
|
||||
if (this.closed) {
|
||||
throw new Error('qq_hook client is closed');
|
||||
}
|
||||
this.assertUsable();
|
||||
|
||||
if (typeof onPacket === 'function') {
|
||||
this.on('packet', onPacket);
|
||||
@@ -996,6 +990,16 @@ export class QqHookClient extends EventEmitter {
|
||||
this.controlWriteChain = Promise.resolve();
|
||||
}
|
||||
|
||||
private assertUsable(): void {
|
||||
if (this.runtimeFailure) {
|
||||
throw this.runtimeFailure;
|
||||
}
|
||||
|
||||
if (this.closed) {
|
||||
throw new Error('qq_hook client is closed');
|
||||
}
|
||||
}
|
||||
|
||||
private rejectLoginWaiters(error: Error): void {
|
||||
for (const waiter of this.loginWaiters) {
|
||||
waiter.reject(error);
|
||||
@@ -1033,26 +1037,28 @@ export class QqHookClient extends EventEmitter {
|
||||
const socket = this.controlSocket;
|
||||
|
||||
if (!socket || socket.destroyed) {
|
||||
return Promise.reject(new Error('control pipe is not connected'));
|
||||
return Promise.reject(
|
||||
this.runtimeFailure ?? new Error('control pipe is not connected'),
|
||||
);
|
||||
}
|
||||
|
||||
const writePromise = this.controlWriteChain.then(
|
||||
() =>
|
||||
new Promise<void>((resolve, reject) => {
|
||||
if (!this.controlSocket || this.controlSocket.destroyed) {
|
||||
reject(new Error('control pipe is not connected'));
|
||||
return;
|
||||
}
|
||||
const writePromise = this.controlWriteChain.then(() => {
|
||||
this.assertUsable();
|
||||
|
||||
socket.write(payload, (error) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
} else {
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
}),
|
||||
);
|
||||
if (this.controlSocket !== socket || socket.destroyed) {
|
||||
throw new Error('control pipe is not connected');
|
||||
}
|
||||
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
socket.write(payload, (error) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
} else {
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
this.controlWriteChain = writePromise.catch(() => {});
|
||||
return writePromise;
|
||||
}
|
||||
@@ -1132,6 +1138,11 @@ export class QqHookClient extends EventEmitter {
|
||||
),
|
||||
);
|
||||
|
||||
if (this.closed || this.runtimeFailure) {
|
||||
socket.destroy();
|
||||
this.assertUsable();
|
||||
}
|
||||
|
||||
if (kind === 'control') {
|
||||
this.controlSocket = socket;
|
||||
} else {
|
||||
@@ -1146,6 +1157,8 @@ export class QqHookClient extends EventEmitter {
|
||||
phase = 'hello';
|
||||
const hello = await this.waitForHello(kind === 'recv');
|
||||
|
||||
this.assertUsable();
|
||||
|
||||
if (kind === 'control') {
|
||||
this.controlHello = hello;
|
||||
} else {
|
||||
@@ -1301,6 +1314,10 @@ export class QqHookClient extends EventEmitter {
|
||||
}
|
||||
|
||||
private handleControlFrame(frame: PipeFrame): void {
|
||||
if (this.runtimeFailure) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (frame.op === PipeOp.hello) {
|
||||
this.resolveHello(frame, false);
|
||||
return;
|
||||
@@ -1346,6 +1363,16 @@ export class QqHookClient extends EventEmitter {
|
||||
frame.status,
|
||||
frame.requestId,
|
||||
);
|
||||
|
||||
if (frame.requestId === 0 && frame.cmd === 'runtime') {
|
||||
this.runtimeFailure = error;
|
||||
this.rejectControlPending(error);
|
||||
this.rejectRecvPending(error);
|
||||
this.rejectLoginWaiters(error);
|
||||
this.emit('runtimeError', error);
|
||||
return;
|
||||
}
|
||||
|
||||
const ack = this.pendingAcks.get(frame.requestId);
|
||||
|
||||
if (ack) {
|
||||
@@ -1363,6 +1390,10 @@ export class QqHookClient extends EventEmitter {
|
||||
}
|
||||
|
||||
private handleRecvFrame(frame: PipeFrame): void {
|
||||
if (this.runtimeFailure) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (frame.op === PipeOp.hello) {
|
||||
this.resolveHello(frame, true);
|
||||
return;
|
||||
|
||||
Reference in new issue
Block a user