nodejs / nodejs/node

memory leak with worker thread and parentPort.postMessage

Open
#41,762 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

worker
Dominant language
JavaScript
Stars
122k
Forks
37.3k
Avg merge
4d 2h
Merged PRs (30d)
283

Description

Version

v17.4.0

Platform

Microsoft Windows NT 10.0.22000.0 x64

Subsystem

No response

What steps will reproduce the bug?

node test.js

  • test.js :
const {
    Worker, isMainThread, parentPort
} = require('worker_threads');

if (isMainThread) {
    function parseJSAsync(script) {
        return new Promise((resolve, reject) => {
            const worker = new Worker(__filename);
            //worker.on('message', resolve);
            worker.on('message', (i)=>{
                //console.log('plop' + i);
            });
            worker.on('error', reject);
            worker.on('exit', (code) => {
                if (code !== 0)
                    reject(new Error(`Worker stopped with exit code ${code}`));
            });
        });
    };
    parseJSAsync()// no leak if called only once
    parseJSAsync()// leak appears if called twice or more
    parseJSAsync()
} else {
    let i = 0;
    while (i<1000000000000) {
        parentPort.postMessage(i);// no leak if commented
        i++;
    }
}

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

Always, if more than 1 worker is instantiated and parentPort.postMessage is used.

What is the expected behavior?

constant memory usage

What do you see instead?

a linear increase of memory usage over time (up to 16Gio+ in less than 1min)

Additional information

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the issue's test.js with node test.js and compare memory use when multiple worker_threads workers call parentPort.postMessage. Trace the worker and message handling involved; done means repeated workers maintain constant memory usage instead of growing linearly.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.