Eric Bower
·
28 Aug 24
structured-concurrency.md
1---
2title: Structured Concurrency
3description: What is structured concurrency?
4---
5
6Resources:
7
8- [wiki](https://en.wikipedia.org/wiki/Structured_concurrency)
9- [await event horizon](https://frontside.com/blog/2023-12-11-await-event-horizon/)
10- [Why structured concurrency?](https://bower.sh/why-structured-concurrency)
11- [Thinking in Effection](https://frontside.com/effection/docs/thinking-in-effection)
12- [Delimited continuation](https://en.wikipedia.org/wiki/Delimited_continuation)
13- [Structured Concurrency](https://ericniebler.com/2020/11/08/structured-concurrency/)
14- [Structured Concurrency explained](https://www.thedevtavern.com/blog/posts/structured-concurrency-explained/)
15- [conc](https://github.com/sourcegraph/conc)
16
17This is a broad term so I'll make this specific to how `starfx` works.
18
19Under-the-hood, thunks and endpoints are registered under the root task. Every
20thunk and endpoint has their own supervisor that manages them. As a result, what
21we have is a single root task for your entire app that is being managed by
22supervisor tasks. When the root task receives a signal to shutdown itself (e.g.
23`task.halt()` or closing browser tab) it first must shutdown all children tasks
24before being resolved.
25
26When a child task throws an exception (whether intentional or otherwise) it will
27propagate that error up the task tree until it is caught or reaches the root
28task.
29
30In review:
31
32- There is a single root task for an app
33- The root task can spawn child tasks
34- If root task is halted then all child tasks are halted first
35- If a child task is halted or raises exception, it propagates error up the task
36 tree
37- An exception can be caught (e.g. `try`/`catch`) at any point in the task tree