fix: MaxListenersExceeded warning (#1297)

Where we create signals that are passed down the stack, increase the max listeners to prevent warnings in the console.
This commit is contained in:
Alex Potsides 2022-07-17 08:25:21 +00:00 committed by GitHub
parent ba56c64662
commit 627b8bf87c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 67 additions and 1 deletions

View File

@ -20,6 +20,7 @@ import type { Connection } from '@libp2p/interface-connection'
import type { RelayConfig } from '../index.js' import type { RelayConfig } from '../index.js'
import { abortableDuplex } from 'abortable-iterator' import { abortableDuplex } from 'abortable-iterator'
import { TimeoutController } from 'timeout-abort-controller' import { TimeoutController } from 'timeout-abort-controller'
import { setMaxListeners } from 'events'
const log = logger('libp2p:circuit') const log = logger('libp2p:circuit')
@ -64,6 +65,11 @@ export class Circuit implements Transport, Initializable {
const { connection, stream } = data const { connection, stream } = data
const controller = new TimeoutController(this._init.hop.timeout) const controller = new TimeoutController(this._init.hop.timeout)
try {
// fails on node < 15.4
setMaxListeners?.(Infinity, controller.signal)
} catch {}
try { try {
const source = abortableDuplex(stream, controller.signal) const source = abortableDuplex(stream, controller.signal)
const streamHandler = new StreamHandler({ const streamHandler = new StreamHandler({

View File

@ -2,6 +2,7 @@ import type { PeerInfo } from '@libp2p/interface-peer-info'
import { logger } from '@libp2p/logger' import { logger } from '@libp2p/logger'
import type { Components } from '@libp2p/components' import type { Components } from '@libp2p/components'
import { TimeoutController } from 'timeout-abort-controller' import { TimeoutController } from 'timeout-abort-controller'
import { setMaxListeners } from 'events'
const log = logger('libp2p:dialer:auto-dialer') const log = logger('libp2p:dialer:auto-dialer')
@ -44,6 +45,11 @@ export class AutoDialer {
const controller = new TimeoutController(this.dialTimeout) const controller = new TimeoutController(this.dialTimeout)
try {
// fails on node < 15.4
setMaxListeners?.(Infinity, controller.signal)
} catch {}
void this.components.getConnectionManager().openConnection(peer.id, { void this.components.getConnectionManager().openConnection(peer.id, {
signal: controller.signal signal: controller.signal
}) })

View File

@ -331,6 +331,11 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
this.connectOnStartupController?.clear() this.connectOnStartupController?.clear()
this.connectOnStartupController = new TimeoutController(this.startupReconnectTimeout) this.connectOnStartupController = new TimeoutController(this.startupReconnectTimeout)
try {
// fails on node < 15.4
setMaxListeners?.(Infinity, this.connectOnStartupController.signal)
} catch {}
await Promise.all( await Promise.all(
keepAlivePeers.map(async peer => { keepAlivePeers.map(async peer => {
await this.openConnection(peer, { await this.openConnection(peer, {
@ -510,6 +515,11 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
if (options?.signal == null) { if (options?.signal == null) {
timeoutController = new TimeoutController(this.dialTimeout) timeoutController = new TimeoutController(this.dialTimeout)
options.signal = timeoutController.signal options.signal = timeoutController.signal
try {
// fails on node < 15.4
setMaxListeners?.(Infinity, timeoutController.signal)
} catch {}
} }
try { try {

View File

@ -14,6 +14,7 @@ import { abortableDuplex } from 'abortable-iterator'
import { pipe } from 'it-pipe' import { pipe } from 'it-pipe'
import first from 'it-first' import first from 'it-first'
import { TimeoutController } from 'timeout-abort-controller' import { TimeoutController } from 'timeout-abort-controller'
import { setMaxListeners } from 'events'
const log = logger('libp2p:fetch') const log = logger('libp2p:fetch')
@ -99,6 +100,11 @@ export class FetchService implements Startable {
if (signal == null) { if (signal == null) {
timeoutController = new TimeoutController(this.init.timeout) timeoutController = new TimeoutController(this.init.timeout)
signal = timeoutController.signal signal = timeoutController.signal
try {
// fails on node < 15.4
setMaxListeners?.(Infinity, timeoutController.signal)
} catch {}
} }
try { try {

View File

@ -27,6 +27,7 @@ import { TimeoutController } from 'timeout-abort-controller'
import type { AbortOptions } from '@libp2p/interfaces' import type { AbortOptions } from '@libp2p/interfaces'
import { abortableDuplex } from 'abortable-iterator' import { abortableDuplex } from 'abortable-iterator'
import type { Duplex } from 'it-stream-types' import type { Duplex } from 'it-stream-types'
import { setMaxListeners } from 'events'
const log = logger('libp2p:identify') const log = logger('libp2p:identify')
@ -164,8 +165,13 @@ export class IdentifyService implements Startable {
const protocols = await this.components.getPeerStore().protoBook.get(this.components.getPeerId()) const protocols = await this.components.getPeerStore().protoBook.get(this.components.getPeerId())
const pushes = connections.map(async connection => { const pushes = connections.map(async connection => {
const timeoutController = new TimeoutController(this.init.timeout)
let stream: Stream | undefined let stream: Stream | undefined
const timeoutController = new TimeoutController(this.init.timeout)
try {
// fails on node < 15.4
setMaxListeners?.(Infinity, timeoutController.signal)
} catch {}
try { try {
stream = await connection.newStream([this.identifyPushProtocolStr], { stream = await connection.newStream([this.identifyPushProtocolStr], {
@ -234,6 +240,11 @@ export class IdentifyService implements Startable {
if (signal == null) { if (signal == null) {
timeoutController = new TimeoutController(this.init.timeout) timeoutController = new TimeoutController(this.init.timeout)
signal = timeoutController.signal signal = timeoutController.signal
try {
// fails on node < 15.4
setMaxListeners?.(Infinity, timeoutController.signal)
} catch {}
} }
try { try {
@ -374,6 +385,11 @@ export class IdentifyService implements Startable {
const { connection, stream } = data const { connection, stream } = data
const timeoutController = new TimeoutController(this.init.timeout) const timeoutController = new TimeoutController(this.init.timeout)
try {
// fails on node < 15.4
setMaxListeners?.(Infinity, timeoutController.signal)
} catch {}
try { try {
const publicKey = this.components.getPeerId().publicKey ?? new Uint8Array(0) const publicKey = this.components.getPeerId().publicKey ?? new Uint8Array(0)
const peerData = await this.components.getPeerStore().get(this.components.getPeerId()) const peerData = await this.components.getPeerStore().get(this.components.getPeerId())
@ -425,6 +441,11 @@ export class IdentifyService implements Startable {
const { connection, stream } = data const { connection, stream } = data
const timeoutController = new TimeoutController(this.init.timeout) const timeoutController = new TimeoutController(this.init.timeout)
try {
// fails on node < 15.4
setMaxListeners?.(Infinity, timeoutController.signal)
} catch {}
let message: Identify | undefined let message: Identify | undefined
try { try {
// make stream abortable // make stream abortable

View File

@ -14,6 +14,7 @@ import type { AbortOptions } from '@libp2p/interfaces'
import { abortableDuplex } from 'abortable-iterator' import { abortableDuplex } from 'abortable-iterator'
import { TimeoutController } from 'timeout-abort-controller' import { TimeoutController } from 'timeout-abort-controller'
import type { Stream } from '@libp2p/interface-connection' import type { Stream } from '@libp2p/interface-connection'
import { setMaxListeners } from 'events'
const log = logger('libp2p:ping') const log = logger('libp2p:ping')
@ -90,6 +91,11 @@ export class PingService implements Startable {
if (signal == null) { if (signal == null) {
timeoutController = new TimeoutController(this.init.timeout) timeoutController = new TimeoutController(this.init.timeout)
signal = timeoutController.signal signal = timeoutController.signal
try {
// fails on node < 15.4
setMaxListeners?.(Infinity, timeoutController.signal)
} catch {}
} }
try { try {

View File

@ -20,6 +20,7 @@ import type { Registrar } from '@libp2p/interface-registrar'
import { DEFAULT_MAX_INBOUND_STREAMS, DEFAULT_MAX_OUTBOUND_STREAMS } from './registrar.js' import { DEFAULT_MAX_INBOUND_STREAMS, DEFAULT_MAX_OUTBOUND_STREAMS } from './registrar.js'
import { TimeoutController } from 'timeout-abort-controller' import { TimeoutController } from 'timeout-abort-controller'
import { abortableDuplex } from 'abortable-iterator' import { abortableDuplex } from 'abortable-iterator'
import { setMaxListeners } from 'events'
const log = logger('libp2p:upgrader') const log = logger('libp2p:upgrader')
@ -133,6 +134,11 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
const timeoutController = new TimeoutController(this.inboundUpgradeTimeout) const timeoutController = new TimeoutController(this.inboundUpgradeTimeout)
try {
// fails on node < 15.4
setMaxListeners?.(Infinity, timeoutController.signal)
} catch {}
try { try {
const abortableStream = abortableDuplex(maConn, timeoutController.signal) const abortableStream = abortableDuplex(maConn, timeoutController.signal)
maConn.source = abortableStream.source maConn.source = abortableStream.source
@ -407,6 +413,11 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
controller = new TimeoutController(30000) controller = new TimeoutController(30000)
options.signal = controller.signal options.signal = controller.signal
try {
// fails on node < 15.4
setMaxListeners?.(Infinity, controller.signal)
} catch {}
} }
let { stream, protocol } = await mss.select(protocols, options) let { stream, protocol } = await mss.select(protocols, options)