-
Notifications
You must be signed in to change notification settings - Fork 439
improvement: Recover from an unresponsive Bloop server #8529
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
Open
jozanek
wants to merge
3
commits into
scalameta:main
Choose a base branch
from
jozanek:improvement/bloop-recover-wedged-server
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,12 +5,13 @@ import java.io.File | |
| import java.io.IOException | ||
| import java.io.OutputStream | ||
| import java.lang.management.ManagementFactory | ||
| import java.net.ConnectException | ||
| import java.net.Socket | ||
| import java.nio.file.Files | ||
| import java.nio.file.Paths | ||
| import java.nio.file.attribute.PosixFilePermissions | ||
| import java.util.concurrent.ScheduledExecutorService | ||
| import java.util.concurrent.TimeUnit | ||
| import java.util.concurrent.atomic.AtomicBoolean | ||
| import java.util.concurrent.atomic.AtomicInteger | ||
|
|
||
| import scala.annotation.tailrec | ||
|
|
@@ -86,12 +87,111 @@ final class BloopServers( | |
| result | ||
| } | ||
|
|
||
| /** | ||
| * Stop a Bloop server that reported itself as running but never answered, so | ||
| * the next connection attempt can cold-start a fresh one. | ||
| * | ||
| * No-op unless this connection reused a pre-existing server: a server we just | ||
| * started that fails to come up is a startup problem, not a wedge, and exiting | ||
| * it would only loop. `BloopRifle.exit` sends a synchronous `ng-stop` over the | ||
| * same (possibly stuck) socket, so we run it off the calling thread and bound | ||
| * it with a timeout. The next `connect` only cold-starts a fresh server if the | ||
| * old one is actually gone (`BloopRifle.check` is socket-based), so we wait for | ||
| * it to stop and, if it can't be stopped, fail with an actionable message | ||
| * rather than silently reconnecting to the same wedged process. | ||
| */ | ||
| private def recoverFromWedgedServer( | ||
| connectedToPreexistingServer: AtomicBoolean | ||
| ): Future[Unit] = | ||
| if (connectedToPreexistingServer.get()) { | ||
| val config = bloopConfig(userConfig = None, projectRoot = None) | ||
| scribe.warn( | ||
| "Bloop server was reported as running but didn't respond; " + | ||
| "stopping it so a fresh one can be started." | ||
| ) | ||
| // `ng-stop` is a synchronous call over the same (possibly stuck) socket, | ||
| // so run it on a dedicated daemon thread: a truly hung server then leaks | ||
| // only this isolated thread instead of occupying an execution-context one | ||
| // after recovery has moved on. | ||
| val exit = new Thread("bloop-exit-on-recovery") { | ||
| override def run(): Unit = | ||
| try { | ||
| BloopRifle.exit(config, bloopWorkingDir.toNIO, bloopLogger) | ||
| () | ||
| } catch { | ||
| case NonFatal(e) => | ||
| scribe.warn("Couldn't cleanly stop the Bloop server.", e) | ||
| } | ||
| } | ||
| exit.setDaemon(true) | ||
| exit.start() | ||
| // Wait — without blocking a thread — for the server to actually go down. | ||
| // `check` is socket-based, so the retry only cold-starts a fresh server | ||
| // once the old one is really gone; otherwise fail with actionable guidance. | ||
| awaitBloopStopped( | ||
| config, | ||
| System.currentTimeMillis() + RecoveryTimeoutMs, | ||
| ).map { | ||
| case true => () | ||
| case false => | ||
| // Show the actionable guidance directly: the reconnect path doesn't go | ||
| // through `ConnectionProvider`, so this is the only message there. Throw | ||
| // a marker so the initial-connect path doesn't also stack its generic | ||
| // "failed to connect" message on top of this one. | ||
| languageClient.showMessage(Messages.UnresponsiveBloopServer.params()) | ||
| throw new AlreadyReportedConnectException( | ||
| Messages.UnresponsiveBloopServer.message | ||
| ) | ||
| } | ||
| } else Future.unit | ||
|
|
||
| /** | ||
| * Poll `BloopRifle.check` until Bloop is down or `deadline` (epoch ms) passes, | ||
| * scheduling the delays on `sh` rather than blocking a thread. | ||
| */ | ||
| private def awaitBloopStopped( | ||
| config: BloopRifleConfig, | ||
| deadline: Long, | ||
| ): Future[Boolean] = { | ||
| val stopped = Promise[Boolean]() | ||
| def poll(): Unit = | ||
| try { | ||
| if (!BloopRifle.check(config, bloopLogger)) stopped.trySuccess(true) | ||
| else if (System.currentTimeMillis() >= deadline) | ||
| stopped.trySuccess(false) | ||
| else { | ||
| sh.schedule( | ||
| new Runnable { def run(): Unit = poll() }, | ||
| RecoveryPollIntervalMs, | ||
| TimeUnit.MILLISECONDS, | ||
| ) | ||
| () | ||
| } | ||
| } catch { | ||
| case NonFatal(e) => | ||
| // A scheduled poll runs on `sh`, where a thrown exception would be | ||
| // swallowed and leave `stopped` pending forever, so complete it here. | ||
| scribe.warn( | ||
| "Error while checking whether the Bloop server stopped.", | ||
| e, | ||
| ) | ||
| stopped.trySuccess(false) | ||
| () | ||
| } | ||
| poll() | ||
| stopped.future | ||
| } | ||
|
|
||
| def newServer( | ||
| projectRoot: AbsolutePath, | ||
| bspTraceRoot: AbsolutePath, | ||
| userConfiguration: () => UserConfiguration, | ||
| bspStatusOpt: Option[ConnectionBspStatus], | ||
| ): Future[BuildServerConnection] = { | ||
| // Set by `connect` to whether it reused an already-running Bloop server; | ||
| // read by `recoverFromWedgedServer` to decide whether to force a restart. | ||
| // Local to this connection so concurrent folder connects don't race on it. | ||
| val connectedToPreexistingServer = new AtomicBoolean(false) | ||
| BuildServerConnection | ||
| .fromSockets( | ||
| projectRoot, | ||
|
|
@@ -102,6 +202,7 @@ final class BloopServers( | |
| connect( | ||
| projectRoot, | ||
| userConfiguration(), | ||
| connectedToPreexistingServer, | ||
| ), | ||
| tables.dismissedNotifications.ReconnectBsp, | ||
| tables.dismissedNotifications.RequestTimeout, | ||
|
|
@@ -110,6 +211,8 @@ final class BloopServers( | |
| name, | ||
| bspStatusOpt, | ||
| workDoneProgress = workDoneProgress, | ||
| recoverConnection = | ||
| () => recoverFromWedgedServer(connectedToPreexistingServer), | ||
| ) | ||
| .recover { case NonFatal(e) => | ||
| Try( | ||
|
|
@@ -375,40 +478,46 @@ final class BloopServers( | |
| } | ||
| } | ||
|
|
||
| private def startNewServer( | ||
| config: BloopRifleConfig, | ||
| userConfiguration: UserConfiguration, | ||
| ): Future[Unit] = { | ||
| scribe.info("No running Bloop server found, starting one.") | ||
| val ext = if (Properties.isWin) ".exe" else "" | ||
| val javaCommand = metalsJavaHome match { | ||
| case Some(metalsJavaHome) => | ||
| Paths.get(metalsJavaHome).resolve(s"bin/java$ext").toString | ||
| case None => "java" | ||
| } | ||
| val version = | ||
| userConfiguration.bloopVersion.getOrElse(defaultBloopVersion) | ||
| checkOldBloopRunning().flatMap { _ => | ||
| BloopRifle.startServer( | ||
| config, | ||
| sh, | ||
| bloopLogger, | ||
| version, | ||
| javaCommand, | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| private def connect( | ||
| projectRoot: AbsolutePath, | ||
| userConfiguration: UserConfiguration, | ||
| connectedToPreexistingServer: AtomicBoolean, | ||
| ): Future[SocketConnection] = { | ||
| val config = bloopConfig(Some(userConfiguration), Some(projectRoot)) | ||
|
|
||
| val maybeStartBloop = { | ||
|
|
||
| val running = BloopRifle.check(config, bloopLogger) | ||
|
|
||
| if (running) { | ||
| val maybeStartBloop = | ||
| if (BloopRifle.check(config, bloopLogger)) { | ||
| scribe.info("Found a Bloop server running") | ||
| connectedToPreexistingServer.set(true) | ||
| Future.unit | ||
| } else { | ||
| scribe.info("No running Bloop server found, starting one.") | ||
| val ext = if (Properties.isWin) ".exe" else "" | ||
| val javaCommand = metalsJavaHome match { | ||
| case Some(metalsJavaHome) => | ||
| Paths.get(metalsJavaHome).resolve(s"bin/java$ext").toString | ||
| case None => "java" | ||
| } | ||
| val version = | ||
| userConfiguration.bloopVersion.getOrElse(defaultBloopVersion) | ||
| checkOldBloopRunning().flatMap { _ => | ||
| BloopRifle.startServer( | ||
| config, | ||
| sh, | ||
| bloopLogger, | ||
| version, | ||
| javaCommand, | ||
| ) | ||
| } | ||
| connectedToPreexistingServer.set(false) | ||
| startNewServer(config, userConfiguration) | ||
| } | ||
| } | ||
|
|
||
| def openConnection( | ||
| conn: BspConnection, | ||
|
|
@@ -421,7 +530,12 @@ final class BloopServers( | |
| val maybeSocket = | ||
| try Right(conn.openSocket(period, timeout)) | ||
| catch { | ||
| case e: ConnectException => Left(e) | ||
| // Any failure while waiting for the BSP socket means the connection | ||
| // didn't materialize. bloop-rifle throws a plain RuntimeException | ||
| // via `sys.error` when the socket never opens, so treat every | ||
| // failure like a connect failure and normalize it to the | ||
| // `IOException` thrown below, which the recovery path acts on. | ||
| case NonFatal(e) => Left(e) | ||
| } | ||
| maybeSocket match { | ||
| case Right(socket) => socket | ||
|
|
@@ -469,6 +583,12 @@ final class BloopServers( | |
| object BloopServers { | ||
| val name = "Bloop" | ||
|
|
||
| // How long to wait for a wedged Bloop server to stop before giving up. | ||
| private val RecoveryTimeoutMs = 10000L | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we set it under MetalsServerOptions? And maybe default to 20000L? |
||
|
|
||
| // How often to poll whether the wedged Bloop server has stopped. | ||
| private val RecoveryPollIntervalMs = 100L | ||
|
|
||
| // Needed for creating unique socket files for each bloop connection | ||
| private[BloopServers] val connectionCounter = new AtomicInteger(0) | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder what will happen if we have multiple editors open with Bloop build tool. We might never get to stopped phase, because another metals server will recover the connection and start Bloop in the meantime. We might just want to connect afterwards.
We should for sure test recoverFromWedgedServer separately.