Skip to content

Commit

Permalink
Add a shutdown timeout to prevent hanging forever
Browse files Browse the repository at this point in the history
  • Loading branch information
JulienVig committed Jul 23, 2024
1 parent a004014 commit 81cdae0
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions discojs/src/client/decentralized/peer_pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ export class PeerPool {
) {}

async shutdown (): Promise<void> {
console.info(`[${this.id}] shutdown their peers`)
console.info(`[${this.id}] is shutting down all its connections`)

await Promise.all(this.peers.valueSeq().map((peer) => peer.disconnect()))
// Add a timeout o.w. the promise hangs forever if the other peer is already disconnected
await Promise.race([
Promise.all(this.peers.valueSeq().map((peer) => peer.disconnect())),
new Promise((res, _) => setTimeout(res, 1000)) // Wait for other peers to finish
])
this.peers = Map()
}

Expand Down

0 comments on commit 81cdae0

Please sign in to comment.