From 30dbd5c781ac30a75243e6545ee89275377bbccc Mon Sep 17 00:00:00 2001 From: achingbrain Date: Wed, 7 Feb 2024 15:43:47 +0100 Subject: [PATCH] fix: send data during graceful close. 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 https://github.com/libp2p/js-libp2p/pull/2398 pass. --- src/stream.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/stream.ts b/src/stream.ts index 3297673..79bb444 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -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