Skip to content

memory leak with worker thread and parentPort.postMessage #41762

Open
@8HoLoN

Description

@8HoLoN

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    workerIssues and PRs related to Worker support.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions