Skip to content

Commit

Permalink
Fix the issue
Browse files Browse the repository at this point in the history
  • Loading branch information
nbirillo committed Sep 12, 2023
1 parent 32857f3 commit b1aaf27
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,25 +113,35 @@ abstract class AbstractUnifier(
}
}

@Suppress("TooGenericExceptionCaught")
private suspend fun isFinishedWithTimeout(
transformation: Transformation,
psiTree: PsiFile,
statsBuilder: TransformationsStatisticsBuilder,
): Boolean {
try {
statsBuilder.forwardApplyMeasuredWithTimeout(transformation, psiTree, TIMEOUT_MS)
} catch (e: TimeoutCancellationException) {
// Skip transformation with timeout in further iterations
skipTransformations.add(transformation)
logger.severe { "Timeout reached for ${transformation.key}: $e" }
return false
} catch (e: Exception) {
when (e) {
// TODO catch error from coroutines, since the initial error MyTimeoutCancellationException will be replaced with another one
is TimeoutCancellationException, is MyTimeoutCancellationException -> {
// Skip transformation with timeout in further iterations
skipTransformations.add(transformation)
logger.severe { "Timeout reached for ${transformation.key}: $e" }
return false
}
}
println("Await reached for ${transformation.key}")
}
return true
}

companion object {
const val MAX_ITERATIONS = 50
const val TIMEOUT_MS: Long = 1
const val TIMEOUT_MS: Long = 100
var skipTransformations = mutableSetOf<Transformation>()
}
}

// TODO: move somewhere
class MyTimeoutCancellationException(message: String): Exception(message)
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@ package org.jetbrains.research.code.submissions.clustering.impl.util.logging

import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.ModalityState
import com.intellij.openapi.progress.util.ProgressIndicatorUtils
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import kotlinx.coroutines.TimeoutCancellationException
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.async
import kotlinx.coroutines.future.await
import kotlinx.coroutines.future.future
import kotlinx.coroutines.withTimeout
import org.jetbrains.research.code.submissions.clustering.impl.unifiers.MyTimeoutCancellationException
import org.jetbrains.research.ml.ast.transformations.Transformation
import java.util.concurrent.Executors
import kotlin.system.measureTimeMillis


class TransformationsStatisticsBuilder {
private val transformationsNumber = mutableMapOf<String, Int>()
private val transformationsExecTime = mutableMapOf<String, Long>()
Expand All @@ -18,18 +25,29 @@ class TransformationsStatisticsBuilder {
val previousTree = ApplicationManager.getApplication().runReadAction<PsiElement> {
psiTree.copy()
}

// println("current thread: ${Thread.currentThread()}")

val executionTime = withTimeout(timeout) {
// val pool = Executors.newFixedThreadPool(1)
println("execution start: ${transformation.key}")
future {
// println("async thread: ${Thread.currentThread()}")
measureTimeMillis {
ApplicationManager.getApplication().invokeAndWait({
ApplicationManager.getApplication().runWriteAction {
transformation.forwardApply(psiTree)
}
}, ModalityState.NON_MODAL)
ApplicationManager.getApplication().invokeAndWait({
// println("invoke thread: ${Thread.currentThread()}")
ProgressIndicatorUtils.withTimeout(timeout) {
transformation.forwardApply(psiTree)
} ?: throw MyTimeoutCancellationException("${transformation.key} with timeout!")
}, ModalityState.NON_MODAL)
}
// println("measureTimeMillis finished")
// ms
}.await()
}

println("Await reached for ${transformation.key}")

var isApplied: Boolean? = null
ApplicationManager.getApplication().invokeAndWait({
isApplied = ApplicationManager.getApplication().runReadAction<Boolean> {
Expand Down

0 comments on commit b1aaf27

Please sign in to comment.