Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Cancel coroutine after receiving goodbye #6

Merged
merged 2 commits into from
Oct 28, 2024
Merged
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
6 changes: 4 additions & 2 deletions src/main/kotlin/io/xconn/xconn/Session.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.TimeoutCancellationException
import kotlinx.coroutines.cancel
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlinx.coroutines.withTimeout
import kotlin.coroutines.cancellation.CancellationException
Expand All @@ -49,7 +50,7 @@ class Session(private val baseSession: BaseSession) {

init {
coroutineScope.launch {
while (true) {
while (isActive) {
try {
val message = baseSession.receive()
processIncomingMessage(wampSession.receive(message))
Expand All @@ -71,15 +72,16 @@ class Session(private val baseSession: BaseSession) {
val goodbyeMsg = Goodbye(mapOf(), closeCloseRealm)
val data = wampSession.sendMessage(goodbyeMsg)
baseSession.send(data)
coroutineScope.cancel()

try {
withTimeout(10_000L) {
goodbyeRequest.await()
}
} catch (e: TimeoutCancellationException) {
coroutineScope.cancel()
baseSession.close()
} finally {
coroutineScope.cancel()
baseSession.close()
}
}
Expand Down