nodejs / nodejs/node

`fsPromise.writeFile` should catch stream's error and reject the promise.

Open
#58,742 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

confirmed-bug fs
Dominant language
JavaScript
Stars
122k
Forks
37.3k
Avg merge
4d 2h
Merged PRs (30d)
283

Description

Version

22.16.0

Platform
All
Subsystem

No response

What steps will reproduce the bug?

Following code fails with an application crash.

import fsp from "node:fs/promises";
import { createReadStream } from "node:fs";

try {
    const s1 = createReadStream("not-existent");
    await fsp.writeFile("new-file.txt", s1);
} catch (error) {
    console.log(`Sorry file copy failed`);
}

Instead it should just say that an error has occurred and reject the promise.

How often does it reproduce? Is there a required condition?

Everytime

What is the expected behavior? Why is that the expected behavior?

The application should not crash and an error should be thrown.

What do you see instead?

Application Crashes.

Reproduction Repository

ackava/node-write-file-promise-fail

This repo contains docker container source code to reproduce error in container. On windows machine, delay causes app to crash but in container, slight delay of 1 ms causes crash.

Workaround

So far following code works correctly with slight time differences, and does not crash the app, promise is rejected correctly.

        const writable = createWriteStream(outputFilePath);
        return pipeline(inputStream, writable, { end: true });

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the fsPromise.writeFile and createReadStream entry points shown in the reproduction, then compare their error handling with the pipeline workaround. Use the linked reproduction repository to reproduce the crash and verify that a missing input file rejects the promise instead of crashing the application.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nodejs
Domain
operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.