A persistent but intermittent Node.js fs.writeFile problem
by Dave Winer Tuesday, September 27, 2016

I have a recurring but very intermittent problem with a Node.js server app that's used to host all my apps. 

It's not reproducible, but it happens every so often. No clear cause and effect. 

So here's what I know about the problem. 

The app calls fs.writeFile to save a JSON file. The file is called appPrefs.json. This is what the file contains (or something like it, depending on the app) when it's working. However, this is what ends up in the file, very rarely, when it fails. Note that it is not valid JSON (the problem is at the end of the file) and the app can't proceed. The user is stuck until the file is fixed. Not good since very often the user is not even aware that the file exists! :-(

What appears to have happened is that the file has gotten smaller, and there's a bit of the old version of the file still hanging around after the real contents of the file. 

This seems to indict fs.writeFile. But it's really hard to imagine it has a bug like this, intermittent or not. I've searched for any other reports like this, but haven't found anything. 

This only seems to happen with JSON files, and only one type of JSON file, appPrefs.json. Which seems to un-indict fs.writeFile. The culprit might be JSON.stringify? Again, doesn't seem very likely. And why just this particular type of JSON file? And since the problem has yet to show up when using S3 storage, it seems to be limited to fs.writeFile or the Linux OS? (All my servers run Ubuntu.)

There are many layers to the code, distributed between the app running on the user's machine, and the server app

I might try to work around it in one of two ways, that I can think of.

1. If I'm writing a file with a .json extension, after writing it, try reading and parsing it. If it fails, write it again. This would be based on the theory that the problem is in fs.writeFile.

2. Before writing any file, delete it and then call fs.writeFile. This might work if the problem is in the filesystem (I'm running on Linux).

This is the number one support problem for 1999.io, and it came up last night in screen2.io. I want to get this behind me. Any help appreciated. 

I turned on comments for this post. Thanks in advance for any help.