Kirk Ouimet
Kirk Ouimet

Reputation: 28404

Externally Modifying a File Breaks Writable Streams in node.js

I've got a simple logging tool to write log data to disk, it works like this:

  1. Use fs.createWriteStream to create a writable stream using the flags 'a' for appending. This creates or opens the file for me to write data to.
  2. Use writeStream.write() to write data to my log file.

This works perfectly, except for in these two cases:

  1. I open the log file while my application is running, change something, and save the log file.
  2. I delete the log file while the application is running. Expected behavior is to recreate the file and keep logging away.

In both cases I can still call writeStream.write() with no error.

Inspecting my writeStream object after I either modify or delete the log file while the application is running shows the writeStream.writable property to be true. Each time I call writeStream.write() the writelen property increases which leads me to believe the writeStream thinks that everything is OK, even though it is not.

My questions:

  1. Why does modifying and saving my log file externally cause writeStream.write() to stop writing data to my file? How do I fix it?
  2. Why does deleting my log file externally cause writeStream.write() to stop writing data to my file? How do I fix it?

Upvotes: 0

Views: 102

Answers (1)

Nitzan Shaked
Nitzan Shaked

Reputation: 13598

Everything is OK, as far the write stream is concerned. When you delete the file you are in effect removing an entry in the directory, but the original inode is not deleted, and your app that has it open keeps writing to the disk, only you don't have a pointer to it (a dir entry), so can't see. You will notice that free disk space keeps decreasing, which tells you that things are indeed written. When the app closes the file it is then removed (since no references are left pointing to it), at which point the free disk space will increase again.

The standard practice for you are trying to do is to send a signal (SIGHUP or SIGUSR1 to the app), and in the signal handler have the app close and re-open the file. This is called "log rotation". There are many node packages that do this for you, or you can do this yourself for the practice :-)

Upvotes: 1

Related Questions