From f8a90ab7301d995108b95639e5a3c81b0a6c2844 Mon Sep 17 00:00:00 2001 From: Dodothereal <129273127+Dodothereal@users.noreply.github.com> Date: Thu, 23 Jul 2026 11:52:38 +0200 Subject: [PATCH] fix: connection lifecycle fixes for socket death and close - In fail(): destroy socket with error, set sock=null, shookHands=false, ready=null, and clear buffers. - In close(): clear buffers after closing socket. - In connect(): only call fail on 'close' event if socket is still open to avoid double-fail. Fixes #6 Assisted-by: Claude Code --- src/connection.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/connection.ts b/src/connection.ts index f1ff1f5..ddf612a 100644 --- a/src/connection.ts +++ b/src/connection.ts @@ -84,7 +84,11 @@ export class DfConnection { this.fail(err); reject(err); }); - sock.on('close', () => this.fail(new Error('connection closed'))); + sock.on('close', () => { + if (this.sock) { + this.fail(new Error('connection closed')); + } + }); }); return this.ready; } @@ -165,10 +169,18 @@ export class DfConnection { } private fail(err: Error): void { + if (this.sock) { + this.sock.destroy(err); + this.sock = null; + } + this.shookHands = false; + this.ready = null; const pending = this.active ? [this.active, ...this.queue] : [...this.queue]; this.active = null; this.queue = []; for (const call of pending) call.reject(err); + this.buf = Buffer.alloc(0); + this.textFrames = []; } close(): void { @@ -178,5 +190,7 @@ export class DfConnection { } this.shookHands = false; this.ready = null; + this.buf = Buffer.alloc(0); + this.textFrames = []; } }