Skip to content

Commit 94ee78c

Browse files
rpaquaygoodwinnk
authored andcommitted
Hitting breakpoint in Kotlin sometimes doesn't work (KT-22205)
Fix KT-22205 and https://issuetracker.google.com/issues/71556313 Hitting breakpoints in Kotlin JUnit test with an Android Gradle project sometimes(*) does not work in Android Studio 3.1 beta. (*) The issue is related to various threads running concurrently with "dumb" mode background code, so the issue reproduces only on certain configurations. In a nutshell, the issue is as follows * On one hand, gradle build finishes, fires an event ("buildFinished") that is processed "later" and that causes the IDE to enter "dumb" mode for a shot amount of time (about 300-500 msec on a somewhat up to date multi-core computer). * On the other hand, once the JVM of the debuggee is started, breakpoints need to be resolved (on the debugger thread, through a call to com.intellij.debugger.engine.CompoundPositionManager.createPrepareRequests. This code calls into the "KotlinPositionManager.createPrepareRequests", which in turns calls into "PerFileAnalysisCache.analyze". That method returns "AnalysisResult.EMPTY" is the project is in dumb mode. This return value prevents callers from successfully resolving the source location into a breakpoint. Given that the 2 code paths above execute on separate threads without explicit synchronization, the "failed to resolve breakpoint" issue occurs sporadically, to the point it happens 100% of the time on certain configuration. The fix is so wrap the kotlin breakpoint resolution code inside a "runReadActionInSmartMode" so that the debugger thread "waits" for "dumb" mode to terminates before trying to resolve breakpoint locations. #KT-22205 Fixed
1 parent b1bcadd commit 94ee78c

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

idea/idea-jvm/src/org/jetbrains/kotlin/idea/debugger/KotlinPositionManager.kt

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ import com.intellij.debugger.engine.evaluation.EvaluationContext
2626
import com.intellij.debugger.impl.DebuggerUtilsEx
2727
import com.intellij.debugger.jdi.StackFrameProxyImpl
2828
import com.intellij.debugger.requests.ClassPrepareRequestor
29+
import com.intellij.openapi.project.DumbService
2930
import com.intellij.openapi.roots.ProjectRootManager
31+
import com.intellij.openapi.util.Computable
3032
import com.intellij.openapi.vfs.VirtualFile
3133
import com.intellij.openapi.vfs.VirtualFileManager
3234
import com.intellij.psi.PsiFile
@@ -342,13 +344,15 @@ class KotlinPositionManager(private val myDebugProcess: DebugProcess) : MultiReq
342344
throw NoDataException.INSTANCE
343345
}
344346

345-
val classNames = DebuggerClassNameProvider(myDebugProcess).getOuterClassNamesForPosition(position)
346-
return classNames.flatMap { name ->
347-
listOfNotNull(
348-
myDebugProcess.requestsManager.createClassPrepareRequest(requestor, name),
349-
myDebugProcess.requestsManager.createClassPrepareRequest(requestor, "$name$*")
350-
)
351-
}
347+
return DumbService.getInstance(myDebugProcess.project).runReadActionInSmartMode(Computable {
348+
val classNames = DebuggerClassNameProvider(myDebugProcess).getOuterClassNamesForPosition(position)
349+
classNames.flatMap { name ->
350+
listOfNotNull(
351+
myDebugProcess.requestsManager.createClassPrepareRequest(requestor, name),
352+
myDebugProcess.requestsManager.createClassPrepareRequest(requestor, "$name$*")
353+
)
354+
}
355+
})
352356
}
353357

354358
private fun ReferenceType.containsKotlinStrata() = availableStrata().contains(KOTLIN_STRATA_NAME)

0 commit comments

Comments
 (0)