Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

238
Views
What happens to the file if a Node script is aborted during a writeFileSync operation?

Let's say I run a Node script in a Linux environment that contains the following:

fs.writeFileSync("output.json", JSON.stringify(data))

Then, I abort the script (cmd/ctrl + c) after the writeFileSync operation has begun but before it has finished. Does "output.json" exist? If so, does it contain any data?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I did a quick test on Windows 10 and had some interesting results:

  • Writing more data to a disk than there's free disk space will result in an error about there not being enough free disk space. The file will still get created though, with 0 bytes.
  • I created a buffer of 1 GB data and tried writing it to a file on a HDD. Takes about 10s for the write to finish. If I kill the process, it would get "locked", where e.g. trying to kill it again errors. The process would still keep writing data to the file, and only disappear once it's done writing. Every byte got written to the file, even if I kill the process if it was physically impossible to have written the full 1 GB by then.
  • Went further. Ran the above test again, but using fs.openSync and fs.writeSync. Between both calls, I took the time to find the file handle in Process Explorer, which I then closed during the fs.writeSync call. Now PE froze until the write closed. Tried ending the Node process using Task Manager in the meantime. Result? Every byte was still written to the file.
  • After all that, I tried what the question actually suggested: start writing the file, and immediately ctrl+c. Node throws a nice error and immediately exits, but the file still got created with 0 bytes. Your mileage might vary though. especially if you set up signal handling.

I didn't go as far as cutting the power or unplugging the disk during write, although at that point it's probably OS/disk-dependent on what happens next. Seems like NodeJS will always create the file though. Not that surprising, since fs.openSync('path', 'w') already does that, although still a bit unexpected it even succeeds in creating the (empty) file right before it errors about not enough disk space.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!