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

Kryo race fix #1968

Merged
merged 1 commit into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ apacheCommonsExecVersion=1.2
apacheCommonsTextVersion=1.9
rgxgenVersion=1.3
antlrVersion=4.9.2
kryoVersion=5.3.0
kryoVersion=5.4.0
kryoSerializersVersion=0.45
asmVersion=9.2
testNgVersion=7.6.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ private fun EngineProcessModel.setup(kryoHelper: KryoHelper, watchdog: IdleWatch
}
watchdog.measureTimeForActiveCall(findMethodParamNames, "Find method parameters names") { params ->
val classId = kryoHelper.readObject<ClassId>(params.classId)
val byMethodDescription = kryoHelper.readObject<Map<MethodDescription, List<String>>>(params.bySignature)
val bySignatureRaw = kryoHelper.readObject<List<Pair<MethodDescription, List<String>>>>(params.bySignature)
val byMethodDescription = bySignatureRaw.associate { it.first to it.second }
FindMethodParamNamesResult(kryoHelper.writeObject(classId.jClass.allNestedClasses.flatMap { clazz -> clazz.id.allMethods.mapNotNull { it.method.kotlinFunction } }
.mapNotNull { method -> byMethodDescription[method.methodDescription()]?.let { params -> method.executableId to params } }
.toMap()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import com.esotericsoftware.kryo.kryo5.util.DefaultInstantiatorStrategy
import com.jetbrains.rd.util.lifetime.Lifetime
import com.jetbrains.rd.util.lifetime.throwIfNotAlive
import java.io.ByteArrayOutputStream
import java.util.concurrent.locks.ReentrantLock
import kotlin.concurrent.withLock

/**
* Helpful class for working with the kryo.
Expand All @@ -24,36 +26,17 @@ class KryoHelper constructor(
private val kryoInput= Input()
private val sendKryo: Kryo = TunedKryo()
private val receiveKryo: Kryo = TunedKryo()
private val myLockObject = ReentrantLock()

init {
sendKryo.setAutoReset(true)
receiveKryo.setAutoReset(true)
lifetime.onTermination {
kryoInput.close()
kryoOutput.close()
}
}

fun <T> register(clazz: Class<T>, serializer: Serializer<T>) {
sendKryo.register(clazz, serializer)
receiveKryo.register(clazz, serializer)
}

private fun <T> addInstantiatorOnKryo(kryo: Kryo, clazz: Class<T>, factory: () -> T) {
val instantiator = kryo.instantiatorStrategy
kryo.instantiatorStrategy = object : InstantiatorStrategy {
override fun <R : Any?> newInstantiatorOf(type: Class<R>): ObjectInstantiator<R> {
return if (type === clazz) {
ObjectInstantiator<R> { factory() as R }
}
else
instantiator.newInstantiatorOf(type)
}
}
}
fun <T> addInstantiator(clazz: Class<T>, factory: () -> T) {
addInstantiatorOnKryo(sendKryo, clazz, factory)
addInstantiatorOnKryo(receiveKryo, clazz, factory)
}

fun setKryoClassLoader(classLoader: ClassLoader) {
sendKryo.classLoader = classLoader
receiveKryo.classLoader = classLoader
Expand All @@ -64,7 +47,7 @@ class KryoHelper constructor(
*
* @throws WritingToKryoException wraps all exceptions
*/
fun <T> writeObject(obj: T): ByteArray {
fun <T> writeObject(obj: T): ByteArray = myLockObject.withLock {
lifetime.throwIfNotAlive()
try {
sendKryo.writeClassAndObject(kryoOutput, obj)
Expand All @@ -84,14 +67,17 @@ class KryoHelper constructor(
*
* @throws ReadingFromKryoException wraps all exceptions
*/
fun <T> readObject(byteArray: ByteArray): T {
fun <T> readObject(byteArray: ByteArray): T = myLockObject.withLock {
lifetime.throwIfNotAlive()
return try {
kryoInput.buffer = byteArray
receiveKryo.readClassAndObject(kryoInput) as T
} catch (e: Exception) {
throw ReadingFromKryoException(e)
}
finally {
receiveKryo.reset()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ object CodeGenerationController {
)
}
}

private fun createUtilityClassIfNeeded(
utilClassListener: UtilClassListener,
model: GenerateTestsModel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ class EngineProcess private constructor(val project: Project, private val classN

val bySignature = executeWithTimeoutSuspended {
DumbService.getInstance(project).runReadActionInSmartMode(Computable {
methods.associate { it.methodDescription() to it.paramNames() }
methods.map { it.methodDescription() to it.paramNames() }
})
}
val arguments = FindMethodParamNamesArguments(
Expand Down
2 changes: 2 additions & 0 deletions utbot-maven/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ task generatePluginDescriptor(type: JavaExec, dependsOn: [compileKotlin, generat
}
}

classes.dependsOn(generatePluginDescriptor)

publishing {
publications {
pluginMaven(MavenPublication) {
Expand Down
1 change: 1 addition & 0 deletions utbot-sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ dependencies {
implementation group: 'org.jetbrains', name: 'annotations', version: '16.0.2'
implementation group: 'com.github.stephenc.findbugs', name: 'findbugs-annotations', version: '1.3.9-1'
implementation 'org.projectlombok:lombok:1.18.20'
testImplementation 'org.mockito:mockito-core:4.2.0'
annotationProcessor 'org.projectlombok:lombok:1.18.20'
implementation(project(":utbot-api"))
implementation group: 'com.google.code.findbugs', name: 'jsr305', version: '3.0.2'
Expand Down