question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error: A ReadStream cannot be created from a destroyed WriteStream.

See original GitHub issue

Hi, I’ve got problem with reading contents from stream.

Either I get this error (👇) or writeStream hangs forever…

Error: A ReadStream cannot be created from a destroyed WriteStream.
    at WriteStream.createReadStream (/var/bb/node_modules/fs-capacitor/dist/index.js:174:19)
    at Object.createReadStream (/var/bb/node_modules/graphql-upload/lib/processRequest.js:297:28)

Example code:

class ImageStorageService {
  async uploadImage(upload: FileUpload): Promise<string> {
    const filename = `${cuid()}-${upload.filename}`;
    const tempFile = `${tmpdir()}/${filename}`;
    const readStream = upload.createReadStream();

    const writeStream = createWriteStream(tempFile);
    readStream.pipe(writeStream);

    // hangs forever, no finish event emitted
    const writePromise = new Promise((res) => {
      writeStream.on("open", () => {
        console.log("Stream opened");
      });
      writeStream.on("ready", () => {
        console.log("Stream ready");
      });
      writeStream.on("finish", () => {
        console.log("Stream finished");
        res();
      });
    });
    await writePromise;
   // ...
  }
}

Any ideas? Am I doing something wrong with streams?

Btw I had to use this fix to prevent maximum call stack size exceeded error.

  "resolutions": {
    "**/**/fs-capacitor": "^5.0.0",
    "**/graphql-upload": "^10.0.0"
  }

Node: v14.2.0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:23 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
mike-marcaccicommented, May 28, 2020

I suspect this is behaving correctly and that your uploadImage function is likely being called after all your resolvers have returned.

Once the GraphQL operation has completed, the files backing these streams are flagged for deletion, and new read streams cannot be created from them. They will continue to serve existing read streams, though.

Essentially, it’s expected that you create all necessary streams before your resolver returns/resolves.

2reactions
jaydensericcommented, Jun 8, 2020

Hope you figured it out ok; we can reopen this issue if a specific graphql-upload bug is discovered.

Read more comments on GitHub >

github_iconTop Results From Across the Web

createReadStream is not present in uploaded image
Below is a single file upload example that uses the graphql-upload and apollo-server-express packages. Package versions:
Read more >
fs-capacitor - npm
Once a WriteStream is fully destroyed, calling .createReadStream() will throw a ReadAfterDestroyedError error. As soon as a ReadStream ends or ...
Read more >
fs-capacitor - npm Package Health Analysis - Snyk
To help you get started, we've collected the most common ways that fs-capacitor ... buffer stream const capacitor = new WriteStream(); capacitor.on("error", ...
Read more >
node_modules/fs-capacitor · master - GitLab
If error is null or undefined, destruction of underlying resources is delayed until no ReadStream s are attached the WriteStream instance.
Read more >
Stream | Node.js v19.3.0 Documentation
transform.destroy([error]) ... All streams created by Node.js APIs operate exclusively on strings and ... createReadStream('file.txt'); const z = zlib.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found