Skip to content

Conversation

@TarikSogukpinar
Copy link
Contributor

fix(node:worker_threads): ensure 'exit' event fires once and matches Node.js behavior

related issue:
closes #30013

The 'exit' event in node:worker_threads was not consistently fired, or in some cases emitted multiple times, diverging from Node.js behavior.

How

• Added a private #exited flag to prevent duplicate 'exit' emissions
• Emit 'exit' with code 1 on TerminalError and with code 0 on Close
• Removed direct 'exit' emission from terminate() — event is now triggered by host signals
• Verified with both success and failure worker scenarios to ensure parity with Node.js and Bun

Copilot AI review requested due to automatic review settings November 8, 2025 22:16
@CLAassistant
Copy link

CLAassistant commented Nov 8, 2025

CLA assistant check
All committers have signed the CLA.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors the worker exit event handling to emit the 'exit' event based on control messages received from the worker, rather than immediately when terminate() is called. The change introduces a guard flag to ensure the exit event is only emitted once.

Key changes:

  • Added #exited flag to track whether the exit event has been emitted
  • Moved exit event emission from terminate() method to control message handlers (TerminalError and Close cases)
  • Exit event now emits with exit code 1 for TerminalError and 0 for normal Close

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@bartlomieju
Copy link
Member

Thanks for the PR, are there tests you could add?

@TarikSogukpinar
Copy link
Contributor Author

Thanks for the PR, are there tests you could add?

I will check the tests and add them as soon as possible.

@TarikSogukpinar
Copy link
Contributor Author

Thanks for the PR, are there tests you could add?

Tests have been added and verified locally — all passing

@bartlomieju bartlomieju changed the title fix(node:worker_threads): ensure 'exit' event fires once and matches Node.js behavior fix(ext/node): ensure 'exit' event is fired only once for worker_threads Nov 12, 2025
Copy link
Member

@bartlomieju bartlomieju left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great fix, thanks!

@bartlomieju bartlomieju merged commit 1edd1fb into denoland:main Nov 12, 2025
35 of 37 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

'exit' does not fire in node:worker_threads

3 participants