Skip to content

Commit e6662cb

Browse files
jasnellrichardlau
authored andcommitted
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 <[email protected]> Assisted-by: Opencode PR-URL: #66473 Reviewed-By: Matteo Collina <[email protected]>
1 parent 1aa10d8 commit e6662cb

6 files changed

Lines changed: 702 additions & 78 deletions

File tree

‎doc/api/fs.md‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8508,13 +8508,20 @@ Close the stream gracefully, flushing the internal buffer before closing.
85088508
* `callback` {Function}
85098509
* `err` {Error|null} An error if the flush failed, otherwise `null`.
85108510
8511-
Writes the current buffer to the file if a write was not in progress. Do
8512-
nothing if `minLength` is zero or if it is already writing.
8511+
Writes the current buffer to the file. The callback is invoked after pending
8512+
writes complete and, unless the `fsync` option is enabled, after the data has
8513+
been synchronized with `fs.fsync()`. `fsync` errors indicating that the file
8514+
descriptor does not support synchronization (for example, a pipe or a TTY)
8515+
are ignored.
85138516
85148517
#### `utf8Stream.flushSync()`
85158518
85168519
Flushes the buffered data synchronously. This is a costly operation.
85178520
8521+
If an asynchronous write is in flight when `flushSync()` is called, only the
8522+
data queued after it is written, and it may reach the file before the data
8523+
of the in-flight write.
8524+
85188525
#### `utf8Stream.fsync`
85198526
85208527
* {boolean} Whether the stream is performing a `fs.fsyncSync()` after every

0 commit comments

Comments
 (0)