Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update util.writeFile to write to tmp file before rename #2683

Merged
merged 1 commit into from Sep 3, 2020

Conversation

knolleary
Copy link
Member

  • Bugfix (non-breaking change which fixes an issue)

Proposed changes

The Local Filesystem storage module has a utility function used to write files - this is used in a number of places such as the flow/credential files and the runtime settings (.config.json).

The existing code does the follow:

  1. renames the existing file to a backup file (if the caller provided a backup file name)
  2. writes the new content to the desired file
  3. tries to sync the file descriptor to get the writes flushed

This means there is a window of time where the original file has been renamed and the new content has not yet been written to the file.

I have seen a small number of times when using grunt dev (which will cause the node process to be killed whenever I modify a file) that the .config.json file is corrupted and the runtime settings are lost.

I cannot reliably reproduce it, but I'm pretty sure its related to killing Node-RED whilst its starting up.

This PR modifies the logic around the writeFile function. It now does:

  1. copies the existing file to a backup file (if the caller provided a backup file name)
  2. writes the new content to a temporary file (the filename is <file>.$$$)
  3. flushes the file descriptor
  4. renames the temporary file to the desired file.

That means the file has valid content through-out the process - as the final rename will be an atomic fs action.

@knolleary knolleary added this to the 1.2 milestone Aug 28, 2020
@knolleary knolleary merged commit 17812f0 into dev Sep 3, 2020
@knolleary knolleary deleted the write-to-temp branch January 11, 2021 11:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant