Skip to content

Commit 090aad0

Browse files
committed
if alive is innermost resource, it releases first
1 parent 24d1bdb commit 090aad0

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

std/shared/src/main/scala/cats/effect/std/Dispatcher.scala

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,6 @@ object Dispatcher {
240240
states
241241
})
242242
ec <- Resource.eval(F.executionContext)
243-
alive <- Resource.make(F.delay(new AtomicBoolean(true)))(ref => F.delay(ref.set(false)))
244243

245244
// supervisor for the main loop, which needs to always restart unless the Supervisor itself is canceled
246245
// critically, inner actions can be canceled without impacting the loop itself
@@ -303,13 +302,15 @@ object Dispatcher {
303302
val worker = dispatcher(doneR, latch, states(n))
304303
val release = F.delay(latch.getAndSet(Open)())
305304
Resource.make(supervisor.supervise(worker)) { _ =>
306-
F.delay(doneR.set(true)) *> F.delay(alive.set(false)) *> step(
307-
states(n),
308-
F.unit) *> release
305+
F.delay(doneR.set(true)) *> step(states(n), F.unit) *> release
309306
}
310307
}
311308
}
312309
}
310+
311+
// Alive is the innermost resource so that when releasing
312+
// the very first thing we do is set dispatcher to un-alive
313+
alive <- Resource.make(F.delay(new AtomicBoolean(true)))(ref => F.delay(ref.set(false)))
313314
} yield {
314315
new Dispatcher[F] {
315316
def unsafeToFutureCancelable[E](fe: F[E]): (Future[E], () => Future[Unit]) = {

0 commit comments

Comments
 (0)