Skip to content

fix: event handlers error #761

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"p-fifo": "^1.0.0",
"p-settle": "^4.0.1",
"peer-id": "^0.14.2",
"proper-event-emitter": "^1.0.0",
"protons": "^2.0.0",
"retimer": "^2.0.0",
"sanitize-filename": "^1.6.3",
Expand Down
2 changes: 1 addition & 1 deletion src/circuit/listener.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const EventEmitter = require('events')
const EventEmitter = require('proper-event-emitter')
const multiaddr = require('multiaddr')

const debug = require('debug')
Expand Down
3 changes: 2 additions & 1 deletion src/connection-manager/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const mergeOptions = require('merge-options')
const LatencyMonitor = require('./latency-monitor')
const retimer = require('retimer')

const { EventEmitter } = require('events')
const EventEmitter = require('proper-event-emitter')

const PeerId = require('peer-id')

Expand Down Expand Up @@ -199,6 +199,7 @@ class ConnectionManager extends EventEmitter {
const storedConn = this.connections.get(peerIdStr)

this.emit('peer:connect', connection)

if (storedConn) {
storedConn.push(connection)
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { EventEmitter } = require('events')
const EventEmitter = require('proper-event-emitter')
const debug = require('debug')
const globalThis = require('ipfs-utils/src/globalthis')
const log = debug('libp2p')
Expand Down
2 changes: 1 addition & 1 deletion src/peer-store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const debug = require('debug')
const log = debug('libp2p:peer-store')
log.error = debug('libp2p:peer-store:error')

const { EventEmitter } = require('events')
const EventEmitter = require('proper-event-emitter')
const PeerId = require('peer-id')

const AddressBook = require('./address-book')
Expand Down
98 changes: 98 additions & 0 deletions test/core/event-handling-error.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
'use strict'
/* eslint-env mocha */

const chai = require('chai')
chai.use(require('dirty-chai'))
const { expect } = chai

const errCode = require('err-code')
const { isNode, isWebWorker } = require('ipfs-utils/src/env')

const multiaddr = require('multiaddr')
const PeerId = require('peer-id')

const peerUtils = require('../utils/creators/peer')
const mockConnection = require('../utils/mockConnection')
const baseOptions = require('../utils/base-options.browser')
const { MULTIADDRS_WEBSOCKETS } = require('../fixtures/browser')

const relayAddr = MULTIADDRS_WEBSOCKETS[0]
const code = 'HANDLER_ERROR'
const errorMsg = 'handle error'

describe('event handlers error', () => {
// TODO: Need a way of catching the error in the process and absorb it
if (isWebWorker) {
return
}

let libp2p
let peerId

before(async () => {
peerId = await PeerId.create()
})

beforeEach(async () => {
[libp2p] = await peerUtils.createPeer({
config: {
modules: baseOptions.modules,
addresses: {
listen: [multiaddr(`${relayAddr}/p2p-circuit`)]
}
},
started: true
})
})

afterEach(async () => {
libp2p && await libp2p.stop()
})

it('should throw on "peer:connect" event handler error', async () => {
const p = catchGlobalError()

libp2p.connectionManager.on('peer:connect', () => {
throw errCode(new Error(errorMsg), code)
})

const connection = await mockConnection()
libp2p.connectionManager.onConnect(connection)

await p
})

it('should throw on "peer:discovery" event handler error', async () => {
const p = catchGlobalError()

libp2p.on('peer:discovery', () => {
throw errCode(new Error(errorMsg), code)
})

const ma = multiaddr('/ip4/127.0.0.1/tcp/0')
libp2p.peerStore.addressBook.add(peerId, [ma])

await p
})
})

const catchGlobalError = () => {
return new Promise((resolve) => {
if (isNode) {
const originalException = process.listeners('uncaughtException').pop()

originalException && process.removeListener('uncaughtException', originalException)
process.once('uncaughtException', (err) => {
expect(err).to.exist()
expect(err.code).to.eql(code)

originalException && process.listeners('uncaughtException').push(originalException)
resolve()
})
} else {
window.onerror = () => {
resolve()
}
}
})
}