Skip to content

Commit d52d366

Browse files
committed
stream: fix Utf8Stream flush handling
Fix several issues with `Utf8Stream` flushing: * `flush()` now writes buffered data regardless of `minLength` and invokes the callback only after pending writes complete, including when `minLength` is zero and a write is in flight. * Multiple concurrent `flush()` calls are tracked correctly and `end()` waits for pending flushes before closing. * `flushSync()` throws `ERR_INVALID_STATE` if called while an asynchronous write is in progress instead of corrupting output. * Periodic flushes no longer stack up while a flush is pending. * `fsync` is skipped for stdout/stderr file descriptors. * `reopen()`, `end()` and `destroy()` behave correctly when the stream is destroyed while still opening. Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-by: Opencode
1 parent f4d2742 commit d52d366

6 files changed

Lines changed: 698 additions & 73 deletions

File tree

‎doc/api/fs.md‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8563,13 +8563,20 @@ Close the stream gracefully, flushing the internal buffer before closing.
85638563
* `callback` {Function}
85648564
* `err` {Error|null} An error if the flush failed, otherwise `null`.
85658565
8566-
Writes the current buffer to the file if a write was not in progress. Do
8567-
nothing if `minLength` is zero or if it is already writing.
8566+
Writes the current buffer to the file. The callback is invoked after pending
8567+
writes complete and, unless the `fsync` option is enabled, after the data has
8568+
been synchronized with `fs.fsync()`. `fsync` errors indicating that the file
8569+
descriptor does not support synchronization (for example, a pipe or a TTY)
8570+
are ignored.
85688571
85698572
#### `utf8Stream.flushSync()`
85708573
85718574
Flushes the buffered data synchronously. This is a costly operation.
85728575
8576+
If an asynchronous write is in flight when `flushSync()` is called, only the
8577+
data queued after it is written, and it may reach the file before the data
8578+
of the in-flight write.
8579+
85738580
#### `utf8Stream.fsync`
85748581
85758582
* {boolean} Whether the stream is performing a `fs.fsyncSync()` after every

0 commit comments

Comments
 (0)