Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to fix #3075 #3081

Merged
merged 5 commits into from
Jul 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 29 additions & 20 deletions core/jvm/src/main/scala/cats/effect/IOFiberPlatform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -112,47 +112,56 @@ private[effect] abstract class IOFiberPlatform[A] extends AtomicBoolean(false) {
} yield {
Some {
IO async { finCb =>
val trigger = IO {
val trigger = IO.defer {
if (!many) {
cb.set(finCb)
}

// if done is false, and we can't get the semaphore, it means
// that the action hasn't *yet* started, so we busy-wait for it
var break = true
while (break && !done.get()) {
if (canInterrupt.tryAcquire()) {

def busyWait(): IO[Unit] = { // NB: side-effecting, see `defer` above
if (done.get()) {
IO.unit // ok, we're done
} else if (canInterrupt.tryAcquire()) {
try {
target.interrupt()
} finally {
break = false
canInterrupt.release()
}
IO.unit // ok, we've interrupted it
} else {
IO.defer(busyWait()) // retry
}
}

busyWait()
}

val repeat = if (many) {
IO {
// we need the outer try because we may be done *before* we enter
try {
while (!done.get()) {
if (canInterrupt.tryAcquire()) {
try {
while (!done.get()) {
target.interrupt() // it's hammer time!
}
} finally {
canInterrupt.release()
}

def reallyTryToInterrupt(): Boolean = {
if (canInterrupt.tryAcquire()) {
try {
while (!done.get()) {
target.interrupt() // it's hammer time!
}
} finally {
canInterrupt.release()
}
} finally {
manyDone.set(true) // signal that we're done looping
true // ok
} else {
false // retry
}
}

finCb(RightUnit)
def loop: IO[Unit] = IO.defer {
if (done.get() || reallyTryToInterrupt()) IO.unit
else loop
}

loop.guarantee(IO { manyDone.set(true) }) *> IO { finCb(RightUnit) }

} else {
IO {
if (done.get() && cb.get() != null) {
Expand Down