From da24a99b9127a5f1c3842962ed6768ec0c7dbe61 Mon Sep 17 00:00:00 2001 From: Alexey Tsvetkov Date: Fri, 17 Mar 2017 15:32:08 +0300 Subject: [PATCH] Set 'java.rmi.server.hostname' property on client too The RMI documentation (http://docs.oracle.com/javase/7/docs/technotes/guides/rmi/javarmiproperties.html) says it is useful to set up a `java.rmi.server.hostname` system property on a client and a server. When the property is set up on a client, I saw that "RenewClean" threads stopped listening to my external IP, so all client and server threads are now only listening the loopback interface. I also changed the way the property is set up on the server side: before the change it was passed in jvmargs at a process launcher. I moved this code directly to the main method of the daemon, because it is easy to forget to set up the property, when running the main for debug purposes. --- .../jetbrains/kotlin/daemon/client/KotlinCompilerClient.kt | 5 ++--- .../src/org/jetbrains/kotlin/daemon/common/NetworkUtils.kt | 7 +++++++ .../src/org/jetbrains/kotlin/daemon/KotlinCompileDaemon.kt | 1 + 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/compiler/daemon/daemon-client/src/org/jetbrains/kotlin/daemon/client/KotlinCompilerClient.kt b/compiler/daemon/daemon-client/src/org/jetbrains/kotlin/daemon/client/KotlinCompilerClient.kt index bec4ea5055e..1b8529bb996 100644 --- a/compiler/daemon/daemon-client/src/org/jetbrains/kotlin/daemon/client/KotlinCompilerClient.kt +++ b/compiler/daemon/daemon-client/src/org/jetbrains/kotlin/daemon/client/KotlinCompilerClient.kt @@ -94,6 +94,7 @@ object KotlinCompilerClient { leaseSession: Boolean, sessionAliveFlagFile: File? = null ): ServiceWithSession? = connectLoop(reportingTargets) { + setupServerHostName() val (service, newJVMOptions) = tryFindSuitableDaemonOrNewOpts(File(daemonOptions.runFilesPath), compilerId, daemonJVMOptions, { cat, msg -> reportingTargets.report(cat, msg) }) if (service != null) { // the newJVMOptions could be checked here for additional parameters, if needed @@ -370,9 +371,7 @@ object KotlinCompilerClient { val javaExecutable = File(File(System.getProperty("java.home"), "bin"), "java") val platformSpecificOptions = listOf( // hide daemon window - "-Djava.awt.headless=true", - // prevent host name resolution - "-Djava.rmi.server.hostname=${LoopbackNetworkInterface.loopbackInetAddressName}") + "-Djava.awt.headless=true") val args = listOf( javaExecutable.absolutePath, "-cp", compilerId.compilerClasspath.joinToString(File.pathSeparator)) + platformSpecificOptions + diff --git a/compiler/daemon/daemon-common/src/org/jetbrains/kotlin/daemon/common/NetworkUtils.kt b/compiler/daemon/daemon-common/src/org/jetbrains/kotlin/daemon/common/NetworkUtils.kt index ab09aeb3066..6a47e509354 100644 --- a/compiler/daemon/daemon-common/src/org/jetbrains/kotlin/daemon/common/NetworkUtils.kt +++ b/compiler/daemon/daemon-common/src/org/jetbrains/kotlin/daemon/common/NetworkUtils.kt @@ -96,3 +96,10 @@ fun findPortAndCreateRegistry(attempts: Int, portRangeStart: Int, portRangeEnd: throw IllegalStateException("Cannot find free port in $attempts attempts", lastException) } +/** + * Needs to be set up on both client and server to prevent localhost resolution, + * which may be slow and can cause a timeout when there is a network problem/misconfiguration. + */ +fun setupServerHostName() { + System.setProperty("java.rmi.server.hostname", LoopbackNetworkInterface.loopbackInetAddressName) +} diff --git a/compiler/daemon/src/org/jetbrains/kotlin/daemon/KotlinCompileDaemon.kt b/compiler/daemon/src/org/jetbrains/kotlin/daemon/KotlinCompileDaemon.kt index 1be59f76c10..85d877683f0 100644 --- a/compiler/daemon/src/org/jetbrains/kotlin/daemon/KotlinCompileDaemon.kt +++ b/compiler/daemon/src/org/jetbrains/kotlin/daemon/KotlinCompileDaemon.kt @@ -89,6 +89,7 @@ object KotlinCompileDaemon { @JvmStatic fun main(args: Array) { + setupServerHostName() log.info("Kotlin compiler daemon version " + (loadVersionFromResource() ?: "")) log.info("daemon JVM args: " + ManagementFactory.getRuntimeMXBean().inputArguments.joinToString(" "))