Skip to content

Commit

Permalink
Fix JVM crash when finalizing a session that wasn't properly initiali…
Browse files Browse the repository at this point in the history
…zed (#139) (#140)
  • Loading branch information
jonathan-hepp authored Aug 1, 2024
1 parent a306987 commit 2690590
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
7 changes: 5 additions & 2 deletions zenoh-kotlin/src/commonMain/kotlin/io/zenoh/Session.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import java.time.Duration
*/
class Session private constructor(private val config: Config) : AutoCloseable {

private var jniSession: JNISession? = JNISession()
private var jniSession: JNISession? = null

companion object {

Expand Down Expand Up @@ -439,7 +439,10 @@ class Session private constructor(private val config: Config) : AutoCloseable {

/** Launches the session through the jni session, returning the [Session] on success. */
private fun launch(): Result<Session> = runCatching {
return jniSession!!.open(config).map { this@Session }
jniSession = JNISession()
return jniSession!!.open(config)
.map { this@Session }
.onFailure { jniSession = null }
}
}

7 changes: 7 additions & 0 deletions zenoh-kotlin/src/commonTest/kotlin/io/zenoh/SessionTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import io.zenoh.keyexpr.KeyExpr
import io.zenoh.keyexpr.intoKeyExpr
import io.zenoh.sample.Sample
import kotlinx.coroutines.runBlocking
import java.nio.file.Path
import kotlin.test.*

class SessionTest {
Expand All @@ -42,6 +43,12 @@ class SessionTest {
assertFalse(session.isOpen())
}

@Test
fun sessionOpeningFailure() {
val invalidConfig = Config.from(Path.of("invalid"))
assertFailsWith<SessionException> { Session.open(invalidConfig).getOrThrow() }
}

@Test
fun sessionClose_succeedsDespiteNotFreeingAllDeclarations() {
val session = Session.open().getOrThrow()
Expand Down

0 comments on commit 2690590

Please sign in to comment.