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
  • Loading branch information
jonathan-hepp authored and DariusIMP committed Aug 5, 2024
1 parent e3ad5a3 commit 675d7cf
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 @@ -51,7 +51,7 @@ import java.time.Duration
*/
class Session private constructor(private val config: Config) : AutoCloseable {

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

private var declarations = mutableListOf<SessionDeclaration>()

Expand Down Expand Up @@ -440,7 +440,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 675d7cf

Please sign in to comment.