nodejs / nodejs/node

worker: `messageerror` listeners remain during `exit`

未关闭 适合新手
#65,782 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

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

描述

Version

v24.14.1

The typo is also present in the v26.8.1 source and
in the current main branch.

Platform

Microsoft Windows NT 10.0.26200.0 x64

Subsystem

worker_threads

What steps will reproduce the bug?

Run the following without any third-party dependencies:

const { Worker } = require('node:worker_threads');

const worker = new Worker('', { eval: true });
const listener = () => {};

worker.on('message', listener);
worker.on('messageerror', listener);

worker.on('exit', () => {
  console.log({
    message: worker.listenerCount('message'),
    messageerror: worker.listenerCount('messageerror'),
  });
});

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

It reproduces every time on v24.14.1 and on the current main branch at
7177c14789a7376866dfe6c04f5cd4da489cfbe6. It occurs both when a worker exits
normally and when it is stopped with worker.terminate().

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

Both messaging listener counts should be zero while the exit listener is
running:

{ message: 0, messageerror: 0 }

The worker exit cleanup explicitly removes the message listeners before
emitting exit and appears intended to do the same for messageerror.

What do you see instead?

The message listener is removed, but the messageerror listener is still
present:

{ message: 0, messageerror: 1 }

Additional information

The targeted cleanup currently calls
removeAllListeners('messageerrors'), but the event name is messageerror:

https://github.com/nodejs/node/blob/7177c14789a7376866dfe6c04f5cd4da489cfbe6/lib/internal/worker.js#L395-L396

The typo was introduced in commit 5968c5423530aeaa10c02eae3e8423c6514cdd1a:

https://github.com/nodejs/node/commit/5968c5423530aeaa10c02eae3e8423c6514cdd1a

There is a later generic removeAllListeners() call after the exit event is
emitted, so this report does not claim a persistent listener leak. The observed
problem is that the targeted pre-exit cleanup does not remove messageerror
listeners as intended.

贡献指南

打开贡献指南

从这里开始

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

调研方向

从 lib/internal/worker.js 第 395-396 行附近的退出清理开始,比较消息监听器使用的事件名称与 messageerror 事件名称。重新运行 issue 中的复现步骤,并确认在退出监听器运行时,两个监听器计数都为零。

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

评估

技术栈
javascript
领域
backend
Issue 类型
缺陷
难度
1/5
预计耗时
1 小时以内
活跃度
活跃
描述清晰度
描述清楚
新手友好度
90/100

把新 issue 发到你的邮箱

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