Skip to content

Commit

Permalink
deps: update any-signal to 4.x.x (libp2p#453)
Browse files Browse the repository at this point in the history
`any-signal` can now remove the event listeners it installs preventing a source of memory leaks.
  • Loading branch information
achingbrain authored Apr 13, 2023
1 parent 4a4e93e commit 852d757
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 11 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
"lint": "aegir lint",
"build": "aegir build",
"generate": "protons ./src/message/dht.proto",
"test": "aegir test -t node -f \"./dist/test/kad-dht.spec.js\"",
"test": "aegir test",
"test:node": "aegir test -t node --cov",
"test:chrome": "aegir test -t browser --cov",
"test:chrome-webworker": "aegir test -t webworker",
Expand Down Expand Up @@ -161,7 +161,7 @@
"@libp2p/topology": "^4.0.0",
"@multiformats/multiaddr": "^12.0.0",
"abortable-iterator": "^4.0.2",
"any-signal": "^3.0.0",
"any-signal": "^4.1.1",
"datastore-core": "^9.0.1",
"hashlru": "^2.3.0",
"interface-datastore": "^8.0.0",
Expand Down
18 changes: 10 additions & 8 deletions src/query-self.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,17 @@ export class QuerySelf implements Startable {
_querySelf (): void {
Promise.resolve().then(async () => {
const timeoutController = new TimeoutController(this.queryTimeout)
this.controller = new AbortController()
const signal = anySignal([this.controller.signal, timeoutController.signal])

// this controller will get used for lots of dial attempts so make sure we don't cause warnings to be logged
try {
if (setMaxListeners != null) {
setMaxListeners(Infinity, signal)
}
} catch {} // fails on node < 15.4

try {
this.controller = new AbortController()
const signal = anySignal([this.controller.signal, timeoutController.signal])
// this controller will get used for lots of dial attempts so make sure we don't cause warnings to be logged
try {
if (setMaxListeners != null) {
setMaxListeners(Infinity, signal)
}
} catch {} // fails on node < 15.4
const found = await pipe(
this.peerRouting.getClosestPeers(this.components.peerId.toBytes(), {
signal
Expand All @@ -96,6 +97,7 @@ export class QuerySelf implements Startable {
} finally {
this.timeoutId = setTimeout(this._querySelf.bind(this), this.interval)
timeoutController.clear()
signal.clear()
}
}).catch(err => {
this.log('query error', err)
Expand Down
2 changes: 2 additions & 0 deletions src/query/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ export class QueryManager implements Startable {
throw err
}
} finally {
signal.clear()

this.controllers.delete(abortController)

if (timeoutController != null) {
Expand Down
1 change: 1 addition & 0 deletions src/query/query-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ export async function * queryPath (options: QueryPathOptions): AsyncGenerator<Qu
})
}
} finally {
compoundSignal.clear()
timeout?.clear()
}
}, {
Expand Down
2 changes: 1 addition & 1 deletion test/network.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('Network', () => {
const msg = new Message(MESSAGE_TYPE.PING, uint8ArrayFromString('hello'), 0)

// mock it
dht.components.connectionManager.openConnection = async (peer: PeerId | Multiaddr) => {
dht.components.connectionManager.openConnection = async (peer: PeerId | Multiaddr | Multiaddr[]) => {
// @ts-expect-error incomplete implementation
const connection: Connection = {
newStream: async (protocols: string | string[]) => {
Expand Down

0 comments on commit 852d757

Please sign in to comment.