Skip to content

Commit 960d79d

Browse files
committed
refactor(csv): enhance output stream cleanup logic
- Updated the cleanup logic in the `convertCsvCommand` function to ensure proper handling of output streams after I/O errors. - Modified the conditions for destroying the output stream to prevent skipping cleanup when the stream is not gracefully ended.
1 parent c130ba9 commit 960d79d

2 files changed

Lines changed: 6 additions & 4 deletions

File tree

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ apps/ensrainbow/data*
3939

4040
# fallback-ensapi dist
4141
apps/fallback-ensapi/dist
42-
apps/ensrainbow/data*
4342
apps/ensrainbow/temp*
4443
apps/ensrainbow/v2*
4544
apps/ensrainbow/ens-test*

apps/ensrainbow/src/commands/convert-csv-command.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -619,15 +619,18 @@ export async function convertCsvCommand(options: ConvertCsvCommandOptions): Prom
619619
throw error;
620620
} finally {
621621
// Clean up output stream if it wasn't gracefully ended (error path).
622-
// After end(), writable is false, so this only triggers on error paths.
623-
if (outputStream?.writable) {
622+
// Use writableEnded/destroyed so we still run cleanup after an I/O error
623+
// (writable can be false in that case and would incorrectly skip cleanup).
624+
if (outputStream && !outputStream.writableEnded && !outputStream.destroyed) {
624625
const outputPath = (outputStream as WriteStream & { path?: string }).path ?? outputFile;
625626
try {
626627
// Suppress errors from in-flight writes whose async I/O completes
627628
// after destroy (e.g. "Cannot call write after a stream was destroyed").
628629
// On error paths the output file is incomplete anyway.
629630
outputStream.on("error", () => {});
630-
outputStream.destroy();
631+
if (!outputStream.destroyed) {
632+
outputStream.destroy();
633+
}
631634
// Wait for the OS file handle to be released before unlinking.
632635
// destroy() is asynchronous; the handle is only freed on the 'close' event.
633636
await once(outputStream, "close");

0 commit comments

Comments
 (0)