nodejs / nodejs/node

BroadcastChannel doesn't fill in useful MessageEvent fields like source, or ports

Offen
#59,053 21 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

worker
Vorherrschende Sprache
JavaScript
Sterne
122k
Forks
37.3k
Ø Merge
4 T. 2 Std.
Gemergte PRs (30 T.)
283

Beschreibung

Please populate the Worker value in BroadcastChannel MessageEvents 'source' field the same way it is in WebWorkers.

There is currently no mechanism to compose Workers in the NodeJS APIs and this change would provide a mechanism to
solve that problem, if not for the general case, at least with only a small amount of additional bookkeeping on the part of
application developers.

Without a composition mechanism, it is extremely difficult to implement cross-cutting concerns between workers, and between workers and large subsystems in the main application. Which is problematic for many areas of production code including caching, telemetry, reloadable configuration, and security.

Fundamentally, there is no mechanism to tell if a Worker has terminated. And since Workers tend to be used for tasks with high utilization of the event toop, even sending a message to ask if anyone is still listening to the BroadcastChannel is not guaranteed to work on any reasonable time frame, and in particular for cross-cutting concerns who can't know what, for instance, the cutoff time is for image processing in the Image Worker, and that the Cache Worker has a cutoff of 1/100th of that time. Which then leads to a scenario where you've decided a Worker is dead and then the same worker pops back up 10 seconds later. What you need is worker.on('exit') which is unambiguous.

Version

All versions

Platform

All Platforms

Subsystem

node:worker_threads

What steps will reproduce the bug?
        const util = require('util');
	const channel = new BroadcastChannel('example');

	if (isMainThread) {
		channel.addEventListener('message', evt => {
			console.log(util.inspect(evt)); // breakpoint here
		});
	} else {
		channel.postMessage('hello');
        }

The Message Event recieved looks like


MessageEvent {
  type: 'message',
  defaultPrevented: false,
  cancelable: false,
  timeStamp: 444.018666
}
How often does it reproduce? Is there a required condition?

Always. Seems to be by design, and the design may have drifted from the current web standards over time.

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

That I get a Worker object to facilitate lifecycle management and IPC coordination.

What do you see instead?

empty string, empty array. And no other mechanism in the Node API to interrogate the running isolates to find this information.

Additional information

No response

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginnen Sie am Einstiegspunkt node:worker_threads BroadcastChannel und reproduzieren Sie die MessageEvent-Ausgabe aus dem bereitgestellten Beispiel. Vergleichen Sie deren source- und ports-Felder mit dem im issue beschriebenen Verhalten von WebWorkers und überprüfen Sie anschließend, dass das resultierende Ereignis die angeforderten Worker-Informationen und das Verhalten zur Koordination des Lebenszyklus offenlegt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript, node.js
Bereich
api, backend
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Aktiv
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
48/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.