Skip to content

race condition causes @libp2p/tcp close method to hang #2760

@tabcat

Description

@tabcat
  • Version: latest

  • Subsystem: @libp2p/tcp

Severity: Low

Description

The hanging promise is to do with shutting down the tcp server

await new Promise<void>((resolve, reject) => {
this.server.close(err => {
if (err != null) {
reject(err)
return
}
resolve()
})

For server.close to resolve all socket connections must be closed.
The connections are closed inside of the close method.

this.connections.forEach(conn => {
conn.abort(err)
})

The connections are tracked in the onSocket listener method with the property this.connections.

this.log('new inbound connection %s', maConn.remoteAddr)
this.context.upgrader.upgradeInbound(maConn)
.then((conn) => {
this.log('inbound connection upgraded %s', maConn.remoteAddr)
this.connections.add(maConn)

The race condition is triggered when close is called before the connection is upgraded. At that point the socket is not added to this.connections.

Steps to reproduce the error:

https://github.com/tabcat/delay5

Metadata

Metadata

Assignees

No one assigned

    Labels

    need/triageNeeds initial labeling and prioritization

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions