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

Add coarse grained lock around BSP requests to avoid deadlocks #3243

Merged
merged 1 commit into from
Jul 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion bsp/worker/src/mill/bsp/worker/MillBuildServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,8 @@ private class MillBuildServer(
}
}

val requestLock = new java.util.concurrent.locks.ReentrantLock()

/**
* Given a function that take input of type T and return output of type V,
* apply the function on the given inputs and return a completable future of
Expand All @@ -745,13 +747,13 @@ private class MillBuildServer(
checkInitialized: Boolean = true
)(f: State => V): CompletableFuture[V] = {
debug(s"Entered ${hint}")

val start = System.currentTimeMillis()
val prefix = hint.split(" ").head
def took =
debug(s"${prefix} took ${System.currentTimeMillis() - start} msec")

val future = new CompletableFuture[V]()

if (checkInitialized && !initialized) {
future.completeExceptionally(
new Exception(
Expand All @@ -762,6 +764,7 @@ private class MillBuildServer(
statePromise.future.onComplete {
case Success(state) =>
try {
requestLock.lock()
val v = f(state)
took
debug(s"${prefix} result: ${v}")
Expand All @@ -772,6 +775,8 @@ private class MillBuildServer(
logStream.println(s"${prefix} caught exception: ${e}")
e.printStackTrace(logStream)
future.completeExceptionally(e)
} finally{
requestLock.unlock()
}
case Failure(exception) =>
future.completeExceptionally(exception)
Expand Down
Loading