Repository navigation
worker: add diagnostic channel 'worker_threads.main.registry' #67311
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e23ed07
worker: add diagnostic channel 'worker_threads.main.registry'
ShogunPanda bbd9341
fixup
ShogunPanda 2858f5f
fixup
ShogunPanda 0029e05
worker: address registry diagnostic channel review
ShogunPanda 3152ebb
test: skip registry diagnostics test in worker runner
ShogunPanda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
test/parallel/test-diagnostics-channel-worker-threads-registry.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| 'use strict'; | ||
| const common = require('../common'); | ||
| const assert = require('assert'); | ||
| const { once } = require('events'); | ||
| const dc = require('diagnostics_channel'); | ||
| const { Worker, isMainThread, parentPort, workerData } = require('worker_threads'); | ||
|
|
||
| // Only the main thread publishes registry updates; allow the test's own workers. | ||
| if (!isMainThread && workerData !== 'parent' && workerData !== 'child') { | ||
| common.skip('This test must be started on the main thread'); | ||
| } | ||
|
|
||
| const channelName = 'worker_threads.main.registry'; | ||
|
|
||
| if (isMainThread) { | ||
| // Expect one snapshot for each registration and removal in the worker tree. | ||
| const snapshots = []; | ||
| const childRegistered = Promise.withResolvers(); | ||
| const childRemoved = Promise.withResolvers(); | ||
|
|
||
| dc.subscribe(channelName, common.mustCall((message) => { | ||
| assert.strictEqual(Object.getPrototypeOf(message), null); | ||
| assert.deepStrictEqual(Object.keys(message), ['threadIds']); | ||
| snapshots.push(message.threadIds); | ||
|
|
||
| if (snapshots.length === 2) { | ||
| childRegistered.resolve(); | ||
| } | ||
| if (snapshots.length === 3) { | ||
| childRemoved.resolve(); | ||
| } | ||
| }, 4)); | ||
|
|
||
| async function test() { | ||
| const worker = new Worker(__filename, { workerData: 'parent' }); | ||
| const threadId = worker.threadId; | ||
|
|
||
| // The child ID and its registry update arrive on independent channels. | ||
| const [[childId]] = await Promise.all([ | ||
| once(worker, 'message'), | ||
| once(worker, 'online'), | ||
| childRegistered.promise, | ||
| ]); | ||
|
|
||
| assert.deepStrictEqual(snapshots, [[threadId], [threadId, childId]]); | ||
|
|
||
| // Remove the nested worker first, keeping its parent alive for the update. | ||
| worker.postMessage('stop'); | ||
| await childRemoved.promise; | ||
|
|
||
| assert.deepStrictEqual(snapshots, [[threadId], [threadId, childId], [threadId]]); | ||
|
|
||
| // Let the parent exit naturally, then check that earlier snapshots are intact. | ||
| const exited = once(worker, 'exit'); | ||
| worker.postMessage('finish'); | ||
| const [code] = await exited; | ||
|
|
||
| assert.strictEqual(code, 0); | ||
| assert.deepStrictEqual(snapshots, [[threadId], [threadId, childId], [threadId], []]); | ||
| } | ||
|
|
||
| test().then(common.mustCall()); | ||
| } else { | ||
| // Neither the parent worker nor its child should publish this channel locally. | ||
| dc.subscribe(channelName, common.mustNotCall()); | ||
|
|
||
| if (workerData === 'child') { | ||
| // Keep the child alive until its parent explicitly terminates it. | ||
| parentPort.on('message', common.mustNotCall()); | ||
| } else { | ||
| async function run() { | ||
| const worker = new Worker(__filename, { workerData: 'child' }); | ||
| await once(worker, 'online'); | ||
| parentPort.postMessage(worker.threadId); | ||
|
|
||
| // The main thread requests termination after observing both registrations. | ||
| await once(parentPort, 'message'); | ||
| assert.strictEqual(await worker.terminate(), 1); | ||
|
|
||
| // Wait until the main thread observes the child's removal before exiting. | ||
| await once(parentPort, 'message'); | ||
| parentPort.close(); | ||
| } | ||
|
|
||
| run().then(common.mustCall()); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.