nodejs / nodejs/node

memory leak with worker thread and parentPort.postMessage

未关闭
#41,762 4 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

worker
主要语言
JavaScript
星标
122k
派生
37.4k
平均合并
4 天 3 小时
30 天内合并 PR
272

描述

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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先使用 node test.js 运行 issue 的 test.js,并比较多个 worker_threads worker 调用 parentPort.postMessage 时的内存使用情况。追踪涉及的 worker 和消息处理;完成的标准是,重复运行的 worker 保持内存使用量恒定,而不是线性增长。

由索引模型根据 Issue 内容生成。

评估

技术栈
javascript, node.js
领域
backend
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。