fix: bootstrap the node environment after we setup the InspectorAgent (#19317)

This commit is contained in:
Samuel Attard
2019-07-18 16:54:23 -07:00
committed by GitHub
parent 2467350180
commit 6fc648cd25
7 changed files with 117 additions and 9 deletions

View File

@@ -285,13 +285,44 @@ void AtomBrowserMainParts::PostEarlyInitialization() {
node_bindings_->Initialize();
// Create the global environment.
node::Environment* env = node_bindings_->CreateEnvironment(
js_env_->context(), js_env_->platform());
js_env_->context(), js_env_->platform(), false);
node_env_.reset(new NodeEnvironment(env));
/**
* 🚨 🚨 🚨 🚨 🚨 🚨 🚨 🚨 🚨
* UNSAFE ENVIRONMENT BLOCK BEGINS
*
* DO NOT USE node::Environment inside this block, bad things will happen
* and you won't be able to figure out why. Just don't touch it, the only
* thing that can use it is NodeDebugger and that is ONLY allowed to access
* the inspector agent.
*
* This is unsafe because the environment is not yet bootstrapped, it's a race
* condition where we can't bootstrap before intializing the inspector agent.
*
* Long term we should figure out how to get node to initialize the inspector
* agent in the correct place without us splitting the bootstrap up, but for
* now this works.
* 🚨 🚨 🚨 🚨 🚨 🚨 🚨 🚨 🚨
*/
// Enable support for v8 inspector
node_debugger_.reset(new NodeDebugger(env));
node_debugger_->Start();
// Only run the node bootstrapper after we have initialized the inspector
// TODO(MarshallOfSound): Figured out a better way to init the inspector
// before bootstrapping
node::BootstrapEnvironment(env);
/**
* ✅ ✅ ✅ ✅ ✅ ✅ ✅
* UNSAFE ENVIRONMENT BLOCK ENDS
*
* Do whatever you want now with that env, it's safe again
* ✅ ✅ ✅ ✅ ✅ ✅ ✅
*/
// Add Electron extended APIs.
electron_bindings_->BindTo(js_env_->isolate(), env->process_object());

View File

@@ -288,7 +288,8 @@ void NodeBindings::Initialize() {
node::Environment* NodeBindings::CreateEnvironment(
v8::Handle<v8::Context> context,
node::MultiIsolatePlatform* platform) {
node::MultiIsolatePlatform* platform,
bool bootstrap_env) {
#if defined(OS_WIN)
auto& atom_args = AtomCommandLine::argv();
std::vector<std::string> args(atom_args.size());
@@ -327,13 +328,17 @@ node::Environment* NodeBindings::CreateEnvironment(
std::unique_ptr<const char*[]> c_argv = StringVectorToArgArray(args);
node::Environment* env = node::CreateEnvironment(
node::CreateIsolateData(context->GetIsolate(), uv_loop_, platform),
context, args.size(), c_argv.get(), 0, nullptr);
context, args.size(), c_argv.get(), 0, nullptr, bootstrap_env);
DCHECK(env);
// Clean up the global _noBrowserGlobals that we unironically injected into
// the global scope
if (browser_env_ != BrowserEnvironment::BROWSER)
if (browser_env_ != BrowserEnvironment::BROWSER) {
// We need to bootstrap the env in non-browser processes so that
// _noBrowserGlobals is read correctly before we remove it
DCHECK(bootstrap_env);
global.Delete("_noBrowserGlobals");
}
if (browser_env_ == BrowserEnvironment::BROWSER) {
// SetAutorunMicrotasks is no longer called in node::CreateEnvironment

View File

@@ -41,9 +41,9 @@ class NodeBindings {
void Initialize();
// Create the environment and load node.js.
node::Environment* CreateEnvironment(
v8::Handle<v8::Context> context,
node::MultiIsolatePlatform* platform = nullptr);
node::Environment* CreateEnvironment(v8::Handle<v8::Context> context,
node::MultiIsolatePlatform* platform,
bool bootstrap_env);
// Load node.js in the environment.
void LoadEnvironment(node::Environment* env);

View File

@@ -107,7 +107,8 @@ void AtomRendererClient::DidCreateScriptContext(
v8::Local<v8::Context> context =
node::MaybeInitializeContext(renderer_context);
DCHECK(!context.IsEmpty());
node::Environment* env = node_bindings_->CreateEnvironment(context);
node::Environment* env =
node_bindings_->CreateEnvironment(context, nullptr, true);
auto* command_line = base::CommandLine::ForCurrentProcess();
// If we have disabled the site instance overrides we should prevent loading
// any non-context aware native module

View File

@@ -50,7 +50,8 @@ void WebWorkerObserver::ContextCreated(v8::Local<v8::Context> worker_context) {
// Setup node environment for each window.
v8::Local<v8::Context> context = node::MaybeInitializeContext(worker_context);
DCHECK(!context.IsEmpty());
node::Environment* env = node_bindings_->CreateEnvironment(context);
node::Environment* env =
node_bindings_->CreateEnvironment(context, nullptr, true);
// Add Electron extended APIs.
electron_bindings_->BindTo(env->isolate(), env->process_object());