X Tutup
The Wayback Machine - https://web.archive.org/web/20250814212428/https://github.com/nodejs/node/pull/59213
Skip to content

worker: add name for worker #59213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 5, 2025

Conversation

theanarkh
Copy link
Contributor

In some scenarios, name is very useful(such as in the APM SDK) and easier to understand.

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

process.on('worker', (worker) => {
  // output: test-worker-thread-name in main thread
  console.log(worker.threadName + " in main thread");
});

new Worker(`
  const { threadName } = require('worker_threads');
  // output: test-worker-thread-name in worker thread
  console.log(threadName + " in worker thread");
`, { eval: true, name: 'test-worker-thread-name' });
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. labels Jul 25, 2025
@theanarkh theanarkh added the worker Issues and PRs related to Worker support. label Jul 25, 2025
@theanarkh theanarkh force-pushed the add_name_for_worker branch 3 times, most recently from 8cffc77 to db88135 Compare July 25, 2025 16:48
Copy link

codecov bot commented Jul 25, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.00%. Comparing base (04c5a18) to head (6f7e498).
⚠️ Report is 56 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #59213      +/-   ##
==========================================
- Coverage   90.04%   90.00%   -0.04%     
==========================================
  Files         648      649       +1     
  Lines      191200   192219    +1019     
  Branches    37472    37659     +187     
==========================================
+ Hits       172160   173006     +846     
- Misses      11665    11826     +161     
- Partials     7375     7387      +12     
Files with missing lines Coverage Δ
lib/internal/worker.js 98.83% <100.00%> (+0.01%) ⬆️
lib/worker_threads.js 100.00% <100.00%> (ø)
src/api/environment.cc 77.05% <100.00%> (+0.23%) ⬆️
src/env-inl.h 94.23% <100.00%> (+0.02%) ⬆️
src/env.cc 80.64% <100.00%> (-0.41%) ⬇️
src/env.h 98.14% <ø> (ø)
src/node.h 92.30% <ø> (ø)
src/node_worker.cc 84.57% <100.00%> (+1.37%) ⬆️

... and 43 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@theanarkh theanarkh force-pushed the add_name_for_worker branch 3 times, most recently from c59afff to cf60597 Compare July 26, 2025 18:37
Copy link
Member

@addaleax addaleax left a comment

Choose a reason for hiding this comment

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

LGTM! :shipit:

@theanarkh theanarkh added the request-ci Add this label to start a Jenkins CI on a PR. label Jul 27, 2025
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Jul 27, 2025
@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@theanarkh theanarkh force-pushed the add_name_for_worker branch from cf60597 to 854c313 Compare July 27, 2025 09:04
@theanarkh theanarkh added the request-ci Add this label to start a Jenkins CI on a PR. label Jul 28, 2025
@github-actions github-actions bot added request-ci-failed An error occurred while starting CI via request-ci label, and manual interventon is needed. and removed request-ci Add this label to start a Jenkins CI on a PR. labels Jul 28, 2025
Copy link
Contributor

Failed to start CI
   ⚠  Commits were pushed since the last approving review:
   ⚠  - worker: add name for worker
   ✘  Refusing to run CI on potentially unsafe PR
https://github.com/nodejs/node/actions/runs/16567111188

@theanarkh theanarkh force-pushed the add_name_for_worker branch from 854c313 to 42df3d9 Compare July 28, 2025 14:44
@theanarkh
Copy link
Contributor Author

@addaleax Hi! I modified the test example to support creating worker in worker. Could you help review again ? Thanks !

@theanarkh theanarkh added request-ci Add this label to start a Jenkins CI on a PR. and removed request-ci-failed An error occurred while starting CI via request-ci label, and manual interventon is needed. labels Jul 29, 2025
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Jul 29, 2025
@nodejs-github-bot
Copy link
Collaborator

@theanarkh theanarkh force-pushed the add_name_for_worker branch from 07eb563 to d1f417c Compare July 29, 2025 18:48
@theanarkh theanarkh force-pushed the add_name_for_worker branch from d1f417c to 6f7e498 Compare July 30, 2025 05:55
@theanarkh theanarkh requested review from jasnell and addaleax July 31, 2025 09:56
@theanarkh theanarkh added the request-ci Add this label to start a Jenkins CI on a PR. label Aug 4, 2025
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Aug 4, 2025
@nodejs-github-bot

This comment was marked as outdated.

@nodejs-github-bot

This comment was marked as outdated.

@nodejs-github-bot
Copy link
Collaborator


const name = 'test-worker-thread-name';

if (workerData?.isWorker) {
Copy link
Member

Choose a reason for hiding this comment

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

It looks like the workerData?.isWorker is here only to determine if you're running in a worker or not. You can use isMainThread for that purpose and simplify this a bit.

const { Worker, isMainThread } = require('worker_threads');

if (!isMainThread) {
  // This is running in a worker
} else {
  // This is running in the main thread
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

workerData?.isWorker is designed to support the creation of worker within worker.

Copy link
Member

Choose a reason for hiding this comment

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

Is that actually necessary tho? I would just make this test as a whole not run in a worker.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We also need to support this use case, so should we support this kind of test ?


if (workerData?.isWorker) {
assert.strictEqual(threadName, name);
process.exit(0);
Copy link
Member

Choose a reason for hiding this comment

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

There should be no reason for process.exit(0) here.

@@ -721,6 +721,17 @@ An integer identifier for the current thread. On the corresponding worker object
(if there is any), it is available as [`worker.threadId`][].
This value is unique for each [`Worker`][] instance inside a single process.

## `worker.threadName`
Copy link
Member

Choose a reason for hiding this comment

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

Just a nit since I see we don't do this with the other properties here so feel free to ignore, but it would be ideal if the docs were clear that this is a read-only property. (same goes for the other read-only properties here)

Comment on lines +430 to +432
if (this[kHandle] === null) return null;

return this[kHandle].threadName;
Copy link
Member

Choose a reason for hiding this comment

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

I believe you could simplify this a bit as...

Suggested change
if (this[kHandle] === null) return null;
return this[kHandle].threadName;
return this[kHandle]?.threadName || null;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will optimize it in another pr.Thanks.

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@theanarkh theanarkh added author ready PRs that have at least one approval, no pending requests for changes, and a CI started. commit-queue Add this label to land a pull request using GitHub Actions. labels Aug 5, 2025
@nodejs-github-bot nodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Aug 5, 2025
@nodejs-github-bot nodejs-github-bot merged commit 3090def into nodejs:main Aug 5, 2025
64 checks passed
@nodejs-github-bot
Copy link
Collaborator

Landed in 3090def

panva pushed a commit to panva/node that referenced this pull request Aug 7, 2025
PR-URL: nodejs#59213
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
targos pushed a commit that referenced this pull request Aug 8, 2025
PR-URL: #59213
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
mete0rfish pushed a commit to mete0rfish/node-contribute that referenced this pull request Aug 9, 2025
PR-URL: nodejs#59213
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
panva pushed a commit to panva/node that referenced this pull request Aug 9, 2025
PR-URL: nodejs#59213
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
RafaelGSS pushed a commit that referenced this pull request Aug 12, 2025
PR-URL: #59213
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. worker Issues and PRs related to Worker support.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
X Tutup