Skip to content

Commit 4a44fef

Browse files
authored
Fix another potential memory leak in WorkerDispatcher (#3445)
1 parent 9e1eb9e commit 4a44fef

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

kotlinx-coroutines-core/native/src/MultithreadedDispatchers.kt

+7-3
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,16 @@ internal class WorkerDispatcher(name: String) : CloseableCoroutineDispatcher(),
2626
}
2727

2828
override fun scheduleResumeAfterDelay(timeMillis: Long, continuation: CancellableContinuation<Unit>) {
29-
worker.executeAfter(timeMillis.toMicrosSafe()) {
29+
val handle = schedule(timeMillis, Runnable {
3030
with(continuation) { resumeUndispatched(Unit) }
31-
}
31+
})
32+
continuation.disposeOnCancellation(handle)
3233
}
3334

34-
override fun invokeOnTimeout(timeMillis: Long, block: Runnable, context: CoroutineContext): DisposableHandle {
35+
override fun invokeOnTimeout(timeMillis: Long, block: Runnable, context: CoroutineContext): DisposableHandle =
36+
schedule(timeMillis, block)
37+
38+
private fun schedule(timeMillis: Long, block: Runnable): DisposableHandle {
3539
// Workers don't have an API to cancel sent "executeAfter" block, but we are trying
3640
// to control the damage and reduce reachable objects by nulling out `block`
3741
// that may retain a lot of references, and leaving only an empty shell after a timely disposal

0 commit comments

Comments
 (0)