Skip to content

worker: add name for worker - #59213

Merged
nodejs-github-bot merged 1 commit into
nodejs:mainfrom
theanarkh:add_name_for_worker
Aug 5, 2025
Merged

nodejs-github-bot merged 1 commit into
nodejs:mainfrom
theanarkh:add_name_for_worker

Conversation

@theanarkh

Copy link
Copy Markdown
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 involving general changes in the lib/ or src/ directories. needs-ci PRs that need a full CI run. labels Jul 25, 2025
@theanarkh theanarkh added the worker Issues and PRs related to the worker_threads module and Worker API. 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
@codecov

codecov Bot commented Jul 25, 2025

Copy link
Copy Markdown

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.

Comment thread src/node.h Outdated
@theanarkh
theanarkh force-pushed the add_name_for_worker branch 3 times, most recently from c59afff to cf60597 Compare July 26, 2025 18:37

@addaleax addaleax left a comment

Copy link
Copy Markdown
Member

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. Only starts once the PR has an approving review. label Jul 27, 2025
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Jul 27, 2025
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@nodejs-github-bot

Copy link
Copy Markdown
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. Only starts once the PR has an approving review. label Jul 28, 2025
@github-actions github-actions Bot added request-ci-failed Starting CI with the request-ci label failed and requires manual intervention. and removed request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. labels Jul 28, 2025
@github-actions

Copy link
Copy Markdown
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
Copy Markdown
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. Only starts once the PR has an approving review. and removed request-ci-failed Starting CI with the request-ci label failed and requires manual intervention. labels Jul 29, 2025
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Jul 29, 2025
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Comment thread src/node_worker.cc Outdated
@theanarkh
theanarkh requested review from addaleax and jasnell July 31, 2025 09:56
@theanarkh theanarkh added the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Aug 4, 2025
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. 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
Copy Markdown
Collaborator


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

if (workerData?.isWorker) {

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

Comment thread doc/api/worker_threads.md
(if there is any), it is available as [`worker.threadId`][].
This value is unique for each [`Worker`][] instance inside a single process.

## `worker.threadName`

ghost Aug 4, 2025

Copy link
Copy Markdown
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 thread lib/internal/worker.js
Comment on lines +430 to +432
if (this[kHandle] === null) return null;

return this[kHandle].threadName;

ghost Aug 4, 2025

Copy link
Copy Markdown
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;

ghost Aug 4, 2025

Copy link
Copy Markdown
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

ghost commented Aug 4, 2025

Copy link
Copy Markdown
Collaborator

@nodejs-github-bot

ghost commented Aug 4, 2025

Copy link
Copy Markdown
Collaborator

@theanarkh theanarkh added author ready PRs with CI started, the required approvals, and no outstanding review comments. commit-queue PRs queued for automated landing through the Commit Queue. labels Aug 5, 2025
@nodejs-github-bot nodejs-github-bot removed the commit-queue PRs queued for automated landing through the Commit Queue. label Aug 5, 2025
@nodejs-github-bot
nodejs-github-bot merged commit 3090def into nodejs:main Aug 5, 2025
@nodejs-github-bot

ghost commented Aug 5, 2025

Copy link
Copy Markdown
Collaborator

Landed in 3090def

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author ready PRs with CI started, the required approvals, and no outstanding review comments. c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs involving general changes in the lib/ or src/ directories. needs-ci PRs that need a full CI run. worker Issues and PRs related to the worker_threads module and Worker API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants