Skip to content

fix(emulators): prevent extra console windows and orphaned java.exe - #11384

Merged
christhompsongoogle merged 5 commits into
mainfrom
windowsFix
Nov 4, 2026
Merged

christhompsongoogle merged 5 commits into
mainfrom
windowsFix

Conversation

@christhompsongoogle

Copy link
Copy Markdown
Contributor

…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

firebase emulators:start --only firestore,database,pubsub,storage

Description

Scenarios Tested

Sample Commands

…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
```

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Comment on lines +505 to +537
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";
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

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
  1. 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)

Comment thread src/emulator/commandUtils.ts Outdated
Comment on lines +608 to +609
const javaBinName = utils.IS_WINDOWS ? "java.exe" : "java";
const candidate = path.join(javaHome, "bin", javaBinName);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Use the newly extracted JAVA_BINARY_NAME constant here to avoid duplicate definition of the Java binary name.

Suggested change
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
  1. Survey prior art & harmonize utilities: Reuse or extend existing utilities rather than writing bespoke inline logic. (link)

@joehan joehan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this LGTM, tho my windows skills are deeply lacking.

@joehan joehan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this LGTM, tho my windows skills are deeply lacking.

@joehan

joehan commented Oct 29, 2026

Copy link
Copy Markdown
Member

Also lint it

@christhompsongoogle
christhompsongoogle enabled auto-merge (squash) November 4, 2026 01:58
@christhompsongoogle
christhompsongoogle merged commit 91bf8ac into main Nov 4, 2026
51 checks passed
@christhompsongoogle
christhompsongoogle deleted the windowsFix branch November 4, 2026 02:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

starting emulators opens a java window that isn't directly controlled by emulators

2 participants