Crate supertrees

source ·
Expand description

§Supervision trees for async Rust, inspired by Erlang/OTP

This crate provides a supervision tree implementation for async Rust, inspired by Erlang/OTP. It provides a Supertree struct that can be used to create a supervision tree, and a Worker trait that can be implemented by workers that can be added to the supervision tree.

This crate is designed to be used with async Rust and the Tokio runtime, but it could theoretically be used with other async runtimes as well.

In its current state, this crate is considered experimental and should not be used for production services, unless you are very excited about the idea and would be willing to contribute to the development of the crate. Notably, this crate lacks a lot of the features that are present in Erlang/OTP, such as monitoring, tracing, and distributed messaging, although Tokio provides a tracing and metrics system that could be used in conjunction with this crate (it has just not been tested yet).

For detailed examples, refer to the integration tests in the tests directory.


  • Supervision trees: Create a supervision tree with a root supervisor
  • Workers: Add workers to the supervision tree
  • Async workers: Workers are async and can use async/await syntax
  • Process isolation: The tree is constructed by forking processes, providing additional isolation (IPC not currently implemented)
  • Hierarchical structure: Create a hierarchy of workers and supervisors
  • Restart policies: Define restart policies for workers
  • Backoff policies: Define backoff policies for workers

§Comparison to Erlang/OTP

  • Unlike Erlang/OTP, this crate does not provide a distributed messaging. Another crate (courtesy chez moi) to look at is genserver, however it does not provide IPC.
  • This crate does not provide monitoring or tracing, but Tokio itself includes a tracing and metrics system that could be used in conjunction with this crate.
  • Erlang/OTP uses a preemptive green-threads scheduler, while this crate uses the Tokio runtime, which is a cooperative multitasking runtime.
  • Each separate supervisor within the tree is a separate thread, and there’s no shared memory or IPC between them (yet, PRs welcome). This is in contrast to Erlang/OTP, which uses a shared-nothing architecture.
  • Erlang/OTP is battle-tested and has been used in production for decades, whereas this crate is not.


A basic example of using this crate to create a supervision tree with a root supervisor, two sub-supervisors, and three workers:

use supertrees::{Restartable, Supertree, Worker};

struct MyWorker {
    num: i32,

impl MyWorker {
    fn new(num: i32) -> Self {
        Self { num }

impl Worker for MyWorker {
    // init() is called before the worker is started, and will be called
    // after each subsequent restart, so it should be safe to call this
    // repeatedly and  any state that needs to be reset should be reset here.
    fn init(
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send + 'static>> {
        let num = self.num;
        Box::pin(async move {
            println!("hi, I'm worker num={num} :)");

// We must provide a restart and backoff policy for the worker, but we can
// use the default policies.
impl Restartable for MyWorker {}

let root = Supertree::new()
    .add_supervisor(|s| {
            .add_supervisor(|s| s.add_worker(MyWorker::new(3)))

// Now you can start the supervision tree, which will run forever.
// Uncomment the line below to run the supervision tree.
// root.start();


  • Represents a backoff policy for retrying operations. If the reset duration passes, the backoff delay is reset to the minimum delay.
  • Represents a supertree, which is a supervision tree that contains a root supervisor.
  • Represents a supervisor that manages a collection of supervisors and tasks.


  • Represents the restart policy for a process or worker task.


  • Trait for restartable processes or worker tasks.
  • A trait representing a worker that can be restarted.