Skip to content

Commit

Permalink
fix: only close stream if it is open (#2823)
Browse files Browse the repository at this point in the history
Adds a guard to the `.close` operation similar to the one on
`.abort` that ensures we only close the stream if it is open.

The individual `.closeRead`/`.closeWrite` operations already guard
on the read/write status of the stream so there's no functional
change, we just avoid a bit more async work as those methods
return promises.
  • Loading branch information
achingbrain authored Nov 15, 2024
1 parent 4db0645 commit 3098232
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/integration-tests/test/circuit-relay.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -519,8 +519,8 @@ describe('circuit-relay', () => {
await deferred.promise

// should have closed connections to remote and to relay
expect(events[0].detail.remotePeer.toString()).to.equal(relay1.peerId.toString())
expect(events[1].detail.remotePeer.toString()).to.equal(remote.peerId.toString())
expect(events[0].detail.remotePeer.toString()).to.equal(remote.peerId.toString())
expect(events[1].detail.remotePeer.toString()).to.equal(relay1.peerId.toString())
})

it('should mark an outgoing relayed connection as limited', async () => {
Expand Down
4 changes: 4 additions & 0 deletions packages/utils/src/abstract-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,10 @@ export abstract class AbstractStream implements Stream {

// Close for both Reading and Writing
async close (options?: AbortOptions): Promise<void> {
if (this.status !== 'open') {
return
}

this.log.trace('closing gracefully')

this.status = 'closing'
Expand Down

0 comments on commit 3098232

Please sign in to comment.