Skip to content

Memory leak on Windows Server 2022 depending on the installation Folder #58435

@valdiss

Description

@valdiss

Version

20.19.2

Platform

Microsoft Windows NT 10.0.20348.0 x64

Subsystem

No response

What steps will reproduce the bug?

I know this is not optimal, I should use pipeline promise function instead, but that does not fix the problem.

module.exports = async (
  jobFileSalt,
  storedFileName,
  jobRelativeFilePath,
  fileEncryptionKey,
  physicalFileStoragePath,
  inputFilePath,
  inputFileBase64,
) => {
  const fullDiskStoragePath = `${physicalFileStoragePath}${jobRelativeFilePath}`;
  const CIPHER_KEY = crypto.createHash('sha256').update(fileEncryptionKey).digest();

  return new Promise((resolve, reject) => {
    let readStream;

    // if file input is a path, stream source
    if (inputFilePath) readStream = fs.createReadStream(inputFilePath);
    else {
      readStream = new stream.PassThrough();
      readStream.write(Buffer.from(inputFileBase64, 'base64'));
      readStream.end();
    }

    const gzip = zlib.createGzip();
    // Generate the cipher to encrypt from CIPHER_KEY and Salt.
    const cipher = crypto.createCipheriv('aes256', CIPHER_KEY, jobFileSalt);

    // Create a write stream with a different file extension.
    const writeStream = fs.createWriteStream(`${fullDiskStoragePath}.enc`);

    const handleStreamErrors = (e) => {
      readStream.destroy();
      gzip.destroy();
      cipher.destroy();
      writeStream.destroy();
      reject(new Error(`Error processing ${storedFileName}: ${e.message}`));
    };

    readStream.on('error', handleStreamErrors);
    gzip.on('error', handleStreamErrors);
    cipher.on('error', handleStreamErrors);
    writeStream.on('error', handleStreamErrors);

    writeStream.on('close', () => {
      // Ensure that the streams are closed properly
      readStream.destroy();
      gzip.destroy();
      cipher.destroy();
      resolve();
    });

    readStream.pipe(gzip).pipe(cipher).pipe(writeStream);
  });
};

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

Execute this function with a binary version of nodejs in a different folder than C:\Program Files\nodejs and add it to the system PATH.

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

Expected behaviour is not to shoot the memory up to 900Mo RSS, heap is fine.

What do you see instead?

The memory shoots up to 900Mo RSS when my portable nodejs is in any other folder than C:\Program Files\nodejs.

Additional information

I tried a bunch of different binaries:
https://nodejs.org/download/release/latest-v20.x/win-x64/node.exe
https://nodejs.org/download/release/latest-v20.x/node-v20.19.2-win-x64.zip
https://nodejs.org/download/release/latest-v20.x/node-v20.19.2-x64.msi

It only worked when installing to the default C:\Program Files\nodejs folder with the msi.

After noticing that, I uninstalled the msi version and put the content of node-v20.19.2-win-x64.zip, in the same folder and it stopped the memory leak.

I also tried other node versions, the problem is not present with node 18 but is present with any version from node 20.x.x

Metadata

Metadata

Assignees

No one assigned

    Labels

    windowsIssues and PRs related to the Windows platform.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions