Repository navigation
feat: UtilityProcess API - #34980
feat: UtilityProcess API#34980
Conversation
| array<EnvironmentVariable> environment; | ||
| }; | ||
|
|
||
| [ServiceSandbox=sandbox.mojom.Sandbox.kNoSandbox] |
There was a problem hiding this comment.
🤔 It could be interesting to allow users to sandbox their utility processes to various degrees!
There was a problem hiding this comment.
Agreed, I was also hoping to expose this but current content API is not flexible and extracts the value from mojom parameter. If we can add a small patch then we can make the sandbox property configurable https://source.chromium.org/chromium/chromium/src/+/main:content/public/browser/service_process_host.h;l=141-158.
Also need to think about which ones are supported via this API from the list https://source.chromium.org/chromium/chromium/src/+/main:sandbox/policy/mojom/sandbox.mojom, we would also need to document what syscalls are allowed/disallowed, paths accessible for each of the type to make this API understandable.
There was a problem hiding this comment.
I wonder if we could provide full control over the sandbox? Maybe not that useful... but interesting! e.g. allow the app to specify individual paths/libraries/syscalls etc to allow.
504a928 to
d2eee14
Compare
5f8e796 to
b669d3f
Compare
|
@deepak1556 it would be great to get an expanded explainer for this! Adding |
|
Sorry I was planning to update before requesting review, @nornagon updated the explainer section, PTAL. Thanks! |
9107e61 to
b189445
Compare
51a20b5 to
b9dc071
Compare
b9dc071 to
c57dfb5
Compare
|
Have created #36074 as follow-up for the docs change. Allows for easier reivew, I will merge this PR today once build is green. |
|
Failing test is unrelated and is also flaky on main branch, merging. |
|
Release Notes Persisted
|
|
I was unable to backport this PR to "21-x-y" cleanly; |
* chore: initial scaffolding * chore: implement interface and docs * chore: address code style review * fix: cleanup of utility process on shutdown * chore: simplify NodeBindings::CreateEnvironment * chore: rename disableLibraryValidation => allowLoadingUnsignedLibraries * chore: implement process.parentPort * chore(posix): implement stdio pipe interface * chore(win): implement stdio interface * chore: reenable SetNodeOptions for utility process * chore: add specs * chore: fix lint * fix: update kill API * fix: update process.parentPort API * fix: exit event * docs: update exit event * fix: tests on linux * chore: expand on some comments * fix: shutdown of pipe reader Avoid logging since it is always the case that reader end of pipe will terminate after the child process. * fix: remove exit code check for crash spec * fix: rm PR_SET_NO_NEW_PRIVS for unsandbox utility process * chore: fix incorrect rebase * fix: address review feedback * chore: rename utility_process -> utility * chore: update docs * chore: cleanup c++ implemantation * fix: leak in NodeServiceHost impl * chore: minor cleanup * chore: cleanup JS implementation * chore: flip default stdio to inherit * fix: some api improvements * Support cwd option * Remove path restriction for modulePath * Rewire impl for env support * fix: add tests for cwd and env option * chore: alt impl for reading stdio handles * chore: support message queuing * chore: fix lint * chore: new UtilityProcess => utilityProcess.fork * fix: support for uncaught exception exits * chore: remove process.execArgv as default * fix: windows build * fix: style changes * fix: docs and style changes * chore: update patches * spec: disable flaky test on win32 arm CI Co-authored-by: PatchUp <73610968+patchup[bot]@users.noreply.github.com> Co-authored-by: Robo <[email protected]>
|
I have automatically backported this PR to "22-x-y", please check out #36089 |
* chore: initial scaffolding * chore: implement interface and docs * chore: address code style review * fix: cleanup of utility process on shutdown * chore: simplify NodeBindings::CreateEnvironment * chore: rename disableLibraryValidation => allowLoadingUnsignedLibraries * chore: implement process.parentPort * chore(posix): implement stdio pipe interface * chore(win): implement stdio interface * chore: reenable SetNodeOptions for utility process * chore: add specs * chore: fix lint * fix: update kill API * fix: update process.parentPort API * fix: exit event * docs: update exit event * fix: tests on linux * chore: expand on some comments * fix: shutdown of pipe reader Avoid logging since it is always the case that reader end of pipe will terminate after the child process. * fix: remove exit code check for crash spec * fix: rm PR_SET_NO_NEW_PRIVS for unsandbox utility process * chore: fix incorrect rebase * fix: address review feedback * chore: rename utility_process -> utility * chore: update docs * chore: cleanup c++ implemantation * fix: leak in NodeServiceHost impl * chore: minor cleanup * chore: cleanup JS implementation * chore: flip default stdio to inherit * fix: some api improvements * Support cwd option * Remove path restriction for modulePath * Rewire impl for env support * fix: add tests for cwd and env option * chore: alt impl for reading stdio handles * chore: support message queuing * chore: fix lint * chore: new UtilityProcess => utilityProcess.fork * fix: support for uncaught exception exits * chore: remove process.execArgv as default * fix: windows build * fix: style changes * fix: docs and style changes * chore: update patches * spec: disable flaky test on win32 arm CI Co-authored-by: PatchUp <73610968+patchup[bot]@users.noreply.github.com> Co-authored-by: Robo <[email protected]>
* feat: UtilityProcess API (#34980) * chore: initial scaffolding * chore: implement interface and docs * chore: address code style review * fix: cleanup of utility process on shutdown * chore: simplify NodeBindings::CreateEnvironment * chore: rename disableLibraryValidation => allowLoadingUnsignedLibraries * chore: implement process.parentPort * chore(posix): implement stdio pipe interface * chore(win): implement stdio interface * chore: reenable SetNodeOptions for utility process * chore: add specs * chore: fix lint * fix: update kill API * fix: update process.parentPort API * fix: exit event * docs: update exit event * fix: tests on linux * chore: expand on some comments * fix: shutdown of pipe reader Avoid logging since it is always the case that reader end of pipe will terminate after the child process. * fix: remove exit code check for crash spec * fix: rm PR_SET_NO_NEW_PRIVS for unsandbox utility process * chore: fix incorrect rebase * fix: address review feedback * chore: rename utility_process -> utility * chore: update docs * chore: cleanup c++ implemantation * fix: leak in NodeServiceHost impl * chore: minor cleanup * chore: cleanup JS implementation * chore: flip default stdio to inherit * fix: some api improvements * Support cwd option * Remove path restriction for modulePath * Rewire impl for env support * fix: add tests for cwd and env option * chore: alt impl for reading stdio handles * chore: support message queuing * chore: fix lint * chore: new UtilityProcess => utilityProcess.fork * fix: support for uncaught exception exits * chore: remove process.execArgv as default * fix: windows build * fix: style changes * fix: docs and style changes * chore: update patches * spec: disable flaky test on win32 arm CI Co-authored-by: PatchUp <73610968+patchup[bot]@users.noreply.github.com> Co-authored-by: Robo <[email protected]> * chore: update patches * docs: add utility process info to tutorial docs (#36074) Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: Robo <[email protected]>
* chore: initial scaffolding * chore: implement interface and docs * chore: address code style review * fix: cleanup of utility process on shutdown * chore: simplify NodeBindings::CreateEnvironment * chore: rename disableLibraryValidation => allowLoadingUnsignedLibraries * chore: implement process.parentPort * chore(posix): implement stdio pipe interface * chore(win): implement stdio interface * chore: reenable SetNodeOptions for utility process * chore: add specs * chore: fix lint * fix: update kill API * fix: update process.parentPort API * fix: exit event * docs: update exit event * fix: tests on linux * chore: expand on some comments * fix: shutdown of pipe reader Avoid logging since it is always the case that reader end of pipe will terminate after the child process. * fix: remove exit code check for crash spec * fix: rm PR_SET_NO_NEW_PRIVS for unsandbox utility process * chore: fix incorrect rebase * fix: address review feedback * chore: rename utility_process -> utility * chore: update docs * chore: cleanup c++ implemantation * fix: leak in NodeServiceHost impl * chore: minor cleanup * chore: cleanup JS implementation * chore: flip default stdio to inherit * fix: some api improvements * Support cwd option * Remove path restriction for modulePath * Rewire impl for env support * fix: add tests for cwd and env option * chore: alt impl for reading stdio handles * chore: support message queuing * chore: fix lint * chore: new UtilityProcess => utilityProcess.fork * fix: support for uncaught exception exits * chore: remove process.execArgv as default * fix: windows build * fix: style changes * fix: docs and style changes * chore: update patches * spec: disable flaky test on win32 arm CI Co-authored-by: PatchUp <73610968+patchup[bot]@users.noreply.github.com>
Kill() now only delivers the signal: base::Process::Terminate() sends SIGTERM on POSIX and calls TerminateProcess on Windows, and nothing else happens. The base::EnsureProcessTerminated call introduced in 489d3ac (#34980) is gone on every platform, and with it the ThreadPool SIGKILL fallback from the previous commit. EnsureProcessTerminated reaped the child itself and SIGKILLed it after 2s, which raced content's BrowserChildProcessHost reap (its kill()/waitpid() then failed with ESRCH/ECHILD and logged two ERROR lines per kill) and force-killed a child that handles SIGTERM. Like Node's child_process.kill(), delivery is now all kill() guarantees; content reaps the child once its mojo pipe drops. The docs for child.kill() describe the new contract, with an API history entry, and a POSIX spec forks a child whose SIGTERM handler exits with 42 after 2.5s and asserts that kill() lets it. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01AFucMLGyVVqSUAKXrqbaUT
* fix: don't reap utilityProcess children in kill() UtilityProcessWrapper::Kill() opened a second base::Process for the child, sent SIGTERM and then handed it to base::EnsureProcessTerminated, which on POSIX reaps the child itself (immediately via kqueue on macOS, after a 2s delay on Linux). content's BrowserChildProcessHostImpl reaps the same child once its mojo pipe drops, via base::GetKnownDeadTerminationStatus. Whichever side lost that race failed: content's kill(SIGKILL) hit ESRCH and its waitpid() hit ECHILD, logging "ERROR:base/process/kill_posix.cc:87 Unable to terminate process" and "kill_posix.cc:33 waitpid(): No child processes" (about 1,200 lines per main CI run), and the exit code content reported was derived from a status that was already gone. content reaps reliably on its own, so only keep base's helper on Windows. On POSIX send SIGTERM and post a delayed SIGKILL that fires only if the wrapper has not yet seen the child terminate, matching the 2s grace period base::EnsureProcessTerminated used. waitpid now reports the signal, which HandleTermination already maps to exit code 0. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01AFucMLGyVVqSUAKXrqbaUT * fix: run the utilityProcess SIGKILL fallback off the UI thread Review feedback on the previous commit: the delayed SIGKILL was posted to the UI thread, so it could not run while main-process JavaScript kept that sequence busy, and a child that ignores SIGTERM could outlive kill() indefinitely. The base::EnsureProcessTerminated call it replaced enforced its two-second deadline on a background thread. Post the fallback to a ThreadPool sequence (MayBlock, BEST_EFFORT) instead. The task takes only the pid and the child's base::Process::CreationTime(), read before SIGTERM while the child is certainly alive, and re-reads the creation time when it fires: once content has reaped the child the pid yields a null time, and a reused pid yields a different one, so neither is signalled. A not-yet-reaped zombie matches and accepts SIGKILL harmlessly. No wrapper state is touched off the UI thread, so nothing has to be marshalled back, and the ForceKill member goes away. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01AFucMLGyVVqSUAKXrqbaUT * fix: align utilityProcess kill() with child_process.kill() Kill() now only delivers the signal: base::Process::Terminate() sends SIGTERM on POSIX and calls TerminateProcess on Windows, and nothing else happens. The base::EnsureProcessTerminated call introduced in 489d3ac (#34980) is gone on every platform, and with it the ThreadPool SIGKILL fallback from the previous commit. EnsureProcessTerminated reaped the child itself and SIGKILLed it after 2s, which raced content's BrowserChildProcessHost reap (its kill()/waitpid() then failed with ESRCH/ECHILD and logged two ERROR lines per kill) and force-killed a child that handles SIGTERM. Like Node's child_process.kill(), delivery is now all kill() guarantees; content reaps the child once its mojo pipe drops. The docs for child.kill() describe the new contract, with an API history entry, and a POSIX spec forks a child whose SIGTERM handler exits with 42 after 2.5s and asserts that kill() lets it. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01AFucMLGyVVqSUAKXrqbaUT * docs: record the utilityProcess kill() change in breaking-changes.md Apply review nits: drop the API-history block under `child.kill()`, note the behavior change under Breaking API Changes (46.0) in docs/breaking-changes.md instead, and say that the child is reaped by Electron on POSIX. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01AFucMLGyVVqSUAKXrqbaUT --------- Co-authored-by: Claude <[email protected]>
Description of Change
Electron applications which run complex Node.js services outside the main process and have necessity to communicate with the renderer process, did it in either of the two ways,
(i) Fork a child process from the renderer via
child_process.forkand establish a direct communication channel via Node.js sockets.(ii) Fork a child process from the main via
child_process.forkand establish a direct communication channel via Node.js sockets.If the renderer is sandboxed, then (i) is not possible and with (ii) a direct socket connection cannot be established with the renderer instead messages have to be routed via the main process. In #22404 we tackled the use case of an application using a renderer process in the background to perform cpu intensive work or isolate crash prone components and allowing the capability to communicate with a sandboxed renderer using
MessageChannel.This PR is inspired by that change and introduces a new module
UtilityProcessin the main process that allows creating light weight chromium child process with only Node.js integration while also allowing to communicate with a sandboxed renderer usingMessageChannel. The API was designed based on Node.js child_process.fork to allow for easier transition with one primary difference being that the entry pointmodulePathmust be from within the packaged application to allow only for trusted scripts to be loaded unlike the Node.js version which can load from arbitrary location.Additionally the module upholds the contract that main process is the only trusted process in the application by not allowing to establish communication channels with any renderer in the application by default. Instead the main process is responsible for creating the desired communication channel and sharing the endpoints to the respective child processes.
Example Usage:
main.js
preload.js
worker.js
Future Work:
Allow sandboxing the utility process, it is possible to specify the type of sandbox for the utility process when launching it
content::UtilityProcessHost::SetSandboxTypebut it is currently restricted to set of predeclared options. I need to explore how much an embedder can extend the chromium sandbox without any serious patches and think about what level of control we can give to users. An usage example for this would be like, utility process that only performs file I/O can declare a sandbox that disallows network and child_process creating syscalls. Given the complexity of this feature, I don't plan to tackle it in this PR.Would an options to create utility process with just v8 be of any interest ? (i-e) no Node.js integration, no blink integration just vanilla v8
Expose Deno integration with utility process ???
Release Notes
Notes: Added new UtilityProcess API to launch chromium child process with node integration