Skip to content

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

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

Open
valdiss opened this issue May 23, 2025 · 3 comments
Open
Labels
windows Issues and PRs related to the Windows platform.

Comments

@valdiss
Copy link

valdiss commented May 23, 2025

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

@marco-ippolito marco-ippolito added the windows Issues and PRs related to the Windows platform. label May 23, 2025
@juanarbol
Copy link
Member

Why do you call it a memory leak? Does it crash due to OOM?

I don't have a Windows machine, can you test this without crypto operations? It may be related to OpenSSL. Who knows.

As a ref:

C:\Program Files\OpenSSL

@valdiss
Copy link
Author

valdiss commented May 23, 2025

I might have wrongfully called that a memory leak, under normal use, the app consumes about 150Mo of RAM, when node is not under C:\Program Files\nodejs, it goes from 100Mo to 950Mo after just one processing and doesn't come back down.

One thing I'd like to add is that, it doesn't cause any problem when putting node under C:\Program Files\nodejs\node-v20.19.2 or any subdirectory to that.

I will try to identify which part creates that big memory consumption.

For info, we're using OpenSSL 3.0.16.

One other thing to notice is that it works normally with node 18.

@valdiss
Copy link
Author

valdiss commented May 26, 2025

Monday here, I tested without the crypto part and it does not go up to 900Mo and acts normally.

I also tried to use OpenSSL 3.2.4 11 Feb 2025 (Library: OpenSSL 3.2.4 11 Feb 2025) installed by Git for Windows, same behaviour.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
windows Issues and PRs related to the Windows platform.
Projects
None yet
Development

No branches or pull requests

3 participants