fix(build-service,tooling-api): Gradle Daemon and ToolingAPI server is not stopped properly

The Tooling API server (which is executed in a separate process) was not being stopped properly after IToolingApiServer.shutdown() was called. As a result, the Gradle daemon started by the tooling API was not stopped as well.

This commit fixes this issue by properly implementing the shutdown() call in Tooling API. Now, when the user closes a project and the GradleBuildService is destroyed, the Tooling API server stops the Gradle daemon(s) and then shutdowns itself.
This commit is contained in:
Akash Yadav
2023-10-03 22:36:20 +05:30
parent 023bbab9e4
commit 6899e1f8fc
7 changed files with 164 additions and 47 deletions
@@ -21,6 +21,7 @@ import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import java.io.InterruptedIOException
/**
* Calls [CoroutineScope.cancel] only if a job is active in the scope.
@@ -39,7 +40,22 @@ fun CoroutineScope.cancelIfActive(message: String, cause: Throwable? = null) =
*/
fun CoroutineScope.cancelIfActive(exception: CancellationException? = null) {
val job = coroutineContext[Job]
if (job?.isActive == true) {
cancel(exception)
job?.cancel(exception)
}
/**
* Runs the given [action] and re-throws the [Throwable] if this [Throwable] is thrown for
* cancellation or interruption purposes.
*
* @param action The action to run if this [Throwable] was cancelled or interrupted.
*/
inline fun Throwable.ifCancelledOrInterrupted(suppress: Boolean = false, action: () -> Unit) {
when (this) {
is CancellationException,
is InterruptedException,
is InterruptedIOException -> {
action()
if (!suppress) { throw this }
}
}
}