fix: launch crash when null device is disabled on Windows (#47870)

fix: fix launch crash when null device is disabled on Windows

add node flag node::ProcessInitializationFlags::kNoStdioInitialization

Co-authored-by: yangzuohui <yangzuohui@bytedance.com>
Co-authored-by: yangliu <yangliu.leo@bytedance.com>
This commit is contained in:
Zuohui Yang
2025-10-12 06:00:04 +08:00
committed by GitHub
parent cbf5c3331f
commit 16b5776b01
8 changed files with 69 additions and 6 deletions

View File

@@ -35,6 +35,8 @@
#include "shell/common/node_bindings.h"
#include "shell/common/node_includes.h"
#include "shell/common/node_util.h"
#include "shell/common/options_switches.h"
#include "shell/common/platform_util.h"
#if BUILDFLAG(IS_WIN)
#include "chrome/child/v8_crashpad_support_win.h"
@@ -153,9 +155,10 @@ int NodeMain() {
v8_crashpad_support::SetUp();
#endif
auto* command_line = base::CommandLine::ForCurrentProcess();
#if BUILDFLAG(IS_LINUX)
int pid = -1;
auto* command_line = base::CommandLine::ForCurrentProcess();
std::optional<std::string> fd_string = os_env->GetVar("CRASHDUMP_SIGNAL_FD");
std::optional<std::string> pid_string =
os_env->GetVar("CRASHPAD_HANDLER_PID");
@@ -189,14 +192,32 @@ int NodeMain() {
NodeBindings::RegisterBuiltinBindings();
// Parse Node.js cli flags and strip out disallowed options.
const std::vector<std::string> args = ElectronCommandLine::AsUtf8();
std::vector<std::string> args = ElectronCommandLine::AsUtf8();
ExitIfContainsDisallowedFlags(args);
uint64_t process_flags =
node::ProcessInitializationFlags::kNoInitializeV8 |
node::ProcessInitializationFlags::kNoInitializeNodeV8Platform;
if (command_line->HasSwitch(switches::kNoStdioInit)) {
process_flags |= node::ProcessInitializationFlags::kNoStdioInitialization;
// remove the option to avoid node error "bad option: --no-stdio-init"
std::string option = std::string("--") + switches::kNoStdioInit;
std::erase(args, option);
} else {
#if BUILDFLAG(IS_WIN)
if (!platform_util::IsNulDeviceEnabled()) {
LOG(FATAL) << "Unable to open nul device needed for initialization,"
"aborting startup. As a workaround, try starting with --"
<< switches::kNoStdioInit;
}
#endif
}
std::shared_ptr<node::InitializationResult> result =
node::InitializeOncePerProcess(
args,
{node::ProcessInitializationFlags::kNoInitializeV8,
node::ProcessInitializationFlags::kNoInitializeNodeV8Platform});
args, static_cast<node::ProcessInitializationFlags::Flags>(
process_flags));
for (const std::string& error : result->errors())
std::cerr << args[0] << ": " << error << '\n';