@@ -66,7 +66,7 @@ abstract class Task<Result>(
6666 abstract suspend fun SafeContext.onAction (): Result
6767
6868 /* *
69- * This function is called when the task is cancelled .
69+ * This function is called when the task is canceled .
7070 * It should be overridden for tasks that need to perform cleanup operations,
7171 * such as cancelling a block breaking progress, releasing resources,
7272 * or stopping any ongoing operations that were started by the task.
@@ -107,41 +107,42 @@ abstract class Task<Result>(
107107
108108 if (iteration == repeats) {
109109 onSuccess(result)
110- stopListening ()
110+ tidyUp ()
111111 return TaskResult .Success (result)
112112 }
113113
114114 onRepeat(iteration)
115115 }
116- } catch (e: CancellationException ) {
117- stopListening()
118- LOG .warn(" Coroutine for task $name cancelled" )
119- runSafe {
120- onCancel()
121- }
122- return TaskResult .Cancelled
123116 } catch (e: TimeoutCancellationException ) {
124117 attempt++
125118 LOG .warn(" Task $name timed out after $age ms and $attempt attempts" )
126119 onRetry()
127- stopListening()
120+ tidyUp()
121+ } catch (e: CancellationException ) {
122+ tidyUp()
123+ LOG .warn(" Coroutine for task $name cancelled" )
124+ return TaskResult .Cancelled
128125 } catch (e: Throwable ) {
129126 attempt++
130127 LOG .error(" Task $name failed after $age ms and $attempt attempts" , e)
131128 onException(e)
132- stopListening ()
129+ tidyUp ()
133130 return TaskResult .Failure (e)
134131 }
135132
136- stopListening ()
133+ tidyUp ()
137134 } while (attempt < maxAttempts || iteration <= repeats)
138135
139136 LOG .error(" Task $name fully timed out after $age ms and $attempt attempts" )
140137 onTimeout()
141138 return TaskResult .Timeout (timeout, attempt)
142139 }
143140
144- private fun stopListening () {
141+ private fun tidyUp () {
142+ runSafe {
143+ onCancel()
144+ }
145+
145146 EventFlow .syncListeners.unsubscribe(syncListeners)
146147 EventFlow .concurrentListeners.unsubscribe(concurrentListeners)
147148
0 commit comments