Skip to content
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

Work around Java emulators + WSL connectivity issues. #2780

Merged
merged 1 commit into from
Nov 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Work around Java emulators + WSL connectivity issues.
  • Loading branch information
yuchenshi committed Nov 4, 2020
commit 7b33dc82b0b1fe12d9bb492c6cef469d366941cb
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Work around Java emulators + WSL connectivity issues.
12 changes: 11 additions & 1 deletion src/emulator/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,20 @@ import { FLAG_EXPORT_ON_EXIT_NAME } from "./commandUtils";
import { fileExistsSync } from "../fsutils";

async function getAndCheckAddress(emulator: Emulators, options: any): Promise<Address> {
const host = Constants.normalizeHost(
let host = Constants.normalizeHost(
options.config.get(Constants.getHostKey(emulator), Constants.getDefaultHost(emulator))
);

if (host === "localhost" && utils.isRunningInWSL()) {
// HACK(https://github.com/firebase/firebase-tools-ui/issues/332): Use IPv4
// 127.0.0.1 instead of localhost. This, combined with the hack in
// downloadableEmulators.ts, forces the emulator to listen on IPv4 ONLY.
// The CLI (including the hub) will also consistently report 127.0.0.1,
// causing clients to connect via IPv4 only (which mitigates the problem of
// some clients resolving localhost to IPv6 and get connection refused).
host = "127.0.0.1";
}

const portVal = options.config.get(Constants.getPortKey(emulator), undefined);
let port;
let findAvailablePort = false;
Expand Down
14 changes: 14 additions & 0 deletions src/emulator/downloadableEmulators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,20 @@ function _getCommand(

const cmdLineArgs = baseCmd.args.slice();

if (
baseCmd.binary === "java" &&
utils.isRunningInWSL() &&
(!args.host || !args.host.includes(":"))
) {
// HACK(https://github.com/firebase/firebase-tools-ui/issues/332): Force
// Java to use IPv4 sockets in WSL (unless IPv6 address explicitly used).
// Otherwise, Java will open a tcp6 socket (even if IPv4 address is used),
// which handles both 4/6 on Linux but NOT IPv4 from the host to WSL.
// This is a hack because it breaks all IPv6 connections as a side effect.
// See: https://docs.oracle.com/javase/8/docs/api/java/net/doc-files/net-properties.html
cmdLineArgs.unshift("-Djava.net.preferIPv4Stack=true"); // first argument
}

const logger = EmulatorLogger.forEmulator(emulator);
Object.keys(args).forEach((key) => {
if (!baseCmd.optionalArgs.includes(key)) {
Expand Down
8 changes: 8 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,3 +478,11 @@ export function datetimeString(d: Date): string {
export function isCloudEnvironment() {
return !!process.env.CODESPACES;
}

/**
* Indicates whether or not this process is likely to be running in WSL.
* @return true if we're likely in WSL, false otherwise
*/
export function isRunningInWSL(): boolean {
return !!process.env.WSL_DISTRO_NAME;
}