r/ProgrammingLanguages • u/mttd • Mar 31 '26
From error-handling to structured concurrency
https://blog.nelhage.com/post/concurrent-error-handling/
20
Upvotes
r/ProgrammingLanguages • u/mttd • Mar 31 '26
3
u/phischu Effekt Mar 31 '26
Thank you for this valuable resource. Based on it, I have implemented some of the ideas in Effekt, you can try them online.
What if we always waited on tasks?
We define an effect
createthat allows us to spawn a task with a result of typeA.A task is box that has only access to the resources
io,async, andglobal. Boxes are mechanism that is distinct from effects.We then define a handler
launcherthat handles thecreateeffect by creating a promise for the task, then resuming, then awaiting.It returns a list of all results when all created tasks are done.
I hate to cancel, but…
Now we define an effect
cancelthat never returns, and an effectyield, that itself uses thecanceleffect to signal cancellation.As a user or library author we will have to strategically insert yield points into our program.
We handle
cancellationwith a global reference that we set totruewhen the program usescancel.When the program yields we check if cancellation happened, and either raise
cancelat the call-site ofyield, or resume normally.A tree of tasks with cancellation
We combine these two ideas with a new effect
startthat starts a task that can be cancel the other tasks and itself be cancelled at yield points.The
concurrencyhandler uses thelauncherto make sure we wait for all tasks to finnish, creates a shared global mutable reference, and handles cancellation of each started task.It returns a list of the results of those tasks that weren't cancelled.
Finally an example
To try our new toy library, we start two tasks that print, sleep, yield, and one of them cancels the other one.
It prints
and we encourage you to experiment with it in our online playground.
1
u/marshaharsha Apr 05 '26
The article mainly reinvents structured concurrency, and the author says so towards the end, and links to Nathaniel Smith’s original article on structured concurrency in Trio. The point of view is error handling rather than structured programming, but this doesn’t change much that I can see. I did enjoy these two observations:
(1) Assuming some form of RAII is being used, a single-task error-handling design based on exceptions and one based on errors as values will often end up processing errors the same way: unwind the stack, allowing each frame to release resources and revert mutations. The goal in the concurrent setting is to design something equally conventional and straightforward.
(2) In a single-task setting, I can afford to think of error handling as something I will add later or that is optional in one-off code. In a multi-task setting, failing to handle errors at the earliest stage of writing code can result in deadlocks, infinite waiting, and other badness that completely blocks development. The goal is to have a multi-task environment that, by default, handles errors well enough to allow a good-enough-for-now attitude.