Skip to content

Commit

Permalink
Merge pull request #2350 from ChristopherDavenport/semaphoreFix
Browse files Browse the repository at this point in the history
Fix Cancellation Point in Semaphore
  • Loading branch information
djspiewak authored Sep 14, 2021
2 parents 7dc4aaa + 79c7ae8 commit 4b42b0f
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions std/shared/src/main/scala/cats/effect/std/Semaphore.scala
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,17 @@ object Semaphore {

if (n == 0) F.unit
else
state.modify {
case State(permits, waiting) =>
if (waiting.isEmpty) State(permits + n, waiting) -> F.unit
else {
val (newN, waitingNow, wakeup) = fulfil(n, waiting, Q())
State(newN, waitingNow) -> wakeup.traverse_(_.complete)
}
}.flatten
state
.modify {
case State(permits, waiting) =>
if (waiting.isEmpty) State(permits + n, waiting) -> F.unit
else {
val (newN, waitingNow, wakeup) = fulfil(n, waiting, Q())
State(newN, waitingNow) -> wakeup.traverse_(_.complete)
}
}
.flatten
.uncancelable
}

def available: F[Long] = state.get.map(_.permits)
Expand Down

0 comments on commit 4b42b0f

Please sign in to comment.