Skip to content

Commit

Permalink
fix: clear signal to avoid memory leaks (#1728)
Browse files Browse the repository at this point in the history
Follow up from #1708 (comment)
  • Loading branch information
maschad authored May 3, 2023
1 parent 2a9617b commit 5c643c3
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 21 deletions.
2 changes: 2 additions & 0 deletions src/autonat/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,8 @@ export class AutonatService implements Startable {
)
} catch (err) {
log.error(err)
} finally {
signal.clear()
}
}

Expand Down
15 changes: 6 additions & 9 deletions src/identify/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,18 +305,14 @@ class DefaultIdentifyService implements Startable, IdentifyService {
}

async _identify (connection: Connection, options: AbortOptions = {}): Promise<Identify> {
let signal = options.signal
let stream: Stream | undefined

// create a timeout if no abort signal passed
if (signal == null) {
signal = anySignal([AbortSignal.timeout(this.timeout), options.signal])
const signal = anySignal([AbortSignal.timeout(this.timeout), options?.signal])

try {
// fails on node < 15.4
setMaxListeners?.(Infinity, signal)
} catch {}
}
try {
// fails on node < 15.4
setMaxListeners?.(Infinity, signal)
} catch {}

try {
stream = await connection.newStream([this.identifyProtocolStr], {
Expand Down Expand Up @@ -348,6 +344,7 @@ class DefaultIdentifyService implements Startable, IdentifyService {
if (stream != null) {
stream.close()
}
signal.clear()
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/peer-routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,11 @@ export class DefaultPeerRouting implements PeerRouting, Startable {
return
}

try {
this.abortController = new AbortController()
const signal = anySignal([this.abortController.signal, AbortSignal.timeout(this.refreshManagerInit.timeout ?? 10e3)])
this.abortController = new AbortController()

const signal = anySignal([this.abortController.signal, AbortSignal.timeout(this.refreshManagerInit.timeout ?? 10e3)])

try {
// this controller may be used while dialing lots of peers so prevent MaxListenersExceededWarning
// appearing in the console
try {
Expand All @@ -121,6 +122,7 @@ export class DefaultPeerRouting implements PeerRouting, Startable {
} finally {
this.abortController?.abort()
this.abortController = undefined
signal.clear()
}
}

Expand Down
15 changes: 6 additions & 9 deletions src/ping/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,14 @@ class DefaultPingService implements Startable, PingService {
const start = Date.now()
const data = randomBytes(PING_LENGTH)
const connection = await this.components.connectionManager.openConnection(peer, options)
let signal = options.signal
let stream: Stream | undefined

// create a timeout if no abort signal passed
if (signal == null) {
signal = anySignal([AbortSignal.timeout(this.timeout), options.signal])
const signal = anySignal([AbortSignal.timeout(this.timeout), options?.signal])

try {
// fails on node < 15.4
setMaxListeners?.(Infinity, signal)
} catch {}
}
try {
// fails on node < 15.4
setMaxListeners?.(Infinity, signal)
} catch {}

try {
stream = await connection.newStream([this.protocol], {
Expand All @@ -134,6 +130,7 @@ class DefaultPingService implements Startable, PingService {
if (stream != null) {
stream.close()
}
signal.clear()
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/upgrader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ export class DefaultUpgrader implements Upgrader {
})
} finally {
this.components.connectionManager.afterUpgradeInbound()
signal.clear()
}
}

Expand Down

0 comments on commit 5c643c3

Please sign in to comment.