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.

Node crash with ERR_STREAM_DESTROYED when rotating logs

See original GitHub issue

I was using a simple file transport in my server.

       new winston.transports.File({ filename: C.DEFAULT_LOG_FILENAME })

That has been working fine throughout development. Now that I’m going to production, I added winston-daily-rotate-file, so I replaced the File transport listed above with this:

      new winston.transports.DailyRotateFile({ 
          filename      : C.DEFAULT_LOG_FILENAME + '-%DATE%.log',
          dirname       : './logs',
          datePattern   : 'YYYY-MM-DD:HH',
          zippedArchive : false,
          maxSize       : '2m',
          maxFiles      : '14d',
        })

But I found that my node server was crashing. Turns out it was crashing when it hit the file size limit and rotated log files. I changed the maxSize to “4k” and was able to get it to crash easily by generating log data. Here is the message from the crash:

Error [ERR_STREAM_DESTROYED]: Cannot call write after a stream was destroyed
    at doWrite (_stream_writable.js:406:19)
    at clearBuffer (_stream_writable.js:517:5)
    at onwrite (_stream_writable.js:465:7)
    at fs.write (fs.js:2282:5)
    at FSReqWrap.wrapper [as oncomplete] (fs.js:643:5)

I’m running node version is 10.3.0 on macOS 10.13.5. I’m new to node / winston, so its not clear to me if this is my problem, a problem in node, or a problem in winston.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
shirakabacommented, Feb 19, 2021

To anyone it may help: I got this error when I ran my app initially with sudo (meaning that logs would be written as root:root), then again without sudo (meaning that logs would be written as ubuntu:ubuntu, and thus lacked privileges to rotate the logs).

So in my case, it wasn’t a fault of Winston. Just a standard consequence of permissions management. To avoid it, always run the app as a user in the same usergroup as you intend to run it with in future.

0reactions
mattberthercommented, Jun 14, 2019

I’m moving this issue to #226, which more accurately describes the problem (winston-daily-rotate-file does not work well with process managers).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error [ERR_STREAM_DESTROYED]: Cannot call write after a ...
It worked locally, but when it got to aws, the error happened because node is running as user 'webapp'. It looks like that...
Read more >
Worker failures from stream write contention - GenieACS Forum
This happens to me on current master builds as well. Smells like a state management bug (trying to write to a socket that...
Read more >
Let It Crash: Best Practices for Handling Node.js Errors on ...
When an error occurred, there was often not enough visibility on why it happened, a lack of logging details, and bouts of downtime...
Read more >
Node.js v12.1.0 Documentation - Index of
Because printing to the console is an asynchronous operation, console.log() will cause the AsyncHooks callbacks to be called. Using console.log() or similar ...
Read more >
Error [ERR_STREAM_DESTROYED] - Node-RED Forum
Error connecting to dbus: Error [ERR_STREAM_DESTROYED]: Cannot call ... Can i assume node-red is being crashed and the error in the log is ......
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