Skip to content

Commit

Permalink
fix: Record the missing-error-handler timers in TCPHelper, UDPHelper,…
Browse files Browse the repository at this point in the history
… and TelnetHelper, and properly clear them when destroy() is called. (#78)
  • Loading branch information
jswalden committed Apr 24, 2024
1 parent 71e5b33 commit 8629820
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/helpers/tcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export class TCPHelper extends EventEmitter<TCPHelperEvents> {
#destroyed = false
#lastStatus: InstanceStatus | undefined
#reconnectTimer: NodeJS.Timeout | undefined
#missingErrorHandlerTimer: NodeJS.Timeout | undefined

get isConnected(): boolean {
return this.#connected
Expand Down Expand Up @@ -123,7 +124,8 @@ export class TCPHelper extends EventEmitter<TCPHelperEvents> {
// Let caller install event handlers first
setImmediate(() => this.connect())

setTimeout(() => {
this.#missingErrorHandlerTimer = setTimeout(() => {
this.#missingErrorHandlerTimer = undefined
if (!this.#destroyed && !this.listenerCount('error')) {
// The socket is active and has no listeners. Log an error for the module devs!
console.error(`Danger: TCP client for ${this.#host}:${this.#port} is missing an error handler!`)
Expand Down Expand Up @@ -175,6 +177,11 @@ export class TCPHelper extends EventEmitter<TCPHelperEvents> {

if (this.#reconnectTimer !== undefined) {
clearTimeout(this.#reconnectTimer)
this.#reconnectTimer = undefined
}
if (this.#missingErrorHandlerTimer !== undefined) {
clearTimeout(this.#missingErrorHandlerTimer)
this.#missingErrorHandlerTimer = undefined
}

this._socket.removeAllListeners()
Expand Down
9 changes: 8 additions & 1 deletion src/helpers/telnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export type TelnetHelperOptions = TCPHelperOptions
export class TelnetHelper extends EventEmitter<TelnetHelperEvents> {
readonly #tcp: TCPHelper
readonly #stream: TelnetStream
#missingErrorHandlerTimer: NodeJS.Timeout | undefined

get isConnected(): boolean {
return this.#tcp.isConnected
Expand Down Expand Up @@ -68,7 +69,8 @@ export class TelnetHelper extends EventEmitter<TelnetHelperEvents> {
this.#stream.on('data', (data) => this.emit('data', data))
this.#stream.on('drain', () => this.emit('drain'))

setTimeout(() => {
this.#missingErrorHandlerTimer = setTimeout(() => {
this.#missingErrorHandlerTimer = undefined
if (!this.isDestroyed && !this.listenerCount('error')) {
// The socket is active and has no listeners. Log an error for the module devs!
console.error(`Danger: Telnet client for ${host}:${port} is missing an error handler!`)
Expand All @@ -86,6 +88,11 @@ export class TelnetHelper extends EventEmitter<TelnetHelperEvents> {
destroy(): void {
this.#tcp.destroy()

if (this.#missingErrorHandlerTimer !== undefined) {
clearTimeout(this.#missingErrorHandlerTimer)
this.#missingErrorHandlerTimer = undefined
}

this.#stream.removeAllListeners()
this.#stream.destroy()
}
Expand Down
8 changes: 7 additions & 1 deletion src/helpers/udp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export class UDPHelper extends EventEmitter<UDPHelperEvents> {

#destroyed = false
#lastStatus: InstanceStatus | undefined
#missingErrorHandlerTimer: NodeJS.Timeout | undefined

get isDestroyed(): boolean {
return this.#destroyed
Expand Down Expand Up @@ -117,7 +118,7 @@ export class UDPHelper extends EventEmitter<UDPHelperEvents> {

this.#socket.on('message', (data) => this.emit('data', data))

setTimeout(() => {
this.#missingErrorHandlerTimer = setTimeout(() => {
if (!this.#destroyed && !this.listenerCount('error')) {
// The socket is active and has no listeners. Log an error for the module devs!
console.error(`Danger: UDP socket for ${this.#host}:${this.#port} is missing an error handler!`)
Expand Down Expand Up @@ -150,6 +151,11 @@ export class UDPHelper extends EventEmitter<UDPHelperEvents> {
destroy(): void {
this.#destroyed = true

if (this.#missingErrorHandlerTimer !== undefined) {
clearTimeout(this.#missingErrorHandlerTimer)
this.#missingErrorHandlerTimer = undefined
}

this.#socket.removeAllListeners()
this.#socket.close()
this.removeAllListeners()
Expand Down

0 comments on commit 8629820

Please sign in to comment.