fix(ext/node): make worker setup synchronous (#22815)

This commit fixes race condition in "node:worker_threads" module were
the first message did a setup of "threadId", "workerData" and
"environmentData".
Now this data is passed explicitly during workers creation and is set up
before any user code is executed.

Closes https://github.com/denoland/deno/issues/22783
Closes https://github.com/denoland/deno/issues/22672

---------

Co-authored-by: Satya Rohith <me@satyarohith.com>
This commit is contained in:
Bartek Iwańczuk 2024-03-11 22:18:03 +00:00 committed by GitHub
parent 28b362adfc
commit d69aab62b0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 60 additions and 46 deletions

View file

@ -786,6 +786,7 @@ function bootstrapWorkerRuntime(
runtimeOptions,
name,
internalName,
maybeWorkerMetadata,
) {
if (hasBootstrapped) {
throw new Error("Worker runtime already bootstrapped");
@ -908,8 +909,17 @@ function bootstrapWorkerRuntime(
// existing global `Deno` with `Deno` namespace from "./deno.ts".
ObjectDefineProperty(globalThis, "Deno", core.propReadOnly(finalDenoNs));
const workerMetadata = maybeWorkerMetadata
? messagePort.deserializeJsMessageData(maybeWorkerMetadata)
: undefined;
if (nodeBootstrap) {
nodeBootstrap(hasNodeModulesDir, argv0, /* runningOnMainThread */ false);
nodeBootstrap(
hasNodeModulesDir,
argv0,
/* runningOnMainThread */ false,
workerMetadata,
);
}
}