Skip to content
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

fix(@libp2p/tcp): race condition in onSocket #2763

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
8 changes: 8 additions & 0 deletions packages/transport-tcp/src/listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export class TCPListener extends TypedEventEmitter<ListenerEvents> implements Li
private readonly server: net.Server
/** Keep track of open connections to destroy in case of timeout */
private readonly connections = new Set<MultiaddrConnection>()
private readonly maConnections = new Set<MultiaddrConnection>()
private status: Status = { code: TCPListenerStatusCode.INACTIVE }
private metrics?: TCPListenerMetrics
private addr: string
Expand Down Expand Up @@ -195,11 +196,13 @@ export class TCPListener extends TypedEventEmitter<ListenerEvents> implements Li
}

this.log('new inbound connection %s', maConn.remoteAddr)
this.maConnections.add(maConn)

this.context.upgrader.upgradeInbound(maConn)
.then((conn) => {
this.log('inbound connection upgraded %s', maConn.remoteAddr)
this.connections.add(maConn)
this.maConnections.delete(maConn)
tabcat marked this conversation as resolved.
Show resolved Hide resolved

socket.once('close', () => {
this.connections.delete(maConn)
Expand Down Expand Up @@ -307,6 +310,11 @@ export class TCPListener extends TypedEventEmitter<ListenerEvents> implements Li
conn.abort(err)
})

// cleanup connections that have not been upgraded
this.maConnections.forEach(conn => {
conn.abort(err)
})

// shut down the server socket, permanently
await this.pause(true)
}
Expand Down
Loading