Skip to content

Commit

Permalink
Merge pull request #6 from muzzammilshahid/dont-cancel-coroutine
Browse files Browse the repository at this point in the history
 Cancel coroutine after receiving goodbye
  • Loading branch information
muzzammilshahid authored Oct 28, 2024
2 parents 8c87a23 + 7b61a19 commit 89f5d97
Showing 1 changed file with 4 additions and 2 deletions.
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

0 comments on commit 89f5d97

Please # to comment.