Make daemon connection more reliable by retrying on certain exceptions

(cherry picked from commit 74a0711)
This commit is contained in:
Ilya Chernikov
2017-02-17 11:52:06 +03:00
parent c7a2422314
commit 4202bde550
@@ -28,6 +28,9 @@ import org.jetbrains.kotlin.utils.addToStdlib.check
import java.io.File import java.io.File
import java.io.OutputStream import java.io.OutputStream
import java.io.PrintStream import java.io.PrintStream
import java.net.SocketException
import java.rmi.ConnectException
import java.rmi.UnmarshalException
import java.rmi.server.UnicastRemoteObject import java.rmi.server.UnicastRemoteObject
import java.util.concurrent.Semaphore import java.util.concurrent.Semaphore
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
@@ -73,8 +76,18 @@ object KotlinCompilerClient {
): CompileService? { ): CompileService? {
var attempts = 0 var attempts = 0
fun retryOrRethrow(e: Exception) {
if (attempts < DAEMON_CONNECT_CYCLE_ATTEMPTS) {
reportingTargets.report(DaemonReportCategory.INFO, "retrying($attempts) on:" + e.toString())
}
else throw e
}
try { try {
while (attempts++ < DAEMON_CONNECT_CYCLE_ATTEMPTS) { while (attempts < DAEMON_CONNECT_CYCLE_ATTEMPTS) {
try {
attempts += 1
val (service, newJVMOptions) = tryFindSuitableDaemonOrNewOpts(File(daemonOptions.runFilesPath), compilerId, daemonJVMOptions, { cat, msg -> reportingTargets.report(cat, msg) }) val (service, newJVMOptions) = tryFindSuitableDaemonOrNewOpts(File(daemonOptions.runFilesPath), compilerId, daemonJVMOptions, { cat, msg -> reportingTargets.report(cat, msg) })
if (service != null) { if (service != null) {
// the newJVMOptions could be checked here for additional parameters, if needed // the newJVMOptions could be checked here for additional parameters, if needed
@@ -88,6 +101,16 @@ object KotlinCompilerClient {
reportingTargets.report(DaemonReportCategory.DEBUG, "new daemon started, trying to find it") reportingTargets.report(DaemonReportCategory.DEBUG, "new daemon started, trying to find it")
} }
} }
catch (e: SocketException) {
retryOrRethrow(e)
}
catch (e: ConnectException) {
retryOrRethrow(e)
}
catch (e: UnmarshalException) {
retryOrRethrow(e)
}
}
} }
catch (e: Throwable) { catch (e: Throwable) {
reportingTargets.report(DaemonReportCategory.EXCEPTION, e.toString()) reportingTargets.report(DaemonReportCategory.EXCEPTION, e.toString())