Skip to content

Commit

Permalink
fix: silence max listeners warning (#2417)
Browse files Browse the repository at this point in the history
When recreating the dial queue shutdown controller, we must also
increase the number of listeners it can have, otherwise warnings
are printed when there are more than 10 peers in the dial queue.
  • Loading branch information
achingbrain committed Feb 19, 2024
1 parent fb7c51c commit bedfd0a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
10 changes: 10 additions & 0 deletions packages/interface/src/event-target.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { setMaxListeners } from './events.js'

export interface EventCallback<EventType> { (evt: EventType): void }
export interface EventObject<EventType> { handleEvent: EventCallback<EventType> }
export type EventHandler<EventType> = EventCallback<EventType> | EventObject<EventType>
Expand Down Expand Up @@ -34,6 +36,14 @@ export interface TypedEventTarget <EventMap extends Record<string, any>> extends
export class TypedEventEmitter<EventMap extends Record<string, any>> extends EventTarget implements TypedEventTarget<EventMap> {
#listeners = new Map<any, Listener[]>()

constructor () {
super()

// silence MaxListenersExceededWarning warning on Node.js, this is a red
// herring almost all of the time
setMaxListeners(Infinity, this)
}

listenerCount (type: string): number {
const listeners = this.#listeners.get(type)

Expand Down
4 changes: 2 additions & 2 deletions packages/libp2p/src/connection-manager/dial-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,9 @@ export class DialQueue {
this.dialTimeout = init.dialTimeout ?? defaultOptions.dialTimeout
this.connections = init.connections ?? new PeerMap()
this.log = components.logger.forComponent('libp2p:connection-manager:dial-queue')

this.components = components
this.shutDownController = new AbortController()

this.shutDownController = new AbortController()
setMaxListeners(Infinity, this.shutDownController.signal)

for (const [key, value] of Object.entries(init.resolvers ?? {})) {
Expand All @@ -103,6 +102,7 @@ export class DialQueue {

start (): void {
this.shutDownController = new AbortController()
setMaxListeners(Infinity, this.shutDownController.signal)
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/libp2p/src/upgrader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ export class DefaultUpgrader implements Upgrader {
throw new Error(`no crypto module found for ${protocol}`)
}

connection.log('encrypting outbound connection to %p using %p', remotePeerId)
connection.log('encrypting outbound connection to %p using %s', remotePeerId, encrypter)

return {
...await encrypter.secureOutbound(this.components.peerId, stream, remotePeerId),
Expand Down
8 changes: 4 additions & 4 deletions packages/transport-websockets/test/compliance.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ describe('interface-transport compliance', () => {
logger: defaultLogger()
})
const addrs = [
multiaddr('/ip4/127.0.0.1/tcp/9091/ws'),
multiaddr('/ip4/127.0.0.1/tcp/9092/ws'),
multiaddr('/dns4/ipfs.io/tcp/9092/ws'),
multiaddr('/dns4/ipfs.io/tcp/9092/wss')
multiaddr('/ip4/127.0.0.1/tcp/9096/ws'),
multiaddr('/ip4/127.0.0.1/tcp/9097/ws'),
multiaddr('/dns4/ipfs.io/tcp/9098/ws'),
multiaddr('/dns4/ipfs.io/tcp/9099/wss')
]

let delayMs = 0
Expand Down

0 comments on commit bedfd0a

Please sign in to comment.