Fibers
Effect is a highly concurrent framework powered by fibers. Fibers are lightweight virtual threads with resource-safe cancellation capabilities, enabling many features in Effect.
In this section, you will learn the basics of fibers and get familiar with some of the powerful low-level operators that utilize fibers.
JavaScript is inherently single-threaded, meaning it executes code in a single sequence of instructions. However, modern JavaScript environments use an event loop to manage asynchronous operations, creating the illusion of multitasking. In this context, virtual threads, or fibers, are logical threads simulated by the Effect runtime. They allow concurrent execution without relying on true multi-threading, which is not natively supported in JavaScript.
All effects in Effect are executed by fibers. If you didn’t create the fiber yourself, it was created by an operation you’re using (if it’s concurrent) or by the Effect runtime system.
A fiber is created any time an effect is run. When running effects concurrently, a fiber is created for each concurrent effect.
Even if you write “single-threaded” code with no concurrent operations, there will always be at least one fiber: the “main” fiber that executes your effect.
Effect fibers have a well-defined lifecycle based on the effect they are executing.
Every fiber exits with either a failure or success, depending on whether the effect it is executing fails or succeeds.
Effect fibers have unique identities, local state, and a status (such as done, running, or suspended).
To summarize:
- An
Effect
is a higher-level concept that describes an effectful computation. It is lazy and immutable, meaning it represents a computation that may produce a value or fail but does not immediately execute. - A fiber, on the other hand, represents the running execution of an
Effect
. It can be interrupted or awaited to retrieve its result. Think of it as a way to control and interact with the ongoing computation.
The Fiber
data type in Effect represents a “handle” on the execution of an effect.
Here is the general form of a Fiber
:
This type indicates that a fiber:
- Succeeds and returns a value of type
Success
- Fails with an error of type
Error
Fibers do not have an Requirements
type parameter because they only execute effects that have already had their requirements provided to them.
You can create a new fiber by forking an effect. This starts the effect in a new fiber, and you receive a reference to that fiber.
Example (Forking a Fiber)
In this example, the Fibonacci calculation is forked into its own fiber, allowing it to run independently of the main fiber. The reference to the fib10Fiber
can be used later to join or interrupt the fiber.
One common operation with fibers is joining them. By using the Fiber.join
function, you can wait for a fiber to complete and retrieve its result. The joined fiber will either succeed or fail, and the Effect
returned by join
reflects the outcome of the fiber.
Example (Joining a Fiber)
The Fiber.await
function is a helpful tool when working with fibers. It allows you to wait for a fiber to complete and retrieve detailed information about how it finished. The result is encapsulated in an Exit value, which gives you insight into whether the fiber succeeded, failed, or was interrupted.
Example (Awaiting Fiber Completion)
While developing concurrent applications, there are several cases that we need to interrupt the execution of other fibers, for example:
-
A parent fiber might start some child fibers to perform a task, and later the parent might decide that, it doesn’t need the result of some or all of the child fibers.
-
Two or more fibers start race with each other. The fiber whose result is computed first wins, and all other fibers are no longer needed, and should be interrupted.
-
In interactive applications, a user may want to stop some already running tasks, such as clicking on the “stop” button to prevent downloading more files.
-
Computations that run longer than expected should be aborted by using timeout operations.
-
When we have an application that perform compute-intensive tasks based on the user inputs, if the user changes the input we should cancel the current task and perform another one.
When it comes to interrupting fibers, a naive approach is to allow one fiber to forcefully terminate another fiber. However, this approach is not ideal because it can leave shared state in an inconsistent and unreliable state if the target fiber is in the middle of modifying that state. Therefore, it does not guarantee internal consistency of the shared mutable state.
Instead, there are two popular and valid solutions to tackle this problem:
-
Semi-asynchronous Interruption (Polling for Interruption): Imperative languages often employ polling as a semi-asynchronous signaling mechanism, such as Java. In this model, a fiber sends an interruption request to another fiber. The target fiber continuously polls the interrupt status and checks whether it has received any interruption requests from other fibers. If an interruption request is detected, the target fiber terminates itself as soon as possible.
With this solution, the fiber itself handles critical sections. So, if a fiber is in the middle of a critical section and receives an interruption request, it ignores the interruption and defers its handling until after the critical section.
However, one drawback of this approach is that if the programmer forgets to poll regularly, the target fiber can become unresponsive, leading to deadlocks. Additionally, polling a global flag is not aligned with the functional paradigm followed by Effect.
-
Asynchronous Interruption: In asynchronous interruption, a fiber is allowed to terminate another fiber. The target fiber is not responsible for polling the interrupt status. Instead, during critical sections, the target fiber disables the interruptibility of those regions. This is a purely functional solution that doesn’t require polling a global state. Effect adopts this solution for its interruption model, which is a fully asynchronous signaling mechanism.
This mechanism overcomes the drawback of forgetting to poll regularly. It is also fully compatible with the functional paradigm because in a purely functional computation, we can abort the computation at any point, except during critical sections where interruption is disabled.
Fibers can be interrupted if their result is no longer needed. This action immediately stops the fiber and safely runs all finalizers to release any resources.
Like Fiber.await
, the Fiber.interrupt
function returns an Exit value that provides detailed information about how the fiber ended.
Example (Interrupting a Fiber)
By default, the effect returned by Fiber.interrupt
waits until the fiber has fully terminated before resuming. This ensures that no new fibers are started before the previous ones have finished, a behavior known as “back-pressuring.”
If you do not require this waiting behavior, you can fork the interruption itself, allowing the main program to proceed without waiting for the fiber to terminate:
Example (Forking an Interruption)
There is also a shorthand for background interruption called Fiber.interruptFork
.
The Fiber.zip
and Fiber.zipWith
functions allow you to combine two fibers into one. The resulting fiber will produce the results of both input fibers. If either fiber fails, the combined fiber will also fail.
Example (Combining Fibers with Fiber.zip
)
In this example, both fibers run concurrently, and the results are combined into a tuple.
Another way to compose fibers is by using Fiber.orElse
. This function allows you to provide an alternative fiber that will execute if the first one fails. If the first fiber succeeds, its result will be returned. If it fails, the second fiber will run instead, and its result will be returned regardless of its outcome.
Example (Providing a Fallback Fiber with Fiber.orElse
)
When we fork fibers, depending on how we fork them we can have four different lifetime strategies for the child fibers:
-
Fork With Automatic Supervision. If we use the ordinary
Effect.fork
operation, the child fiber will be automatically supervised by the parent fiber. The lifetime child fibers are tied to the lifetime of their parent fiber. This means that these fibers will be terminated either when they end naturally, or when their parent fiber is terminated. -
Fork in Global Scope (Daemon). Sometimes we want to run long-running background fibers that aren’t tied to their parent fiber, and also we want to fork them in a global scope. Any fiber that is forked in global scope will become daemon fiber. This can be achieved by using the
Effect.forkDaemon
operator. As these fibers have no parent, they are not supervised, and they will be terminated when they end naturally, or when our application is terminated. -
Fork in Local Scope. Sometimes, we want to run a background fiber that isn’t tied to its parent fiber, but we want to live that fiber in the local scope. We can fork fibers in the local scope by using
Effect.forkScoped
. Such fibers can outlive their parent fiber (so they are not supervised by their parents), and they will be terminated when their life end or their local scope is closed. -
Fork in Specific Scope. This is similar to the previous strategy, but we can have more fine-grained control over the lifetime of the child fiber by forking it in a specific scope. We can do this by using the
Effect.forkIn
operator.
Effect follows a structured concurrency model, where child fibers’ lifetimes are tied to their parent. Simply put, the lifespan of a fiber depends on the lifespan of its parent fiber.
Example (Automatically Supervised Child Fiber)
In this scenario, the parent
fiber spawns a child
fiber that repeatedly prints a message every second.
The child
fiber will be terminated when the parent
fiber completes.
This behavior can be extended to any level of nested fibers, ensuring a predictable and controlled fiber lifecycle.
You can create a long-running background fiber using Effect.forkDaemon
. This type of fiber, known as a daemon fiber, is not tied to the lifecycle of its parent fiber. Instead, its lifetime is linked to the global scope. A daemon fiber continues running even if its parent fiber is terminated and will only stop when the global scope is closed or the fiber completes naturally.
Example (Creating a Daemon Fiber)
This example shows how daemon fibers can continue running in the background even after the parent fiber has finished.
Even if the parent fiber is interrupted, the daemon fiber will continue running independently.
Example (Interrupting the Parent Fiber)
In this example, interrupting the parent fiber doesn’t affect the daemon fiber, which continues to run in the background.
Sometimes we want to create a fiber that is tied to a local scope, meaning its lifetime is not dependent on its parent fiber but is bound to the local scope in which it was forked. This can be done using the Effect.forkScoped
operator.
Fibers created with Effect.forkScoped
can outlive their parent fibers and will only be terminated when the local scope itself is closed.
Example (Forking a Fiber in a Local Scope)
In this example, the child
fiber continues to run beyond the lifetime of the parent
fiber. The child
fiber is tied to the local scope and will be terminated only when the scope ends.
There are some cases where we need more fine-grained control, so we want to fork a fiber in a specific scope.
We can use the Effect.forkIn
operator which takes the target scope as an argument.
Example (Forking a Fiber in a Specific Scope)
In this example, the child
fiber is forked into the outerScope
, allowing it to outlive the inner scope but still be terminated when the outerScope
is closed.
Forked fibers begin execution after the current fiber completes or yields.
Example (Fiber Starting After Value Change)
In the following example, the changes
stream only captures a single value, 2
.
This happens because the fiber created by Effect.fork
starts after the value is updated.
If we add Effect.yieldNow()
to allow the current fiber to yield, the stream will capture all values.
This happens because the fiber running the stream has a chance to start before the values change.
Example (Forcing Fiber to Yield)