Skip to content

Commit 624af32

Browse files
authored
Merge pull request #4449 from durban/fixSpinlock
Fix the spinlock from #4377 and probably also fix #4448
2 parents 1a4eaba + b4798eb commit 624af32

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

core/jvm/src/main/scala/cats/effect/unsafe/WorkerThread.scala

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,7 @@ private[effect] final class WorkerThread[P <: AnyRef](
636636
acc
637637
}
638638

639+
@tailrec
639640
def notifyDoneSleeping(): Unit = {
640641
val st = parked.get()
641642

@@ -645,9 +646,15 @@ private[effect] final class WorkerThread[P <: AnyRef](
645646
// this happens when we wake ourselves at the same moment the pool decides to wake us
646647

647648
while (parked.get() eq ParkedSignal.Interrupting) {}
648-
} else if (parked.compareAndSet(st, ParkedSignal.Unparked)) {
649-
// we won the race to awaken ourselves, so we need to let the pool know
650-
pool.doneSleeping()
649+
} else {
650+
if (parked.compareAndSet(st, ParkedSignal.Unparked)) {
651+
// we won the race to awaken ourselves, so we need to let the pool know
652+
pool.doneSleeping()
653+
} else {
654+
// lost CAS race, possibly concurrent `Interrupting`,
655+
// we'll need to re-read `parked`:
656+
notifyDoneSleeping()
657+
}
651658
}
652659
}
653660
}

0 commit comments

Comments
 (0)