Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

250
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda