Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

242
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda