Skip to content

Commit

Permalink
Make cancelation robust to unpublished callback
Browse files Browse the repository at this point in the history
  • Loading branch information
armanbilge committed Sep 17, 2023
1 parent e0c644c commit fb465db
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions core/jvm/src/main/scala/cats/effect/unsafe/TimerHeap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ private final class TimerHeap extends AtomicBoolean { needsPack =>
/**
* Cancel this timer.
*/
def apply(): Unit = if (callback ne null) { // if we're not already deleted
def apply(): Unit = {
// we can always clear the callback, without explicitly publishing
callback = null

Expand All @@ -363,9 +363,12 @@ private final class TimerHeap extends AtomicBoolean { needsPack =>
if (thread.isInstanceOf[WorkerThread]) {
val worker = thread.asInstanceOf[WorkerThread]
val heap = TimerHeap.this
if (worker.ownsTimers(heap))
heap.removeAt(index)
else // otherwise this heap will need packing
if (worker.ownsTimers(heap)) {
if (index >= 0) { // remove ourselves at most once
heap.removeAt(index)
index = -1 // prevent further removals
}
} else // otherwise this heap will need packing
needsPack.set(true)
} else needsPack.set(true)
}
Expand Down

0 comments on commit fb465db

Please sign in to comment.