Skip to content

Commit 852d757

Browse files
authored
deps: update any-signal to 4.x.x (libp2p#453)
`any-signal` can now remove the event listeners it installs preventing a source of memory leaks.
1 parent 4a4e93e commit 852d757

File tree

5 files changed

+16
-11
lines changed

5 files changed

+16
-11
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@
131131
"lint": "aegir lint",
132132
"build": "aegir build",
133133
"generate": "protons ./src/message/dht.proto",
134-
"test": "aegir test -t node -f \"./dist/test/kad-dht.spec.js\"",
134+
"test": "aegir test",
135135
"test:node": "aegir test -t node --cov",
136136
"test:chrome": "aegir test -t browser --cov",
137137
"test:chrome-webworker": "aegir test -t webworker",
@@ -161,7 +161,7 @@
161161
"@libp2p/topology": "^4.0.0",
162162
"@multiformats/multiaddr": "^12.0.0",
163163
"abortable-iterator": "^4.0.2",
164-
"any-signal": "^3.0.0",
164+
"any-signal": "^4.1.1",
165165
"datastore-core": "^9.0.1",
166166
"hashlru": "^2.3.0",
167167
"interface-datastore": "^8.0.0",

src/query-self.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,17 @@ export class QuerySelf implements Startable {
7272
_querySelf (): void {
7373
Promise.resolve().then(async () => {
7474
const timeoutController = new TimeoutController(this.queryTimeout)
75+
this.controller = new AbortController()
76+
const signal = anySignal([this.controller.signal, timeoutController.signal])
77+
78+
// this controller will get used for lots of dial attempts so make sure we don't cause warnings to be logged
79+
try {
80+
if (setMaxListeners != null) {
81+
setMaxListeners(Infinity, signal)
82+
}
83+
} catch {} // fails on node < 15.4
7584

7685
try {
77-
this.controller = new AbortController()
78-
const signal = anySignal([this.controller.signal, timeoutController.signal])
79-
// this controller will get used for lots of dial attempts so make sure we don't cause warnings to be logged
80-
try {
81-
if (setMaxListeners != null) {
82-
setMaxListeners(Infinity, signal)
83-
}
84-
} catch {} // fails on node < 15.4
8586
const found = await pipe(
8687
this.peerRouting.getClosestPeers(this.components.peerId.toBytes(), {
8788
signal
@@ -96,6 +97,7 @@ export class QuerySelf implements Startable {
9697
} finally {
9798
this.timeoutId = setTimeout(this._querySelf.bind(this), this.interval)
9899
timeoutController.clear()
100+
signal.clear()
99101
}
100102
}).catch(err => {
101103
this.log('query error', err)

src/query/manager.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ export class QueryManager implements Startable {
184184
throw err
185185
}
186186
} finally {
187+
signal.clear()
188+
187189
this.controllers.delete(abortController)
188190

189191
if (timeoutController != null) {

src/query/query-path.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ export async function * queryPath (options: QueryPathOptions): AsyncGenerator<Qu
168168
})
169169
}
170170
} finally {
171+
compoundSignal.clear()
171172
timeout?.clear()
172173
}
173174
}, {

test/network.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ describe('Network', () => {
5454
const msg = new Message(MESSAGE_TYPE.PING, uint8ArrayFromString('hello'), 0)
5555

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

0 commit comments

Comments
 (0)