Compare commits

...

2 Commits

Author SHA1 Message Date
João Silva
792b6a40d1 fix: narrow HandleTermination and Shutdown to uint32_t, add tests
Signed-off-by: John Kleinschmidt <kleinschmidtorama@gmail.com>
2026-03-17 15:59:49 -04:00
João Silva
e87807dcbf fix: correct utility process exit code on Windows
On Windows, process exit codes are 32-bit unsigned integers (DWORD).
When passed from Chromium to Electron as a signed int and then
implicitly converted to uint64_t, values with the high bit set
(e.g., NTSTATUS codes) undergo sign extension, producing incorrect
values.

Cast the exit code to uint32_t before widening to uint64_t to
prevent sign extension and preserve the original Windows exit code.

Fixes #49455

Signed-off-by: John Kleinschmidt <kleinschmidtorama@gmail.com>
2026-03-17 15:59:39 -04:00
3 changed files with 20 additions and 4 deletions

View File

@@ -244,7 +244,7 @@ void UtilityProcessWrapper::OnServiceProcessLaunch(
EmitWithoutEvent("spawn");
}
void UtilityProcessWrapper::HandleTermination(uint64_t exit_code) {
void UtilityProcessWrapper::HandleTermination(uint32_t exit_code) {
// HandleTermination is called from multiple callsites,
// we need to ensure we only process it for the first callsite.
if (terminated_)
@@ -312,7 +312,7 @@ void UtilityProcessWrapper::CloseConnectorPort() {
}
}
void UtilityProcessWrapper::Shutdown(uint64_t exit_code) {
void UtilityProcessWrapper::Shutdown(uint32_t exit_code) {
node_service_remote_.reset();
HandleTermination(exit_code);
}

View File

@@ -58,7 +58,7 @@ class UtilityProcessWrapper final
static gin_helper::Handle<UtilityProcessWrapper> Create(gin::Arguments* args);
static raw_ptr<UtilityProcessWrapper> FromProcessId(base::ProcessId pid);
void Shutdown(uint64_t exit_code);
void Shutdown(uint32_t exit_code);
// gin_helper::Wrappable
static gin::DeprecatedWrapperInfo kWrapperInfo;
@@ -78,7 +78,7 @@ class UtilityProcessWrapper final
void OnServiceProcessLaunch(const base::Process& process);
void CloseConnectorPort();
void HandleTermination(uint64_t exit_code);
void HandleTermination(uint32_t exit_code);
void PostMessage(gin::Arguments* args);
bool Kill();

View File

@@ -129,6 +129,22 @@ describe('utilityProcess module', () => {
expect(code).to.equal(exitCode);
});
ifit(process.platform === 'win32')('emits correct exit code when high bit is set on Windows', async () => {
// NTSTATUS code with high bit set should not be mangled by sign extension.
const exitCode = 0xC0000005;
const child = utilityProcess.fork(path.join(fixturesPath, 'custom-exit.js'), [`--exitCode=${exitCode}`]);
const [code] = await once(child, 'exit');
expect(code).to.equal(exitCode);
});
ifit(process.platform !== 'win32')('emits correct exit code when child process crashes on posix', async () => {
// Crash exit codes should not be sign-extended to large 64-bit values.
const child = utilityProcess.fork(path.join(fixturesPath, 'crash.js'));
const [code] = await once(child, 'exit');
expect(code).to.not.equal(0);
expect(code).to.be.lessThanOrEqual(0xFFFFFFFF);
});
it('does not run JS after process.exit is called', async () => {
const file = path.join(os.tmpdir(), `no-js-after-exit-log-${Math.random()}`);
const child = utilityProcess.fork(path.join(fixturesPath, 'no-js-after-exit.js'), [`--testPath=${file}`]);