Skip to content

Commit

Permalink
fix: send data during graceful close.
Browse files Browse the repository at this point in the history
When a stream is closed gracefully, it's status goes from `'open'`
to `'closing'` then to either `'closed'`, `'aborted'` or `'reset'`.

While it's `'closing'` we should still try to send any queued data,
this can be aborted by calling `.abort` on the stream or by the
signal passed to `.close` firing the `'abort'` event.

This change makes the tests added in libp2p/js-libp2p#2398
pass.
  • Loading branch information
achingbrain committed Feb 7, 2024
1 parent 1f9173b commit 30dbd5c
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,14 @@ export class YamuxStream extends AbstractStream {
while (buf.byteLength !== 0) {
// wait for the send window to refill
if (this.sendWindowCapacity === 0) {
this.log?.trace('wait for send window capacity', this.status)
await this.waitForSendWindowCapacity(options)
}

// check we didn't close while waiting for send window capacity
if (this.status !== 'open') {
return
// check we didn't close while waiting for send window capacity
if (this.status === 'closed' || this.status === 'aborted' || this.status === 'reset') {
this.log?.trace('%s while waiting for send window capacity', this.status)
return
}
}

// send as much as we can
Expand Down

0 comments on commit 30dbd5c

Please sign in to comment.