Skip to content

Commit

Permalink
fix: explicitly close streams when connnections close
Browse files Browse the repository at this point in the history
Make sure we don't leave streams open.

Depends on:

- libp2p/js-libp2p-interfaces#214
- libp2p/js-libp2p-mplex#170
  • Loading branch information
achingbrain committed May 19, 2022
1 parent 35f9c0c commit f6ad536
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/connection-manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,16 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
*/
async _close () {
// Close all connections we're tracking
const tasks = []
const tasks: Array<Promise<void>> = []
for (const connectionList of this.connections.values()) {
for (const connection of connectionList) {
tasks.push(connection.close())
tasks.push((async () => {
try {
await connection.close()
} catch (err) {
log.error(err)
}
})())
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/upgrader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,9 +382,11 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
getStreams: () => muxer != null ? muxer.streams : errConnectionNotMultiplexed(),
close: async () => {
await maConn.close()
// Ensure remaining streams are aborted
// Ensure remaining streams are closed
if (muxer != null) {
muxer.streams.map(stream => stream.abort())
await Promise.all(muxer.streams.map(async stream => {
await stream.close()
}))
}
}
})
Expand Down

0 comments on commit f6ad536

Please sign in to comment.