Skip to content

Commit

Permalink
Appending current coroutineContext to make sure running/pending jobs …
Browse files Browse the repository at this point in the history
…cancelled

when atomicCoroutineScope.cancel is called
  • Loading branch information
sacOO7 committed Nov 9, 2024
1 parent d0d2c9c commit 1a3a37b
Showing 1 changed file with 5 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package com.ably.chat

import java.util.concurrent.PriorityBlockingQueue
import kotlin.coroutines.cancellation.CancellationException
import kotlin.coroutines.coroutineContext
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
import kotlinx.coroutines.cancelChildren
import kotlinx.coroutines.launch

Expand Down Expand Up @@ -66,7 +68,8 @@ class AtomicCoroutineScope(private val scope: CoroutineScope = CoroutineScope(Di

private suspend fun <T : Any> safeExecute(job: Job<T>) {
try {
scope.launch {
// Appends coroutineContext to cancel current/pending jobs when AtomicCoroutineScope is cancelled
scope.launch(coroutineContext) {
try {
val result = job.coroutineBlock(this)
job.deferredResult.complete(result)
Expand All @@ -81,6 +84,7 @@ class AtomicCoroutineScope(private val scope: CoroutineScope = CoroutineScope(Di

/**
* Cancels ongoing and pending operations with given error.
* See [Coroutine cancellation](https://kt.academy/article/cc-cancellation#cancellation-in-a-coroutine-scope) for more information.
*/
@Synchronized
fun cancel(message: String?, cause: Throwable? = null) {
Expand Down

0 comments on commit 1a3a37b

Please sign in to comment.