Skip to content

Commit 5f94021

Browse files
committed
Session: save querier and publisher as weak refs
1 parent a77d60d commit 5f94021

1 file changed

Lines changed: 21 additions & 9 deletions

File tree

  • zenoh-java/src/commonMain/kotlin/io/zenoh

zenoh-java/src/commonMain/kotlin/io/zenoh/Session.kt

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import io.zenoh.query.Queryable
3232
import io.zenoh.sample.Sample
3333
import io.zenoh.session.SessionDeclaration
3434
import io.zenoh.session.SessionInfo
35+
import java.lang.ref.WeakReference
3536
import java.util.*
3637
import java.util.concurrent.BlockingQueue
3738
import java.util.concurrent.LinkedBlockingDeque
@@ -54,7 +55,11 @@ class Session private constructor(private val config: Config) : AutoCloseable {
5455

5556
internal var jniSession: JNISession? = null
5657

57-
private var declarations = mutableListOf<SessionDeclaration>()
58+
// Subscribers and Queryables that keep running despite losing references to them.
59+
private var strongDeclarations = mutableListOf<SessionDeclaration>()
60+
61+
// Publishers and queriers that shouldn't be kept alive when losing all references to them.
62+
private var weakDeclarations = mutableListOf<WeakReference<SessionDeclaration>>()
5863

5964
companion object {
6065

@@ -83,11 +88,16 @@ class Session private constructor(private val config: Config) : AutoCloseable {
8388
* However, any session declaration that was still alive and bound to the session previous to closing it, will still be alive.
8489
*/
8590
override fun close() {
86-
declarations.removeIf {
91+
strongDeclarations.removeIf {
8792
it.undeclare()
8893
true
8994
}
9095

96+
weakDeclarations.removeIf {
97+
it.get()?.undeclare()
98+
true
99+
}
100+
91101
jniSession?.close()
92102
jniSession = null
93103
}
@@ -375,7 +385,7 @@ class Session private constructor(private val config: Config) : AutoCloseable {
375385
fun declareKeyExpr(keyExpr: String): KeyExpr {
376386
return jniSession?.run {
377387
val keyexpr = declareKeyExpr(keyExpr)
378-
declarations.add(keyexpr)
388+
strongDeclarations.add(keyexpr)
379389
keyexpr
380390
} ?: throw sessionClosedException
381391
}
@@ -568,7 +578,7 @@ class Session private constructor(private val config: Config) : AutoCloseable {
568578
internal fun resolvePublisher(keyExpr: KeyExpr, options: PublisherOptions): Publisher {
569579
return jniSession?.run {
570580
val publisher = declarePublisher(keyExpr, options)
571-
declarations.add(publisher)
581+
weakDeclarations.add(WeakReference(publisher))
572582
publisher
573583
} ?: throw (sessionClosedException)
574584
}
@@ -579,7 +589,7 @@ class Session private constructor(private val config: Config) : AutoCloseable {
579589
): HandlerSubscriber<R> {
580590
return jniSession?.run {
581591
val subscriber = declareSubscriberWithHandler(keyExpr, handler)
582-
declarations.add(subscriber)
592+
strongDeclarations.add(subscriber)
583593
subscriber
584594
} ?: throw (sessionClosedException)
585595
}
@@ -590,7 +600,7 @@ class Session private constructor(private val config: Config) : AutoCloseable {
590600
): CallbackSubscriber {
591601
return jniSession?.run {
592602
val subscriber = declareSubscriberWithCallback(keyExpr, callback)
593-
declarations.add(subscriber)
603+
strongDeclarations.add(subscriber)
594604
subscriber
595605
} ?: throw (sessionClosedException)
596606
}
@@ -601,7 +611,7 @@ class Session private constructor(private val config: Config) : AutoCloseable {
601611
): HandlerQueryable<R> {
602612
return jniSession?.run {
603613
val queryable = declareQueryableWithHandler(keyExpr, handler, options)
604-
declarations.add(queryable)
614+
strongDeclarations.add(queryable)
605615
queryable
606616
} ?: throw (sessionClosedException)
607617
}
@@ -612,7 +622,7 @@ class Session private constructor(private val config: Config) : AutoCloseable {
612622
): CallbackQueryable {
613623
return jniSession?.run {
614624
val queryable = declareQueryableWithCallback(keyExpr, callback, options)
615-
declarations.add(queryable)
625+
strongDeclarations.add(queryable)
616626
queryable
617627
} ?: throw (sessionClosedException)
618628
}
@@ -623,7 +633,9 @@ class Session private constructor(private val config: Config) : AutoCloseable {
623633
options: QuerierOptions
624634
): Querier {
625635
return jniSession?.run {
626-
declareQuerier(keyExpr, options)
636+
val querier = declareQuerier(keyExpr, options)
637+
weakDeclarations.add(WeakReference(querier))
638+
querier
627639
} ?: throw sessionClosedException
628640
}
629641

0 commit comments

Comments
 (0)