Skip to content

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

Closed

Description

@tbrandenburg

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

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).

Activity

  1. added a commit that references this issue on Oct 7, 2026
    bcf4737
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions