Skip to content

fs.mkdtempDisposableSync() / fsPromises.mkdtempDisposable() throw and leak the directory when the path is a Buffer #66593

Description

@gengjiawen

Version

v27.0.0-nightly20261007aa1bf2cc37 (main at 6d5e309)

Platform

Linux x64

Subsystem

fs

What steps will reproduce the bug?

const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');

const base = fs.mkdtempSync(path.join(os.tmpdir(), 'repro-'));
const prefix = path.join(base, 'x-');

for (const args of [[Buffer.from(prefix)], [prefix, { encoding: 'buffer' }]]) {
  try {
    fs.mkdtempDisposableSync(...args);
  } catch (err) {
    console.log(err.message);
    console.log('left behind:', fs.readdirSync(base));
  }
}

fs.promises.mkdtempDisposable() behaves the same way.

How often does it reproduce? Is there a required condition?

Always, whenever the created path is a Buffer: the prefix is a Buffer, or encoding is 'buffer'.

What is the expected behavior? Why is that the expected behavior?

Both calls return the disposable object with a Buffer path, and remove() deletes the directory. The docs list path as {string|Buffer}, and since #64397 a Buffer prefix defaults encoding to 'buffer'.

What do you see instead?

The "paths[1]" argument must be of type string. Received an instance of Buffer
left behind: [ 'x-Kx8KEG' ]
The "paths[1]" argument must be of type string. Received an instance of Buffer
left behind: [ 'x-Kx8KEG', 'x-gV98fq' ]

The ERR_INVALID_ARG_TYPE is thrown after the directory has been created. The caller never gets the disposable object, so nothing removes the directory.

Additional information

After binding.mkdtemp() creates the directory, both functions stash its absolute path for remove() with pathModule.resolve(cwd, path), and path.resolve() only accepts strings. The { encoding: 'buffer' } form has failed this way since mkdtempDisposable() was added in #58516; #64397 made every Buffer prefix take the same path.

I have a fix and will open a PR.

Activity

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