libp2p / js-libp2p

The JavaScript Implementation of libp2p networking stack.
https://libp2p.github.io/js-libp2p/
Other
2.34k stars 447 forks source link

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

Closed tabcat closed 1 month ago

tabcat commented 1 month ago

Severity: Low

Description

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

https://github.com/libp2p/js-libp2p/blob/1cbfd6cf392b0db80f744cfe4a0841ed80760c84/packages/transport-tcp/src/listener.ts#L364-L372

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

https://github.com/libp2p/js-libp2p/blob/1cbfd6cf392b0db80f744cfe4a0841ed80760c84/packages/transport-tcp/src/listener.ts#L306-L308

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

https://github.com/libp2p/js-libp2p/blob/1cbfd6cf392b0db80f744cfe4a0841ed80760c84/packages/transport-tcp/src/listener.ts#L197-L202

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