fix: hanging close promise (#140)

This commit is contained in:
Cayman
2021-02-03 07:03:09 -05:00
committed by GitHub
parent 8661c09b69
commit 3813100438
2 changed files with 36 additions and 1 deletions

View File

@ -78,7 +78,10 @@ module.exports = (socket, options) => {
resolve()
}, CLOSE_TIMEOUT)
socket.once('close', () => clearTimeout(timeout))
socket.once('close', () => {
clearTimeout(timeout)
resolve()
})
socket.end(err => {
maConn.timeline.close = Date.now()
if (err) return reject(err)

View File

@ -50,4 +50,36 @@ describe('valid localAddr and remoteAddr', () => {
expect(dialerConn.remoteAddr.toString())
.to.equal(listenerConn.localAddr.toString())
})
it('should handle multiple simultaneous closes', async () => {
// Create a Promise that resolves when a connection is handled
let handled
const handlerPromise = new Promise(resolve => { handled = resolve })
const handler = conn => handled(conn)
// Create a listener with the handler
const listener = tcp.createListener(handler)
// Listen on the multi-address
await listener.listen(ma)
const localAddrs = listener.getAddrs()
expect(localAddrs.length).to.equal(1)
// Dial to that address
const dialerConn = await tcp.dial(localAddrs[0])
// Wait for the incoming dial to be handled
await handlerPromise
// Close the listener with two simultaneous calls to `close`
await Promise.race([
new Promise((resolve, reject) => setTimeout(() => reject(new Error('Timed out waiting for connection close')), 500)),
await Promise.all([
dialerConn.close(),
dialerConn.close()
])
])
})
})