Skip to content

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

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

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
5 changes: 5 additions & 0 deletions docs/api/command-line-switches.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ Disables the Chromium [sandbox](https://www.chromium.org/developers/design-docum
Forces renderer process and Chromium helper processes to run un-sandboxed.
Should only be used for testing.

### --no-stdio-init

Disable stdio initialization during node initialization.
Used to avoid node initialization crash when the nul device is disabled on Windows platform.

### --proxy-bypass-list=`hosts`

Instructs Electron to bypass the proxy server for the given semi-colon-separated
Expand Down
29 changes: 25 additions & 4 deletions shell/app/node_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,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"
Expand Down Expand Up @@ -180,14 +182,33 @@ 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;

base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
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;
Copy link

@t13m t13m Jul 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @yangllu , thanks for your work. I've tried this patch once, here is what I found:

it seems that in a windows machine without NUL device, the main process works normally, but the nodejs integrated with renderer processes still don't work, though --no-stdio-init is provided from command line. maybe the reason is that the command line flag is erased here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, very likely need to pass the flag in

if (process_type == ::switches::kRendererProcess) {

std::erase(args, option);
} else {
#if BUILDFLAG(IS_WIN)
if (!platform_util::IsNulDeviceEnabled()) {
LOG(FATAL) << "Fail to open nul device and node initialization may "
"crash! Try using --"
<< switches::kNoStdioInit;
Comment on lines +201 to +203
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
LOG(FATAL) << "Fail to open nul device and node initialization may "
"crash! Try using --"
<< switches::kNoStdioInit;
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())
fprintf(stderr, "%s: %s\n", args[0].c_str(), error.c_str());
Expand Down
1 change: 1 addition & 0 deletions shell/browser/api/electron_api_utility_process.cc
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ UtilityProcessWrapper::UtilityProcessWrapper(
OPEN_EXISTING, 0, nullptr);
if (handle == INVALID_HANDLE_VALUE) {
PLOG(ERROR) << "Failed to create null handle";
Emit("error", "Failed to create null handle for ignoring stdio");
return;
}
if (io_handle == IOHandle::STDOUT) {
Expand Down
15 changes: 15 additions & 0 deletions shell/common/node_bindings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
#include "shell/common/mac/main_application_bundle.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"
#include "shell/common/process_util.h"
#include "shell/common/world_ids.h"
#include "third_party/blink/public/web/web_local_frame.h"
Expand Down Expand Up @@ -568,6 +570,19 @@ void NodeBindings::Initialize(v8::Local<v8::Context> context) {
if (!fuses::IsNodeOptionsEnabled())
process_flags |= node::ProcessInitializationFlags::kDisableNodeOptionsEnv;

base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
if (command_line->HasSwitch(switches::kNoStdioInit)) {
process_flags |= node::ProcessInitializationFlags::kNoStdioInitialization;
} else {
#if BUILDFLAG(IS_WIN)
if (!platform_util::IsNulDeviceEnabled()) {
LOG(FATAL) << "Fail to open nul device and node initialization may "
"crash! Try using --"
<< switches::kNoStdioInit;
Comment on lines +579 to +581
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
LOG(FATAL) << "Fail to open nul device and node initialization may "
"crash! Try using --"
<< switches::kNoStdioInit;
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,
Expand Down
4 changes: 4 additions & 0 deletions shell/common/options_switches.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,10 @@ inline constexpr base::cstring_view kEnableAuthNegotiatePort =
// If set, NTLM v2 is disabled for POSIX platforms.
inline constexpr base::cstring_view kDisableNTLMv2 = "disable-ntlm-v2";

// If set, flag node::ProcessInitializationFlags::kNoStdioInitialization would
// be set for node initialization.
inline constexpr base::cstring_view kNoStdioInit = "no-stdio-init";

} // namespace switches

} // namespace electron
Expand Down
3 changes: 3 additions & 0 deletions shell/common/platform_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ void Beep();
#if BUILDFLAG(IS_WIN)
// SHGetFolderPath calls not covered by Chromium
bool GetFolderPath(int key, base::FilePath* result);

// Check if nul device can be used.
bool IsNulDeviceEnabled();
#endif

#if BUILDFLAG(IS_MAC)
Expand Down
14 changes: 14 additions & 0 deletions shell/common/platform_util_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include <comdef.h>
#include <commdlg.h>
#include <dwmapi.h>
#include <fcntl.h>
#include <io.h>
#include <objbase.h>
#include <shellapi.h>
#include <shlobj.h>
Expand Down Expand Up @@ -450,4 +452,16 @@ void Beep() {
MessageBeep(MB_OK);
}

bool IsNulDeviceEnabled() {
bool ret = true;
int fd = _open("nul", _O_RDWR);
if (fd < 0) {
ret = false;
} else {
ret = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ret = true;

_close(fd);
}
return ret;
}

} // namespace platform_util