#5966·node-red

storage/localfilesystem: saveLibraryEntry does not await util.writeFile, causing intermittent test/race on final rename

Author: tbrandenburgCreated Sep 20, 2026Updated Sep 20, 2026

Summary

saveLibraryEntry in packages/node_modules/@node-red/runtime/lib/storage/localfilesystem/library.js does not return/await the promise from util.writeFile(), so it resolves before the write+rename to the final file path has completed.

util.writeFile() (.../localfilesystem/util.js) writes to a temp file <path>.$$$ first and only fs.rename()s it to the final path once the write+fsync completes. Because that promise is not returned from the .then() callback in saveLibraryEntry, callers can observe the library directory before the rename has happened.

Repro

This surfaces as an intermittent failure of test/unit/@node-red/runtime/lib/storage/localfilesystem/library_spec.js ("should return a newly saved library function"), more frequently reproducible under Node 24's I/O timing:

AssertionError: expected [ { mno: 'pqr', fn: 'file3.js.$$$' } ]
              to equal [ { mno: 'pqr', fn: 'file3.js' } ]

The test calls saveLibraryEntry then immediately lists the library directory; occasionally it observes the temp .$$$ file because the rename hasn't happened yet.

Fix

javascript
saveLibraryEntry: async function(type,path,meta,body) {
    ...
    return fs.ensureDir(fspath.dirname(fn)).then(function () {
        return util.writeFile(fn,headers+body); // <-- return the promise
    });
}

Environment

Observed on a fork's CI matrix, Node 24, but the bug is present in current node-red/node-red main (confirmed via diff — this file is unmodified from upstream in the fork where it was found).