Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Hanging promise in connection.close() #139

Closed
wemeetagain opened this issue Jan 29, 2021 · 0 comments · Fixed by #140
Closed

Hanging promise in connection.close() #139

wemeetagain opened this issue Jan 29, 2021 · 0 comments · Fixed by #140
Labels
kind/bug A bug in existing code (including security flaws) status/ready Ready to be worked

Comments

@wemeetagain
Copy link
Member

wemeetagain commented Jan 29, 2021

Calling conn.close() twice (without awaiting) will result in the second call hanging.

The problem is that if socket.end(cb) is called multiple times before the callback is executed, only the first callback will be executed.
Since the promise resolves in the callback, only the first conn.close() will resolve.

The socket.on('close', cb) callback will be triggered for both invocations, cancelling the timeout that provides the only other path to resolve the promise.

Here is a realistic-ish testcase showing how this may be an issue:

const Libp2p = require('libp2p')
const TCP = require('libp2p-tcp')
const MPLEX = require('libp2p-mplex')
const { NOISE } = require('libp2p-noise')

async function createNode() {
  return Libp2p.create({
    modules: {
      transport: [TCP],
      streamMuxer: [MPLEX],
      connEncryption: [NOISE],
    },
    addresses: {
      listen: ["/ip4/127.0.0.1/tcp/0"],
    },
  })
}

(async function () {
  // create nodes
  const nodes = await Promise.all(Array.from({length: 2}, createNode))

  // start nodes
  await Promise.all(nodes.map(n => n.start()))

  // connect node 0 to node 1
  nodes[0].peerStore.addressBook.add(nodes[1].peerId, nodes[1].multiaddrs)
  await nodes[0].dial(nodes[1].peerId)

  // hangUp and stop
  nodes[0].hangUp(nodes[1].peerId) // not awaited
  await Promise.all(nodes.map(n => n.stop()))

  // will hang, never complete
  console.log('will hang, never complete')
})()
@vasco-santos vasco-santos added kind/bug A bug in existing code (including security flaws) status/ready Ready to be worked labels Feb 1, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
kind/bug A bug in existing code (including security flaws) status/ready Ready to be worked
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants