Skip to content

Commit

Permalink
Allowing session declarations to stay alive for the lifespan of a ses…
Browse files Browse the repository at this point in the history
…sion. (eclipse-zenoh#96)

* feat(background declarations): Allowing session declarations to stay alive for the lifespan of a session.

Zenoh declarations ran for as long as the Kotlin variable representing them was kept alive. This
meant that whenever the user lost track of the variable, it got garbage collected and undeclared
in the process. This behavior seems to be counterintuitive for programmers used to garbage collected
languages (see eclipse-zenoh#43).

Therefore in this PR we provide the following change: we keep track of session declarations in a list
inside the session, allowing users to keep running them despite losing their references.
When the session is finalized, the associated declarations are undeclared.
In case the user needs to close a declaration earlier, they need to keep the variable in order
to undeclare it.

* feat(background declarations): closing all session declarations immediately after doing `session.close()`
  • Loading branch information
DariusIMP committed Aug 5, 2024
1 parent 9a34bdb commit 68dbdf5
Showing 1 changed file with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import io.zenoh.prelude.CongestionControl
import io.zenoh.value.Value

/**
* # Publisher
*
* A Zenoh Publisher.
*
* A publisher is automatically dropped when using it with the 'try-with-resources' statement (i.e. 'use' in Kotlin).
Expand Down Expand Up @@ -57,6 +59,12 @@ import io.zenoh.value.Value
*
* The publisher configuration parameters can be later changed using the setter functions.
*
* ## Lifespan
*
* Internally, the [Session] from which the [Publisher] was declared keeps a reference to it, therefore keeping it alive
* until the session is closed. For the cases where we want to stop the publisher earlier, it's necessary
* to keep a reference to it in order to undeclare it later.
*
* @property keyExpr The key expression the publisher will be associated to.
* @property jniPublisher Delegate class handling the communication with the native code.
* @property congestionControl The congestion control policy.
Expand Down Expand Up @@ -119,7 +127,7 @@ class Publisher internal constructor(
jniPublisher?.setPriority(priority)?.onSuccess { this.priority = priority }
}

override fun isValid(): Boolean {
fun isValid(): Boolean {
return jniPublisher != null
}

Expand All @@ -132,10 +140,6 @@ class Publisher internal constructor(
jniPublisher = null
}

protected fun finalize() {
jniPublisher?.close()
}

class Put internal constructor(
private var jniPublisher: JNIPublisher?,
val value: Value,
Expand Down

0 comments on commit 68dbdf5

Please sign in to comment.