Skip to content

Commit e321394

Browse files
cbeylselizarov
authored andcommitted
Prevent temporary leaks in HandlerDispatcher by properly handling
cancellations in scheduleResumeAfterDelay() implementation
1 parent 39107ee commit e321394

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

ui/kotlinx-coroutines-android/src/HandlerDispatcher.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,11 @@ internal class HandlerContext private constructor(
120120
}
121121

122122
override fun scheduleResumeAfterDelay(timeMillis: Long, continuation: CancellableContinuation<Unit>) {
123-
handler.postDelayed({
123+
val block = Runnable {
124124
with(continuation) { resumeUndispatched(Unit) }
125-
}, timeMillis.coerceAtMost(MAX_DELAY))
125+
}
126+
handler.postDelayed(block, timeMillis.coerceAtMost(MAX_DELAY))
127+
continuation.invokeOnCancellation { handler.removeCallbacks(block) }
126128
}
127129

128130
override fun invokeOnTimeout(timeMillis: Long, block: Runnable): DisposableHandle {

0 commit comments

Comments
 (0)