Skip to content
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

process: check no handle or request is active after bootstrap #26593

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ if (isMainThread) {
setupProcessStdio(getStdout, getStdin, getStderr);
} else {
const { getStdout, getStdin, getStderr } =
workerThreadSetup.initializeWorkerStdio();
workerThreadSetup.createStdioGetters();
setupProcessStdio(getStdout, getStdin, getStderr);
}

Expand Down
29 changes: 11 additions & 18 deletions lib/internal/process/worker_thread_only.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,25 @@

// This file contains process bootstrappers that can only be
// run in the worker thread.
const {
getEnvMessagePort
} = internalBinding('worker');

const {
kWaitingStreams,
ReadableWorkerStdio,
WritableWorkerStdio
createWorkerStdio
} = require('internal/worker/io');

const {
codes: { ERR_WORKER_UNSUPPORTED_OPERATION }
} = require('internal/errors');
const workerStdio = {};

function initializeWorkerStdio() {
const port = getEnvMessagePort();
port[kWaitingStreams] = 0;
workerStdio.stdin = new ReadableWorkerStdio(port, 'stdin');
workerStdio.stdout = new WritableWorkerStdio(port, 'stdout');
workerStdio.stderr = new WritableWorkerStdio(port, 'stderr');

let workerStdio;
function lazyWorkerStdio() {
if (!workerStdio) workerStdio = createWorkerStdio();
return workerStdio;
}
function createStdioGetters() {
return {
getStdout() { return workerStdio.stdout; },
getStderr() { return workerStdio.stderr; },
getStdin() { return workerStdio.stdin; }
getStdout() { return lazyWorkerStdio().stdout; },
getStderr() { return lazyWorkerStdio().stderr; },
getStdin() { return lazyWorkerStdio().stdin; }
joyeecheung marked this conversation as resolved.
Show resolved Hide resolved
};
}

Expand Down Expand Up @@ -55,7 +48,7 @@ function unavailable(name) {
}

module.exports = {
initializeWorkerStdio,
createStdioGetters,
unavailable,
wrapProcessMethods
};
18 changes: 16 additions & 2 deletions lib/internal/worker/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ const {
moveMessagePortToContext,
stopMessagePort
} = internalBinding('messaging');
const { threadId } = internalBinding('worker');
const {
threadId,
getEnvMessagePort
} = internalBinding('worker');

const { Readable, Writable } = require('stream');
const EventEmitter = require('events');
Expand Down Expand Up @@ -227,6 +230,16 @@ class WritableWorkerStdio extends Writable {
}
}

function createWorkerStdio() {
const port = getEnvMessagePort();
port[kWaitingStreams] = 0;
return {
stdin: new ReadableWorkerStdio(port, 'stdin'),
stdout: new WritableWorkerStdio(port, 'stdout'),
stderr: new WritableWorkerStdio(port, 'stderr')
};
}

module.exports = {
drainMessagePort,
messageTypes,
Expand All @@ -239,5 +252,6 @@ module.exports = {
MessageChannel,
setupPortReferencing,
ReadableWorkerStdio,
WritableWorkerStdio
WritableWorkerStdio,
createWorkerStdio
};
6 changes: 6 additions & 0 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,12 @@ MaybeLocal<Value> RunBootstrapping(Environment* env) {
.IsNothing())
return MaybeLocal<Value>();

// Make sure that no request or handle is created during bootstrap -
// if necessary those should be done in pre-exeuction.
// TODO(joyeecheung): print handles/requests before aborting
CHECK(env->req_wrap_queue()->IsEmpty());
CHECK(env->handle_wrap_queue()->IsEmpty());

env->set_has_run_bootstrapping_code(true);

return scope.EscapeMaybe(result);
Expand Down
32 changes: 16 additions & 16 deletions src/node_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -269,22 +269,6 @@ void Worker::Run() {
Debug(this, "Created Environment for worker with id %llu", thread_id_);
if (is_stopped()) return;
{
HandleScope handle_scope(isolate_);
Mutex::ScopedLock lock(mutex_);
// Set up the message channel for receiving messages in the child.
child_port_ = MessagePort::New(env_.get(),
env_->context(),
std::move(child_port_data_));
// MessagePort::New() may return nullptr if execution is terminated
// within it.
if (child_port_ != nullptr)
env_->set_message_port(child_port_->object(isolate_));

Debug(this, "Created message port for worker %llu", thread_id_);
}

if (is_stopped()) return;
{
#if NODE_USE_V8_PLATFORM && HAVE_INSPECTOR
StartWorkerInspector(env_.get(),
std::move(inspector_parent_handle_),
Expand All @@ -296,6 +280,9 @@ void Worker::Run() {
Environment::AsyncCallbackScope callback_scope(env_.get());
env_->async_hooks()->push_async_ids(1, 0);
if (!RunBootstrapping(env_.get()).IsEmpty()) {
CreateEnvMessagePort(env_.get());
if (is_stopped()) return;
Debug(this, "Created message port for worker %llu", thread_id_);
USE(StartExecution(env_.get(), "internal/main/worker_thread"));
}

Expand Down Expand Up @@ -348,6 +335,19 @@ void Worker::Run() {
Debug(this, "Worker %llu thread stops", thread_id_);
}

void Worker::CreateEnvMessagePort(Environment* env) {
HandleScope handle_scope(isolate_);
Mutex::ScopedLock lock(mutex_);
// Set up the message channel for receiving messages in the child.
child_port_ = MessagePort::New(env,
env->context(),
std::move(child_port_data_));
// MessagePort::New() may return nullptr if execution is terminated
// within it.
if (child_port_ != nullptr)
env->set_message_port(child_port_->object(isolate_));
}

void Worker::JoinThread() {
if (thread_joined_)
return;
Expand Down
2 changes: 1 addition & 1 deletion src/node_worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Worker : public AsyncWrap {

private:
void OnThreadStopped();

void CreateEnvMessagePort(Environment* env);
const std::string url_;

std::shared_ptr<PerIsolateOptions> per_isolate_opts_;
Expand Down