src: split ownsProcessState off isMainThread · nodejs/node@01bb7b7 · GitHub
Skip to content

Commit 01bb7b7

Browse files
addaleaxtargos
authored andcommitted
src: split ownsProcessState off isMainThread
Embedders may want to control whether a Node.js instance controls the current process, similar to what we currently have with `Worker`s. Previously, the `isMainThread` flag had a bit of a double usage, both for indicating whether we are (not) running a Worker and whether we can modify per-process state. PR-URL: #25881 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent fd6ce53 commit 01bb7b7

14 files changed

Lines changed: 76 additions & 41 deletions

lib/internal/bootstrap/node.js

Lines changed: 3 additions & 3 deletions

lib/internal/worker.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ const { deserializeError } = require('internal/error-serdes');
3030
const { pathToFileURL } = require('url');
3131

3232
const {
33-
Worker: WorkerImpl,
33+
ownsProcessState,
34+
isMainThread,
3435
threadId,
35-
isMainThread
36+
Worker: WorkerImpl,
3637
} = internalBinding('worker');
3738

3839
const kHandle = Symbol('kHandle');
@@ -251,7 +252,8 @@ function pipeWithoutWarning(source, dest) {
251252
}
252253

253254
module.exports = {
255+
ownsProcessState,
256+
isMainThread,
254257
threadId,
255258
Worker,
256-
isMainThread
257259
};

lib/trace_events.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ const {
1313
ERR_INVALID_ARG_TYPE
1414
} = require('internal/errors').codes;
1515

16-
const { isMainThread } = require('internal/worker');
17-
if (!hasTracing || !isMainThread)
16+
const { ownsProcessState } = require('internal/worker');
17+
if (!hasTracing || !ownsProcessState)
1818
throw new ERR_TRACE_EVENTS_UNAVAILABLE();
1919

2020
const { CategorySet, getEnabledCategories } = internalBinding('trace_events');

src/api/environment.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,12 @@ Environment* CreateEnvironment(IsolateData* isolate_data,
134134
std::vector<std::string> args(argv, argv + argc);
135135
std::vector<std::string> exec_args(exec_argv, exec_argv + exec_argc);
136136
// TODO(addaleax): Provide more sensible flags, in an embedder-accessible way.
137-
Environment* env =
138-
new Environment(isolate_data, context, Environment::kIsMainThread);
137+
Environment* env = new Environment(
138+
isolate_data,
139+
context,
140+
static_cast<Environment::Flags>(Environment::kIsMainThread |
141+
Environment::kOwnsProcessState |
142+
Environment::kOwnsInspector));
139143
env->Start(per_process::v8_is_profiling);
140144
env->ProcessCliArgs(args, exec_args);
141145
return env;

src/env-inl.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,14 @@ inline bool Environment::is_main_thread() const {
650650
return flags_ & kIsMainThread;
651651
}
652652

653+
inline bool Environment::owns_process_state() const {
654+
return flags_ & kOwnsProcessState;
655+
}
656+
657+
inline bool Environment::owns_inspector() const {
658+
return flags_ & kOwnsInspector;
659+
}
660+
653661
inline uint64_t Environment::thread_id() const {
654662
return thread_id_;
655663
}

src/env.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ void InitThreadLocalOnce() {
142142
}
143143

144144
void Environment::TrackingTraceStateObserver::UpdateTraceCategoryState() {
145-
if (!env_->is_main_thread()) {
145+
if (!env_->owns_process_state()) {
146146
// Ideally, we’d have a consistent story that treats all threads/Environment
147147
// instances equally here. However, tracing is essentially global, and this
148148
// callback is called from whichever thread calls `StartTracing()` or

src/env.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,9 @@ class Environment {
597597

598598
enum Flags {
599599
kNoFlags = 0,
600-
kIsMainThread = 1
600+
kIsMainThread = 1 << 0,
601+
kOwnsProcessState = 1 << 1,
602+
kOwnsInspector = 1 << 2,
601603
};
602604

603605
static inline Environment* GetCurrent(v8::Isolate* isolate);
@@ -766,6 +768,8 @@ class Environment {
766768
inline void set_has_run_bootstrapping_code(bool has_run_bootstrapping_code);
767769

768770
inline bool is_main_thread() const;
771+
inline bool owns_process_state() const;
772+
inline bool owns_inspector() const;
769773
inline uint64_t thread_id() const;
770774
inline worker::Worker* worker_context() const;
771775
inline void set_worker_context(worker::Worker* context);

src/inspector/tracing_agent.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ DispatchResponse TracingAgent::start(
141141
return DispatchResponse::Error(
142142
"Call NodeTracing::end to stop tracing before updating the config");
143143
}
144-
if (!env_->is_main_thread()) {
144+
if (!env_->owns_process_state()) {
145145
return DispatchResponse::Error(
146146
"Tracing properties can only be changed through main thread sessions");
147147
}

src/inspector_agent.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ bool Agent::Start(const std::string& path,
686686
host_port_ = host_port;
687687

688688
client_ = std::make_shared<NodeInspectorClient>(parent_env_, is_main);
689-
if (parent_env_->is_main_thread()) {
689+
if (parent_env_->owns_inspector()) {
690690
CHECK_EQ(start_io_thread_async_initialized.exchange(true), false);
691691
CHECK_EQ(0, uv_async_init(parent_env_->event_loop(),
692692
&start_io_thread_async,

src/node.cc

Lines changed: 9 additions & 2 deletions

0 commit comments

Comments
 (0)