Repository navigation
fix(emulators): prevent extra console windows and orphaned java.exe - #11384
Conversation
…n Windows (#3871, #11261) ### Description Fixes #3871, #11261, #3506, and #8007 (internal: b/247008279, b/572430891). On Windows, starting Java-based emulators (`firestore`, `database`, `pubsub`, `storage`) could pop up a separate console window for `java.exe` and leave `java.exe` orphaned after the Emulator Suite exits: 1. **Intermediate launchers under `DETACHED_PROCESS`**: `_runBinary` spawns emulators with `detached: true` (`DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP` on Windows) so interactive `Ctrl+C` does not terminate child emulators before `--export-on-exit` runs. While the immediate child spawned by `node.exe` has no console, if that child is an intermediate wrapper (`cmd.exe` running `cloud-pubsub-emulator.bat`, or an Oracle `Common Files\Oracle\Java\javapath\java.exe` / package-manager shim on `PATH`) that spawns a grandchild `java.exe` without `CREATE_NO_WINDOW`, Windows allocates a brand-new visible console window for the grandchild `java.exe`. Moreover, `emulator.instance.pid` only tracks the wrapper PID, so stopping the emulator terminates the wrapper while leaving `java.exe` running and holding the port. 2. **`libuv` `UV_INHERIT_FD` check**: `libuv` on Windows ignores `windowsHide: true` (`CREATE_NO_WINDOW`) whenever any `stdio` stream is set to `"inherit"`. #### Changes - **`src/emulator/commandUtils.ts`**: Pass `-XshowSettings:properties`, `windowsHide: true`, and `stdio: ["ignore", "pipe", "pipe"]` in `checkJavaMajorVersion()`, and resolve the real `<java.home>/bin/java(.exe)` binary (`getJavaBinary()`) so subsequent Java spawns bypass `PATH` shims on Windows. - **`src/emulator/downloadableEmulators.ts`**: - On Windows (when `PUBSUB_EMULATOR_BINARY_PATH` is not set), launch the Pub/Sub emulator's fat jar (`cloud-pubsub-emulator-<version>-all.jar`) directly via `getJavaBinary()` with `shell: false` instead of `cloud-pubsub-emulator.bat` via `cmd.exe`. - Pass `windowsHide: true` and `stdio: ["ignore", "pipe", "pipe"]` (while keeping `detached: true`) in `_runBinary()`. - Include `Emulators.PUBSUB` in `requiresJava()` so `checkJavaMajorVersion()` runs when starting Pub/Sub standalone. - **`src/emulator/storage/rules/runtime.ts`**: Pass `windowsHide: true` when spawning the Storage Rules runtime process. ### Scenarios Tested - Unit tests in `src/emulator/commandUtils.spec.ts` and `src/emulator/downloadableEmulators.spec.ts`. - Verified `java.home` resolution from `-XshowSettings:properties -version` and direct `-jar` invocation of `cloud-pubsub-emulator-0.8.37-all.jar`. ### Sample Commands ```bash firebase emulators:start --only firestore,database,pubsub,storage ```
There was a problem hiding this comment.
Code Review
This pull request addresses an issue on Windows where starting Java-based emulators could open extra console windows or leave orphaned processes. It introduces a mechanism to resolve and invoke the real java binary directly from java.home or JAVA_HOME instead of relying on a PATH wrapper shim, and configures process spawning with windowsHide: true and shell: false where appropriate. The review feedback suggests adhering to the repository style guide by extracting the platform-specific Java binary name logic into a top-level constant JAVA_BINARY_NAME and improving robustness by stripping potential surrounding quotes from process.env.JAVA_HOME.
| const JAVA_HOME_REGEX = /^\s*java\.home\s*=\s*(.+)$/m; | ||
| const JAVA_HINT = "Please make sure Java is installed and on your system PATH."; | ||
|
|
||
| let resolvedJavaBinary: string | undefined; | ||
|
|
||
| /** | ||
| * Reset the cached Java binary path (for unit testing). | ||
| */ | ||
| export function resetJavaBinaryForTesting(): void { | ||
| resolvedJavaBinary = undefined; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the resolved path to the real `java` binary inside `java.home` (if discovered by | ||
| * `checkJavaMajorVersion()` or `JAVA_HOME`), falling back to `"java"` on `PATH`. | ||
| * | ||
| * On Windows, invoking the real `<java.home>\bin\java.exe` directly instead of a `PATH` wrapper | ||
| * shim (such as `C:\Program Files\Common Files\Oracle\Java\javapath\java.exe`) prevents the shim | ||
| * from spawning a grandchild `java.exe` in a new visible console window and breaking PID tracking. | ||
| */ | ||
| export function getJavaBinary(): string { | ||
| if (resolvedJavaBinary) { | ||
| return resolvedJavaBinary; | ||
| } | ||
| if (process.env.JAVA_HOME) { | ||
| const javaBinName = utils.IS_WINDOWS ? "java.exe" : "java"; | ||
| const candidate = path.join(process.env.JAVA_HOME, "bin", javaBinName); | ||
| if (fsutils.fileExistsSync(candidate)) { | ||
| return candidate; | ||
| } | ||
| } | ||
| return "java"; | ||
| } |
There was a problem hiding this comment.
To adhere to the repository style guide, we should extract the duplicated Java binary name logic into a top-level constant JAVA_BINARY_NAME. Additionally, we can improve robustness by stripping potential surrounding quotes from process.env.JAVA_HOME (which is a common issue on Windows).
const JAVA_HOME_REGEX = /^\s*java\.home\s*=\s*(.+)$/m;
const JAVA_HINT = "Please make sure Java is installed and on your system PATH.";
const JAVA_BINARY_NAME = utils.IS_WINDOWS ? "java.exe" : "java";
let resolvedJavaBinary: string | undefined;
/**
* Reset the cached Java binary path (for unit testing).
*/
export function resetJavaBinaryForTesting(): void {
resolvedJavaBinary = undefined;
}
/**
* Returns the resolved path to the real java binary inside java.home (if discovered by
* checkJavaMajorVersion() or JAVA_HOME), falling back to "java" on PATH.
*
* On Windows, invoking the real <java.home>\bin\java.exe directly instead of a PATH wrapper
* shim (such as C:\\Program Files\\Common Files\\Oracle\\Java\\javapath\\java.exe) prevents the shim
* from spawning a grandchild java.exe in a new visible console window and breaking PID tracking.
*/
export function getJavaBinary(): string {
if (resolvedJavaBinary) {
return resolvedJavaBinary;
}
if (process.env.JAVA_HOME) {
const javaHome = process.env.JAVA_HOME.replace(/^"(.*)"$/, "$1");
const candidate = path.join(javaHome, "bin", JAVA_BINARY_NAME);
if (fsutils.fileExistsSync(candidate)) {
return candidate;
}
}
return "java";
}References
- Extract magic numbers and static constants: Define static IDs, default timeouts, millisecond offsets, and repeated strings as top-level constants (UPPER_SNAKE_CASE) instead of inlining magic values inside functions. (link)
| const javaBinName = utils.IS_WINDOWS ? "java.exe" : "java"; | ||
| const candidate = path.join(javaHome, "bin", javaBinName); |
There was a problem hiding this comment.
Use the newly extracted JAVA_BINARY_NAME constant here to avoid duplicate definition of the Java binary name.
| const javaBinName = utils.IS_WINDOWS ? "java.exe" : "java"; | |
| const candidate = path.join(javaHome, "bin", javaBinName); | |
| const candidate = path.join(javaHome, "bin", JAVA_BINARY_NAME); |
References
- Survey prior art & harmonize utilities: Reuse or extend existing utilities rather than writing bespoke inline logic. (link)
…recedence, and avoid Java 21 gate for Pub/Sub
joehan
left a comment
There was a problem hiding this comment.
I think this LGTM, tho my windows skills are deeply lacking.
joehan
left a comment
There was a problem hiding this comment.
I think this LGTM, tho my windows skills are deeply lacking.
|
Also lint it |
…n Windows (#3871, #11261)
Description
Fixes #3871, #11261, #3506, and #8007 (internal: b/247008279, b/572430891).
On Windows, starting Java-based emulators (
firestore,database,pubsub,storage) could pop up a separate console window forjava.exeand leavejava.exeorphaned after the Emulator Suite exits:DETACHED_PROCESS:_runBinaryspawns emulators withdetached: true(DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUPon Windows) so interactiveCtrl+Cdoes not terminate child emulators before--export-on-exitruns. While the immediate child spawned bynode.exehas no console, if that child is an intermediate wrapper (cmd.exerunningcloud-pubsub-emulator.bat, or an OracleCommon Files\Oracle\Java\javapath\java.exe/ package-manager shim onPATH) that spawns a grandchildjava.exewithoutCREATE_NO_WINDOW, Windows allocates a brand-new visible console window for the grandchildjava.exe. Moreover,emulator.instance.pidonly tracks the wrapper PID, so stopping the emulator terminates the wrapper while leavingjava.exerunning and holding the port.libuvUV_INHERIT_FDcheck:libuvon Windows ignoreswindowsHide: true(CREATE_NO_WINDOW) whenever anystdiostream is set to"inherit".Changes
src/emulator/commandUtils.ts: Pass-XshowSettings:properties,windowsHide: true, andstdio: ["ignore", "pipe", "pipe"]incheckJavaMajorVersion(), and resolve the real<java.home>/bin/java(.exe)binary (getJavaBinary()) so subsequent Java spawns bypassPATHshims on Windows.src/emulator/downloadableEmulators.ts:PUBSUB_EMULATOR_BINARY_PATHis not set), launch the Pub/Sub emulator's fat jar (cloud-pubsub-emulator-<version>-all.jar) directly viagetJavaBinary()withshell: falseinstead ofcloud-pubsub-emulator.batviacmd.exe.windowsHide: trueandstdio: ["ignore", "pipe", "pipe"](while keepingdetached: true) in_runBinary().Emulators.PUBSUBinrequiresJava()socheckJavaMajorVersion()runs when starting Pub/Sub standalone.src/emulator/storage/rules/runtime.ts: PasswindowsHide: truewhen spawning the Storage Rules runtime process.Scenarios Tested
src/emulator/commandUtils.spec.tsandsrc/emulator/downloadableEmulators.spec.ts.java.homeresolution from-XshowSettings:properties -versionand direct-jarinvocation ofcloud-pubsub-emulator-0.8.37-all.jar.Sample Commands
Description
Scenarios Tested
Sample Commands