Repository navigation
Scoped Fiber init - #1379
Scoped Fiber init#1379
Conversation
| Fiber.init(Math.cos(42).toInt).map(_.get) | ||
| Fiber.initUnscoped(Math.cos(42).toInt).map(_.get) | ||
|
|
||
| // Avoid handling 'Async' directly | ||
| val b: Fiber[Int, Any] < Sync = | ||
| Fiber.init(a) | ||
| Fiber.initUnscoped(a) |
There was a problem hiding this comment.
@johnhungerford Fiber.init ? There are a lot of cases where it should be Fiber.init
There was a problem hiding this comment.
+1 I think we should prefer the scoped versions in the readme
💯 but we need to document ways to create some |
ahoy-jon
left a comment
There was a problem hiding this comment.
README.md / tests could use Fiber.init more. However, good to go for me.
| reduce: Reducible[Abort[E]] | ||
| ): Fiber[A, reduce.SReduced] < (Sync & S) = | ||
| Fiber.init { | ||
| Fiber.initUnscoped { |
There was a problem hiding this comment.
I don't think we need to address everything in this PR but, in some cases like this one, it makes more sense to add the pending Scope
There was a problem hiding this comment.
Agreed, I'll take a look through and see if there are some obvious other ones to update. I did that with the other two resource safety PRs.
|
|
||
| // Async combinator | ||
| case fork @ q"$eff.fork" => | ||
| Patch.replaceTree(fork, s"$eff.forkUnscoped") | ||
|
|
||
| case forkScoped @ q"$eff.forkScoped" => | ||
| Patch.replaceTree(forkScoped, s"$eff.fork") |
There was a problem hiding this comment.
Nice! I need to alter a bit the setup to test from 1.0-RC1 to main. (I will update in another PR)
There was a problem hiding this comment.
Yeah I was considering adding new rules just for RC1 -> 1.0 but would get a little complicated. Should we have separate rules for each RC?
There was a problem hiding this comment.
Yes, it's better, and we can keep tests for the last two rules.
| Console.printLine("hello").now | ||
|
|
||
| val async: Fiber[Nothing, Unit] < Async = Fiber.initUnscoped(Async.defer(Async.defer(Kyo.unit))) | ||
| val async: Fiber[Nothing, Unit] < Async = /* Consider using Fiber.init or Fiber.use to guarantee termination */ Fiber.initUnscoped(Async.defer(Async.defer(Kyo.unit))) |
There was a problem hiding this comment.
I will try to rework the rule to be able to do something like
/* Consider using Fiber.init or Fiber.use to guarantee termination */
val async: Fiber[Nothing, Unit] < Async = Fiber.initUnscoped(Async.defer(Async.defer(Kyo.unit)))|
@johnhungerford build is failing |
Fixes #1167
Problem
By default,
Fiber.initforks a fiber without ever guaranteeing it will be interrupted. The result is that fibers can easily end up lost in an application in a permanently suspended or long-running state. It would be good to have a behavior somewhat like ZIO, that guarantees cleanup of fibersSolution
Following the pattern established by #1168 , we change
Fiber.initto introduce theScopeeffect, interrupting the forked fiber in the registered finalizer; we add aFiber.usemethod to use a forked fiber within a function and clean it up afterwards; and finally, we addFiber.initUnscopedto produce the previous behavior, which forks a fiber and never guarantees interruptions.Notes
While this change was inspired by ZIO's behavior, it departs from ZIO in that by default ZIO binds each forked fiber to the parent fiber and interrupts child fiber whenever its parent fiber completes. I chose not to try to reproduce this for two reasons:
eff1on its own will just print1eff2will print1,A,B,2eff3will print1,2eff4will print1,4If you construct an effect that depends on ephemeral forked tasks, it will behave properly in a test or when used in an application in a forked handler, but when you sequence it other effects it may behave differently.
In conclusion, I think introducing the notion of parent fiber and child fiber makes it harder to reason about the fiber lifecycle than if fibers are just treated the same way as other resources.