Skip to content

The supervisor

Source files: 67 · checked against Etemenanki 46c12e4 · katana v6.1.0
  • Etemenanki/supervisor/Cargo.toml
  • Etemenanki/supervisor/src/lib.rs
  • Etemenanki/supervisor/src/supervisor.rs
  • Etemenanki/supervisor/src/policy.rs
  • Etemenanki/supervisor/src/guard.rs
  • Etemenanki/supervisor/src/carrier.rs
  • Etemenanki/supervisor/src/ping.rs
  • Etemenanki/supervisor/src/entity/mod.rs
  • Etemenanki/supervisor/src/entity/id.rs
  • Etemenanki/supervisor/src/entity/session.rs
  • Etemenanki/supervisor/src/entity/user.rs
  • Etemenanki/supervisor/src/entity/usage.rs
  • Etemenanki/supervisor/src/build/mod.rs
  • Etemenanki/supervisor/src/build/apply.rs
  • Etemenanki/supervisor/src/build/validate.rs
  • Etemenanki/supervisor/src/build/users.rs
  • Etemenanki/supervisor/src/build/dns.rs
  • Etemenanki/supervisor/src/build/outbound.rs
  • Etemenanki/supervisor/src/build/inbound.rs
  • Etemenanki/supervisor/src/topology/mod.rs
  • Etemenanki/supervisor/src/topology/spec_plan/mod.rs
  • Etemenanki/supervisor/src/topology/spec_plan/plan.rs
  • Etemenanki/supervisor/src/topology/plane.rs
  • Etemenanki/supervisor/src/topology/router.rs
  • Etemenanki/supervisor/src/topology/inbound/mod.rs
  • Etemenanki/supervisor/src/topology/balancer.rs
  • Etemenanki/supervisor/src/topology/flow.rs
  • Etemenanki/supervisor/src/connector.rs
  • Etemenanki/supervisor/src/serve.rs
  • Etemenanki/supervisor/src/system/mod.rs
  • Etemenanki/supervisor/src/system/blocking.rs
  • Etemenanki/supervisor/src/system/listener.rs
  • Etemenanki/supervisor/src/system/handoff.rs
  • Etemenanki/supervisor/src/track/mod.rs
  • Etemenanki/supervisor/src/track/sampler.rs
  • Etemenanki/supervisor/tests/hot_swap.rs
  • Etemenanki/supervisor/tests/socket_policy.rs
  • Etemenanki/supervisor/tests/tracking.rs
  • Etemenanki/supervisor/tests/guard.rs
  • Etemenanki/supervisor/tests/anytls.rs
  • Etemenanki/supervisor/tests/sss.rs
  • Etemenanki/supervisor/tests/ping.rs
  • Etemenanki/supervisor/tests/handoff.rs
  • Etemenanki/supervisor/tests/support/mod.rs
  • Etemenanki/supervisor/tests/unit/supervisor.rs
  • Etemenanki/supervisor/tests/unit/session.rs
  • Etemenanki/supervisor/tests/unit/plane.rs
  • Etemenanki/supervisor/tests/unit/plan.rs
  • Etemenanki/supervisor/tests/unit/validate.rs
  • Etemenanki/supervisor/benches/tracking.rs
  • Etemenanki/environment/src/dial/socket.rs
  • Etemenanki/protocols/src/hysteria/server/inbound.rs
  • Etemenanki/app/src/main.rs
  • Etemenanki/app/src/instance.rs
  • Etemenanki/app/src/lower.rs
  • Etemenanki/app/src/api.rs
  • Etemenanki/ffi/src/proxy.rs
  • Etemenanki/ffi/src/platform.rs
  • Etemenanki/ffi/src/client.rs
  • Etemenanki/webclient/src/tracking.rs
  • Etemenanki/app/tests/unit/subscribe.rs
  • Etemenanki/app/tests/integration/e2e_balancer.rs
  • Etemenanki/app/tests/integration/e2e_dns.rs
  • katana/src/manager/node.rs
  • katana/src/lower/mod.rs
  • katana/src/lower/inbound.rs
  • katana/src/api/mod.rs

etemenanki-supervisor is the runtime every Etemenanki program runs on. etemenanki-app, the mobile library etemenanki-ffi and katana each lower their own input (a TOML file and its subscribe file, the strings a phone app passes in, a panel’s answers) into one typed Spec<U> and hand it to a Supervisor. The supervisor validates the spec, plans a reconcile from what runs, and applies it in two phases: a fallible prepare that changes nothing that runs, then an infallible commit. The reconcile keeps what did not change: a session does not belong to the listener that accepted it, an unchanged bind keeps its socket, and an unchanged outbound keeps its state. The crate knows nothing of config files, their formats, or how a reload is triggered.

This page is the entry to the supervisor section. It covers the split between the supervisor and its front ends, the module map, the public API, the actor every change goes through and the data flow of an apply, the cancellation tokens and the task tree, the socket policy, shutdown, and the ids that name what runs. The spec’s types are on The spec, the semantic rules on Validation, and every step of the plan, prepare and commit on Reconcile.

The supervisor:

  • validates a spec against every semantic rule and refuses it as a whole when one fails (build/validate.rs is the only owner of those rules);
  • plans, per resource, whether to keep, build, swap, stop, close or drain, by comparing the running spec with the desired one;
  • builds outbounds, balancers, the route table, the DNS resolvers and DNS service, inbound handlers and user tables;
  • binds listeners: TCP, UDP for Hysteria 2, Unix sockets and handoff sockets (on Unix only), and TUN devices it creates (a utun on macOS, a Wintun adapter on Windows) or, on Unix, is handed as a descriptor (system/listener.rs → bind, topology/inbound/mod.rs → TunSource);
  • runs the accept loops, registers every accepted connection as a session, and drives each connection’s protocol runtime; on a handoff socket it takes the TCP connections a relay running on the same host hands over, with the client the relay names, and serves them as its TCP listener would (system/handoff.rs); on a REALITY inbound it relays a socket that is not a client of ours to the cover site, and on an sss inbound it drains a stream refused before authentication or hands it to its fallback (serve.rs);
  • routes every new flow, mux sub-flow and UDP packet through the plane current at that moment;
  • keeps a version per outbound build, and hands a replaced or removed version to its drain policy (a WireGuard version is always closed);
  • admits users per inbound, revokes removed users under a removal policy, and enforces per-user speed limits and device limits (entity/session.rs → Sessions::set_device_limits, Session::admit);
  • turns away clients that keep failing to authenticate, when its policies ask for it: it bans an IP (an IPv6 client by its /64) after too many failed authentications, and closes a gRPC connection whose streams keep failing (guard.rs, carrier.rs); on a WebSocket inbound behind a reverse proxy, it holds the client a trusted proxy names to those bans and to the device limit (serve.rs → real_ip_source);
  • tracks every live flow, samples rates, and keeps a per-user usage ledger;
  • closes, when a front end asks after a switch, the live flows a route change moved to another outbound (Supervisor::close_rerouted), and measures one outbound’s delay with HTTP requests made through it (Supervisor::http_ping, ping.rs);
  • stops all of it on shutdown.

It does not:

  • parse any format or read config files. Certificates, keys and user lists arrive in the spec as bytes and typed values. The only files it reads are the geo data files a RouteSpec names, and the system hosts file when a split DNS spec sets use_hosts.
  • decide when to reload. A file watcher, a panel poll or an API call is the front end’s business; the supervisor only answers apply.
  • log config errors. It returns an ApplyError, and the front end decides how to show it.

The crate docs (supervisor/src/lib.rs) name four things an apply keeps:

What How it is kept Owner page
Connections Each accepted connection is a session under its own cancellation token, a child of the supervisor’s root token, not of its listener. Users and sessions
Listeners A listener is keyed by its BindSpec. An unchanged bind keeps its socket or device, and only the handler serving new connections is swapped. An inbound with a handoff socket has two listeners, each kept on its own, and both count against the inbound’s one set of admission caps. Serving
Routing Routes and outbounds form one Plane behind an atomic cell. Every new flow, mux sub-flow and UDP packet reads the current plane; a route change does not re-route what is already open. A front end that wants those flows moved calls close_rerouted after the apply. Routing plane
Stateful outbounds An outbound whose spec is unchanged, and whose resolvers did not change, is carried over by Arc, keeping its QUIC connection, tunnel or balancer health. A changed DnsSpec rebuilds every outbound except a blackhole (see Reconcile). Outbounds

Users are a resource of their own. They change through set_users, upsert_user and remove_user, which rebuild only the user tables of the inbounds admitting the changed set. A user’s speed limit is shared by all their flows and takes effect on live flows at their next read or write, without ending their sessions. A user’s device limit (UserSpec::device_limit) caps how many client IPs may have a session of the user at once; it is published at commit, and lowering it closes no session.

These end live connections:

Cause Decided by Default
A user no longer admitted by an inbound: removed from its set, or their credential for it changed UserRemovalPolicy, per inbound or supervisor-wide Close
An outbound version replaced or removed DrainPolicy, per outbound or supervisor-wide; a WireGuard version is always drained with Close Keep: no action
An inbound removed from the spec, or its tag renamed Always: its sessions are closed none
A change to a Hysteria 2 listener’s obfuscation, which the planner marks Step::Disrupt Refused without ApplyOptions::allow_disruptive. katana sets it, so a Hysteria 2 obfuscation change from the panel goes through refused
A change to a TUN inbound’s device or settings etemenanki-app refuses it, and a restart applies it refused
A gRPC connection whose streams keep failing authentication: once carrier_max_failures of its streams in a row have failed, the connection is closed Policies::carrier_max_failures None: never
A new TCP connection from an IP banned for failed authentications: dropped before its transport handshake, while the IP’s open connections run on Policies::ip_ban None: no IP is banned
A session whose first flow would take its user’s sessions past their device limit: refused, and its token cancelled UserSpec::device_limit None: any number of IPs
close(selector) The caller none
Tracker::kill(id) or Tracker::kill_where(select): one flow, or the flows a predicate picks The caller: the FFI’s close_connection, the REST API (webclient/src/tracking.rs) none
close_rerouted(): the live flows the current routing sends to another outbound than the one carrying them The caller: the REST API’s route switch with "close": true, the FFI’s set_route with close_connections and its close_rerouted none: an apply never calls it
shutdown(grace) The caller; live connections get up to grace first none

supervisor/src/policy.rs holds the policies and their supervisor-wide defaults:

supervisor/src/policy.rs
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum UserRemovalPolicy {
Keep,
#[default]
Close,
CloseAfter(Duration),
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum DrainPolicy {
#[default]
Keep,
Close,
CloseAfter(Duration),
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Policies {
pub user_removal: UserRemovalPolicy,
pub drain: DrainPolicy,
pub ip_ban: Option<IpBanPolicy>,
pub carrier_max_failures: Option<NonZeroU32>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct IpBanPolicy {
pub max_failures: NonZeroU32,
pub window: Duration,
pub duration: Duration,
}
  • A UserRemovalPolicy is applied to each live session bound to a revoked principal (entity/session.rs → enforce): Keep leaves the session’s token alone, Close cancels it at once, and CloseAfter(grace) cancels it after grace unless the session ends first.
  • A DrainPolicy is applied to an old outbound version (drain in supervisor.rs): Keep takes no action on the old version’s flows, Close kills them at once (Target::close_flows), and CloseAfter(grace) does so after grace.
  • The defaults differ on purpose. Removing a user defaults to Close because revoking access is meant to take effect. Replacing an outbound defaults to Keep because a TCP flow cannot move to the successor mid-stream, so by default the drain does not close the old version’s flows.
  • A WireGuard outbound is the exception: a replaced or removed WireGuard version is always drained with Close, whatever the policy, because its flows do not keep its device alive and a successor on the same key and peer could not run beside it. The planner decides this from the running spec (topology/spec_plan/plan.rs → the drain_policy closure), so an outbound turned into WireGuard drains its old version under its own policy.
  • Spec::policies sets the supervisor-wide values. InboundSpec::user_removal overrides the removal policy for one inbound (removal_policy in supervisor.rs falls back to spec.policies.user_removal), and OutboundSpec::drain overrides the drain policy for one outbound (resolved by the planner, see Reconcile).
  • Both CloseAfter timers run on the supervisor’s TaskTracker. A removal timer selects on its session’s token and a drain timer on the root token; session tokens are children of the root (see The task tree).
  • ip_ban and carrier_max_failures are read by the guard (guard.rs → Guard), one per supervisor. serve.rs → drive reports how a stream’s handshake ended to it through a Report: a refused credential (PermissionDenied) as Report::failed, an established core as Report::succeeded. With an IpBanPolicy, a failed authentication counts against the client’s IP (an IPv6 client’s whole /64 counts as one IP, an IPv4-mapped address as its IPv4 address); once max_failures have counted within window of the first, the IP is banned for duration: a new TCP connection from it is dropped before its transport handshake, while its connections already open run on. A successful authentication clears the IP’s count. With carrier_max_failures, a gRPC connection is closed once that many of its streams in a row have failed authentication. Both default to None, which never bans and never closes.
  • Every apply’s commit hands ip_ban and carrier_max_failures to the guard (Guard::set_policy), so they take effect at the commit that brings them. Turning bans off lifts every ban. How the guard counts is on Serving.
Concern Front end Supervisor
Input format Parses its own: TOML, a subscribe file, a panel’s JSON, FFI arguments Takes only a Spec<U>
Parsing and lowering Reports syntax errors and unknown keys; parses its strings (UUIDs, method names, addresses, keys, CIDRs); applies its schema’s defaults; names its users; refuses key combinations a spec cannot express Never sees any of it
Semantics (duplicate tags, unknown references, how the typed pieces fit together) Does not re-check them build/validate.rs, the one owner
Files Reads certificates, keys and user lists; passes bytes Reads geo data and the hosts file only
User identity Chooses the key type U Hands out an internal UserKey per admitted U
When to change Watches files, polls a panel, serves an API Answers apply, update and user edits
Disruptive changes Chooses ApplyOptions Refuses them without allow_disruptive
Host socket policy Passes SocketOptions to the builder Applies it to every outbound socket
Usage Polls take_usage or installs a UsageSink Counts wire bytes per user
Logging Sets up tracing and wraps ApplyError in its own text Logs through tracing under its own targets

The actor and the listeners it starts log these lines. What the accept loops and connections log is on Serving.

Level Target Line When
debug etemenanki_supervisor::supervisor applied: {} built, {} reused, {} swapped, {} drained At the end of every commit, with the lengths of those four report fields
info etemenanki_supervisor::system::listener inbound {tag} listening on {bind} Listener::start, for each listener a commit starts
info etemenanki_supervisor::system::listener inbound {tag} owns tun device {name} A bind that created a TUN device, with the device’s name
info etemenanki_supervisor::system::listener inbound {tag} serves a supplied tun device A bind that adopted a supplied descriptor
error etemenanki_supervisor::system::listener could not move {staged} over {path}, so no one reaches the listener there: {e} Listener::start could not rename a Unix or handoff socket from its staging name over its path (SocketFile::publish)
debug etemenanki_supervisor::system::listener could not remove {path}: {e} A Unix or handoff listener drops and its socket file, still the one it created, cannot be removed (SocketFile::drop)

supervisor/Cargo.toml declares etemenanki-supervisor version 1.1.0, published only to a private Cargo registry. It depends on etemenanki-concepts (requirement 2.1.2, the first whose proxy client flushes after each datagram), etemenanki-environment (3.2) and etemenanki-protocols (6.2, the first whose VLESS datagram codec takes a flow, VlessDatagram::with_flow) with the hysteria, tun and reality features always on, so every supervisor can serve Hysteria 2, TUN and REALITY. The reality feature pulls in the etemenanki-reality crate behind the protocols crate’s own wrapper types; no type of that crate appears in the supervisor’s API. Among the other dependencies, the manifest’s own comments say why these are there: scc is the live-flow registry and the waker a flow is killed through, quinn only names the TLS material a Hysteria 2 listener is swapped to, serde lets a UsageDelta reach a panel as it is (“No config types: this crate never parses a config format”), cidr holds the networks of the proxies a WebSocket inbound trusts to name its client (RealIpSpec::trusted), and http and httparse are the delay test’s URL and the response heads it reads. On Unix only, socket2 and libc serve the handoff socket: it is bound before it listens, so its mode is set first, and a handed-over TCP socket is received with recvmsg and SCM_RIGHTS, which neither std nor socket2 offers. The spec’s structs and their fields are public and not #[non_exhaustive], so a new field breaks every front end that builds one: the fields 0.12.0 added (VLESS’s flow, a transport on the SOCKS and Shadowsocks inbounds, the transport specs’ reality) made it a breaking release. Three enums are #[non_exhaustive] since that release, InboundProtocolSpec, OutboundProtocolSpec and StreamShape, so a new protocol or transport is an addition and a front end’s match keeps a wildcard arm. The other public enums, StreamProtocol and Outbound among them, are exhaustive, so the sss variants added to both called for a new minor version. That release is 1.0.0, the first outside 0.x. 1.0.1 builds a Vision VLESS outbound’s datagram codec with the flow, so UDP through it rides XUDP. 1.1.0 adds close_rerouted, http_ping and the public ping module, which is new API and so a minor version.

The development dependencies are tokio with test-util (so grace periods can be tested against a paused clock), openssl for test certificates, proptest for the usage ledger’s property tests, criterion for the tracking bench, socket2 for the socket type a socket-policy hook is shown, h2, http and bytes for a gRPC client opening many streams on one connection, and tokio-tungstenite for a WebSocket client whose upgrade carries the headers a proxy would add (both in tests/guard.rs).

  • Directorysupervisor/
    • Cargo.toml
    • Directorysrc/
      • lib.rs crate docs, modules and re-exports
      • supervisor.rs Supervisor, SupervisorBuilder, check, and the actor
      • policy.rs UserRemovalPolicy, DrainPolicy, Policies, IpBanPolicy
      • ping.rs HttpPing, PingError, PingUrl: the delay test through one outbound
      • guard.rs Guard, Report: failed authentications (crate-private)
      • carrier.rs Carrier: what the streams of one gRPC connection share (crate-private)
      • serve.rs accept loops and serving one connection (crate-private)
      • connector.rs AppConnector, the connector every inbound dials through
      • Directorybuild/ constructing what runs from a validated spec
        • mod.rs
        • apply.rs ApplyReport, ApplyError
        • validate.rs every semantic rule of a spec
        • dns.rs resolvers and the DNS service
        • inbound.rs inbound handlers and their user tables
        • outbound.rs one outbound handler per protocol
        • route.rs the route table and its geo data
        • users.rs admissions, principals and user keys
      • Directoryentity/
        • mod.rs
        • id.rs OutboundId, Resource, SessionId, UserKey, FlowId, RuleId
        • user.rs UserId, UserName, UserSpec, UserSet, credentials
        • session.rs the session registry
        • usage.rs wire counters, the usage ledger, UsageSink
      • Directorysystem/
        • mod.rs
        • blocking.rs off_runtime, drop_off_runtime: work kept off the async workers
        • handoff.rs receiving a TCP socket a relay hands over (Unix only)
        • listener.rs binding a BindSpec and the task serving it
      • Directorytopology/
        • mod.rs
        • Directoryspec_plan/
          • mod.rs Spec, ObfsSpec, Secret
          • inbound.rs inbound specs
          • outbound.rs outbound specs
          • transport.rs stream shapes and TLS specs
          • route.rs RouteSpec, BalancerSpec
          • dns.rs DnsSpec
          • plan.rs RunningState, Plan, Step, plan
        • Directoryinbound/
          • mod.rs BindSpec and the inbound handlers
        • Directoryoutbound/
          • mod.rs Outbound, one variant per kind
          • proxy.rs proxy protocol clients
          • freedom.rs direct TCP and UDP
          • udp_fanout.rs FanOutLink, routing a UDP association per packet
          • dns.rs the DNS service as an outbound, RoutedDialer
        • plane.rs Plane, PlaneCell, Target
        • router.rs the compiled route table
        • balancer.rs balancers and their health probes
        • flow.rs Flow, Principal, FlowContext
      • Directorytrack/
        • mod.rs Tracker, FlowEntry, FlowEvent
        • metered.rs Metered, MeteredDatagram
        • pace.rs per-user token buckets
        • sampler.rs the sampler and StatsSnapshot
    • Directorytests/
      • hot_swap.rs applies against live connections
      • socket_policy.rs the socket-policy hook
      • tracking.rs flows, sessions and usage
      • ping.rs delay tests through a named outbound
      • guard.rs PROXY headers, forwarded WebSocket clients, device limits, IP bans and gRPC connection closes
      • handoff.rs handed-over connections end to end (Unix only)
      • Directorysupport/
        • mod.rs shared specs, echo servers, protocol clients
      • Directoryunit/ unit tests, compiled into the crate
        • …
    • Directorybenches/
      • tracking.rs

lib.rs makes build, connector, entity, ping, policy, system, topology and track public, keeps carrier, guard and serve crate-private and supervisor private, and re-exports the entry points at the crate root:

supervisor/src/lib.rs
pub use build::apply;
pub use supervisor::{ApplyOptions, Selector, SessionInfo, Supervisor, SupervisorBuilder, check};

Inside build, only apply and validate are public; the builders (dns, inbound, outbound, route, users) are crate-private. system has no public item: its modules, blocking, listener and, on Unix only, handoff, are crate-private. The unit tests live under tests/unit/ and are compiled into the modules they test through #[path] attributes (plan.rs, validate.rs, session.rs, plane.rs, router.rs, balancer.rs, track.rs, usage.rs, serve.rs, dns_outbound.rs, guard.rs, ping.rs and supervisor.rs).

Module Documented on
topology::spec_plan (types), policy, entity::user (types) The spec
build::validate, build::apply Validation
topology::spec_plan::plan, the actor’s prepare and commit Reconcile
serve, guard, carrier, system::listener, system::handoff, topology::inbound, build::inbound Serving
entity::user, entity::session, build::users Users and sessions
connector, topology::plane, topology::router, topology::flow, build::route Routing plane
topology::outbound, topology::balancer, build::outbound, ping Outbounds
build::dns, topology::outbound::dns, topology::spec_plan DNS types DNS
track Tracking
entity::usage Usage
supervisor (the handle, the builder, the actor), system::blocking, entity::id this page
supervisor/src/supervisor.rs
#[derive(Clone)]
pub struct Supervisor<U: UserId> {
commands: mpsc::Sender<Command<U>>,
plane: PlaneCell,
tracker: Tracker,
usage: Arc<UsageBook<U>>,
}
impl<U: UserId> Supervisor<U> {
pub fn builder() -> SupervisorBuilder<U>;
pub async fn start(spec: Spec<U>) -> Result<(Self, ApplyReport), ApplyError>;
pub async fn apply(&self, spec: Spec<U>) -> Result<ApplyReport, ApplyError>;
pub async fn apply_with(&self, spec: Spec<U>, options: ApplyOptions)
-> Result<ApplyReport, ApplyError>;
pub async fn update(&self, edit: impl FnOnce(&mut Spec<U>) + Send + 'static)
-> Result<ApplyReport, ApplyError>;
pub async fn update_with(
&self,
edit: impl FnOnce(&mut Spec<U>) + Send + 'static,
options: ApplyOptions,
) -> Result<ApplyReport, ApplyError>;
pub async fn set_users(&self, set: &str, users: BTreeMap<U, UserSpec>)
-> Result<(), ApplyError>;
pub async fn upsert_user(&self, set: &str, id: U, user: UserSpec) -> Result<(), ApplyError>;
pub async fn remove_user(&self, set: &str, id: U) -> Result<bool, ApplyError>;
pub async fn close(&self, selector: Selector<U>) -> usize;
pub async fn sessions(&self) -> Vec<SessionInfo<U>>;
pub fn epoch(&self) -> u64;
pub fn close_rerouted(&self) -> usize;
pub async fn http_ping(&self, tag: &str, url: &str, timeout: Duration)
-> Result<HttpPing, PingError>;
pub fn tracker(&self) -> &Tracker;
pub fn take_usage(&self) -> Vec<UsageDelta<U>>;
pub fn usage_snapshot(&self) -> Vec<UsageTotal<U>>;
pub async fn shutdown(&self, grace: Duration);
}

A Supervisor is a handle. Cloning it gives another handle to the same running supervisor: the command sender, the plane cell, the tracker and the usage book are all shared. When the last handle is dropped without shutdown, the actor sees its command channel close and stops everything at once (see Shutdown).

Method Through the actor What it does
builder no A SupervisorBuilder with every default.
start runs the first apply itself Self::builder().start(spec).
apply, apply_with Command::Apply Reconcile to spec. apply passes ApplyOptions::default().
update, update_with Command::Update Reconcile to the running spec as edit changes it.
set_users Command::Users Replace every user of the set set.
upsert_user Command::Users Add one user to set, or replace their UserSpec.
remove_user Command::Users Remove one user from set; true if they were in it.
close Command::Close Close the sessions selector names at once; returns how many this call closed.
sessions Command::Sessions Every live session, by id.
epoch no How many planes have been published: self.plane.load().epoch().
close_rerouted no Kill every live flow the current plane sends to another outbound than the one carrying it; returns how many. See Closing what a route change moved.
http_ping Command::Target, for the lookup only Look the outbound or balancer tag up, then run a delay test through it on the caller’s task. See Delay tests through an outbound.
tracker no The Tracker: live flows and sessions, flow events, sampled rates.
take_usage no Every user’s wire bytes since the previous take.
usage_snapshot no Every user’s wire bytes since start, taken or not.
shutdown Command::Shutdown Stop accepting, give live connections up to grace, close the rest, wait for them.

apply replaces the whole spec, user sets included. update starts from the spec the actor holds, which includes every user edit committed since the last apply, so a front end that edits users one at a time and later changes one listener can use update without re-sending its users.

take_usage returns at most one UsageDelta per user and none for a user who moved nothing. It visits one account per active user. Its price is lag: a live session’s bytes are counted at each sampler tick, so what it moved since the last tick comes in a later take; a session that has ended is counted in full. Bytes a usage sink took are never returned by take_usage, and the other way round. The ledger itself is on Usage.

supervisor/src/supervisor.rs
pub struct SupervisorBuilder<U: UserId> {
options: ApplyOptions,
sample_interval: Duration,
usage_sink: Option<PushUsage<U>>,
socket: SocketOptions,
}
impl<U: UserId> Default for SupervisorBuilder<U>;
impl<U: UserId> SupervisorBuilder<U> {
pub fn options(self, options: ApplyOptions) -> Self;
pub fn sample_interval(self, interval: Duration) -> Self;
pub fn usage_sink(self, sink: impl UsageSink<U>, interval: Duration) -> Self;
pub fn socket_options(self, socket: SocketOptions) -> Self;
pub async fn start(self, spec: Spec<U>)
-> Result<(Supervisor<U>, ApplyReport), ApplyError>;
}

The builder holds what outlives any one spec:

Setting Default Effect
options ApplyOptions::default() Options for the first apply. Nothing runs before it, so the first plan never contains a disruptive step, and the option has no effect on it.
sample_interval DEFAULT_SAMPLE_INTERVAL, 1 s How often the sampler publishes a StatsSnapshot and reconciles the usage ledger. start panics with the sample interval must not be zero for Duration::ZERO.
usage_sink none Hand every user’s usage to sink once per interval, and a last batch at shutdown, after the last session has ended.
socket_options SocketOptions::default() The policy every outbound socket is opened under, for the supervisor’s lifetime (see Socket policy).

usage_sink stores a boxed closure (PushUsage<U>) that start turns into a task running push_usage. The sink runs on that task, so a slow sink never stalls a connection: it only delays its next batch, which then covers the longer interval. The task works like this:

  • The first batch comes one interval after start (tokio::time::interval_at(now + interval, interval)), and a tick missed while the sink was slow is not made up in a burst (MissedTickBehavior::Delay).
  • On each tick, and once more when background_stop is cancelled, it reconciles the ledger on the blocking pool (off_runtime(move || ledger.reconcile())), so the batch holds everything moved up to that moment rather than up to the last sampler tick. A panic in the reconcile is resumed on the task (see Off the runtime).
  • It then takes the usage (UsageBook::take) and calls sink.report(&batch) only when the batch is not empty, as the UsageSink contract promises (“Never called with an empty batch”).

How a batch is formed is on Usage.

The sampler (track/sampler.rs → Sampler::run) ticks first one sample_interval after start (interval_at), with MissedTickBehavior::Delay so a tick delayed by a busy runtime is not made up in a burst. Each tick runs on the blocking pool (off_runtime, which resumes a panic), reconciles the usage ledger, and publishes its StatsSnapshot with watch::Sender::send_replace. When background_stop is cancelled it runs one final tick and returns. The tick itself is on Tracking.

start does, in order:

  1. Asserts that sample_interval is not zero.
  2. Builds an Actor with the socket policy (Actor::new), which creates the root token, the TaskTracker, the usage ledger, the session registry, the tracker and its (not yet running) sampler, the guard (Guard::default()), and the empty plane.
  3. Runs the first apply from the caller’s task: actor.apply(spec, options).await?. Its staging runs on the blocking pool. A refused spec returns its ApplyError here. The sampler, the guard sweep and the actor have not been spawned yet, and any listener the refused prepare bound was released when prepare returned its error.
  4. Spawns the sampler (tokio::spawn(sampler.run(sample_interval, background_stop))).
  5. Spawns the guard sweep (tokio::spawn(guard::sweep_until(guard, background_stop))), kept in background like the sampler.
  6. Spawns the usage push task when a sink is set, under the same background_stop token.
  7. Creates the command channel, mpsc::channel(16).
  8. Builds the handle from the actor’s plane cell, tracker and usage book.
  9. Spawns the actor’s loop, tokio::spawn(actor.run(rx)), and returns the handle with the first ApplyReport.

Listeners started by the first commit begin accepting in step 3, before the sampler runs. That is harmless: the sampler counts what flows moved whenever it first ticks.

supervisor/src/supervisor.rs
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct ApplyOptions {
pub allow_disruptive: bool,
}

The planner marks a change that ends live connections as Step::Disrupt, for example a change to a Hysteria 2 listener’s obfuscation. Without allow_disruptive the actor refuses a spec whose plan has such a step, after staging and before prepare:

inbound <tag>: <reason>; this ends its live connections and needs allow_disruptive

The actor reports only the first disruption in plan order (plan.disruptions().next()), so ApplyError::Disruptive names one inbound even when several have a Disrupt step. The staged spec and users of the refused apply are dropped on the blocking pool (drop_off_runtime((spec, users))). The reasons and the steps are on Reconcile.

etemenanki-app never sets the flag, so it refuses a TUN change or a Hysteria 2 obfuscation change with a restart hint, and restarting the app applies the change. katana always sets it, which lets a Hysteria 2 obfuscation change from the panel through. a_hysteria2_obfuscation_change_needs_allow_disruptive pins the Hysteria 2 case: refused without the flag; with it, restarted names the inbound and the port is kept.

supervisor/src/supervisor.rs
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Selector<U> {
Session(SessionId),
User(U),
Inbound(CompactString),
All,
}
Selector Closes
Session(id) That session, if it is live.
User(id) Every session bound to that user, on any inbound. A user id no inbound has admitted has no key and closes nothing. A session whose first flow has not yet said who it is belongs to no user yet.
Inbound(tag) Every session of that inbound, bound to a user or not.
All Every live session.

The actor maps the selector to a registry scope (Scope::One, Scope::User, Scope::Inbound, Scope::All), mapping a user id to its UserKey first. The count is of sessions this call cancelled: a session already closed is not counted again.

supervisor/src/supervisor.rs
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SessionInfo<U> {
pub id: SessionId,
pub inbound: CompactString,
pub source: Option<IpAddr>,
pub user: Option<U>,
pub up: u64,
pub down: u64,
pub started: Instant,
}

sessions builds these from the registry’s SessionStats, in session id order, translating each UserKey back to the front end’s U. user is None until the session’s first flow binds it to a user. It stays None for a session that never authenticates, and for a session admitted without a user, whose principal is Principal::anonymous() (for example on an inbound that names no user set). up and down are wire bytes (see Usage). started is a std::time::Instant.

Tracker::sessions returns the same registry as SessionStats (with a UserKey and the user’s label instead of U) without going through the actor. The REST API uses that path.

supervisor/src/supervisor.rs
pub async fn check<U: UserId>(spec: &Spec<U>) -> Result<(), ApplyError> {
let mut actor = Actor::new(SocketOptions::default());
let (spec, plan, users) = actor.stage(spec.clone()).await?;
let checked = actor.prepare(&plan, &spec, users, false).await.map(drop);
drop_off_runtime(spec);
checked
}

check is a dry run of a first start. It builds a throwaway Actor with SocketOptions::default(), stages a clone of the spec from the empty running state (Actor::stage: plan, then stage_users, which admits the users and builds every inbound handler, on the blocking pool), runs prepare with binding switched off, and then drops everything it built, the spec on the blocking pool.

check does check does not
Run every validation rule (plan calls validate first) Bind a TCP, UDP or Unix listener
Build the DNS resolvers, reading the hosts file if the spec asks for it Create or adopt a TUN device
Build every outbound, loading its TLS material Pair a handler with a bound socket or device (Pending::pair), so no Hysteria 2 endpoint is opened and no TUN device is prepared for a runtime
Build every balancer Start the sampler, the guard sweep or a listener, or dial anything; only staging and the final drop run as tasks, on the blocking pool
Compile the route table, reading its geo data Check for disruptive changes (nothing runs)
Admit users and build every inbound handler, parsing certificates and keys, in staging Use the host’s socket policy (it never needs one)

It refuses what a start would, short of binding and of what pairing a bound socket or device with its handler does: opening a Hysteria 2 endpoint (with its obfuscation) and preparing a TUN device for its runtime. etemenanki-app calls it through instance::check_bytes in two places: its --test flag (instance::check, over the file named by -c), and the REST API’s route switch (app/src/api.rs → set_route), which checks the edited config before writing it. Captured from the pinned binary (timestamps and colours removed):

$ etemenanki-app --test -c ok.toml
Configuration OK.
$ etemenanki-app --test -c duplicate-tag.toml
ERROR etemenanki_app: configuration invalid: duplicate outbound tag direct
$ etemenanki-app --test -c bad-ca.toml
ERROR etemenanki_app: configuration invalid: building outbound proxy@v1 failed: error:04800064:PEM routines:PEM_read_bio_ex:bad base64 decode:../crypto/pem/pem_lib.c:968:

The text after failed: is OpenSSL’s and varies with the OpenSSL version the binary links. The second error comes from validation. The third comes from building an outbound during prepare: the app read the CA file and passed its bytes in the spec, and the supervisor’s outbound builder, the same one a start uses, could not parse them. configuration invalid: is the app’s prefix; the rest is the ApplyError’s Display.

Supervisor, its builder, and every public type that names a user are generic over the front end’s user key:

supervisor/src/entity/user.rs
pub trait UserId: Clone + Eq + Ord + Hash + Debug + Send + Sync + 'static {
fn name(&self) -> UserName;
}

U keys a UserSet and names the user in every UsageDelta, UsageTotal, SessionInfo and Selector::User. name gives the UserName (an email or a username, never a secret) that logs and the protocols’ user labels carry. The data plane never sees U: the actor hands out a UserKey per admitted id and connections carry that u64 (see Entity ids). etemenanki-app and the FFI key users by UserName itself; katana keys them by the panel’s numeric id. The trait and user sets are on Users and sessions.

Both live in supervisor/src/build/apply.rs. An apply or update answers with an ApplyReport or an ApplyError; a user edit answers with () or a bool, or an ApplyError. ApplyReport lists what the apply did to each resource; every resource of the desired spec appears in reused or built. Commit fills it from the plan’s steps:

Field Filled from
reused every Step::Reuse
built every Step::Build
swapped every Step::SwapHandler
drained every Step::Drain. The drain policy runs only when the running target of that tag is the version the step names (target.id() == outbound), but the version is listed either way.
rebound a Step::Bind for an inbound whose tag had a different bind in the running spec; a Step::Bind for a new tag is reported under built only
restarted every Step::Disrupt
removed every Step::CloseSessions

The fields are explained on Reconcile.

ApplyError says why a change was not made, and a refused change leaves what runs as it was. Where each variant can arise:

Variant Display Raised in
DuplicateTag duplicate {kind} tag {tag} plan → validate
UnknownReference {from} references unknown {kind} {tag} plan → validate
EmptyBalancer balancer {balancer} has no members plan → validate
UnprobeableMember balancer {balancer}: outbound {member} has no upstream a TCP health probe can reach plan → validate
Invalid {resource}: {reason} plan → validate; a user edit’s validate_admission; a user edit naming a missing set
Disruptive inbound {inbound}: {reason}; this ends its live connections and needs allow_disruptive the actor, between staging and prepare
Build building {resource} failed: {source} prepare, which also reports a handler or user table staging could not build; a user edit building an inbound’s user table or fitting it to the listener (stage_user_edit)
Bind inbound {inbound}: binding {bind} failed: {source} prepare, only when binding
Stopped the supervisor has shut down the handle, when the actor is gone; the actor, for an update or a user edit with no running spec (unreachable after a successful start)

Every rule and its exact reason text are on Validation.

One task owns everything that runs. The handle talks to it over a channel, so applies and user changes are serialised without a lock held across an .await.

supervisor/src/supervisor.rs
type Reply<T> = oneshot::Sender<T>;
type Edit<U> = Box<dyn FnOnce(&mut Spec<U>) + Send>;
enum Command<U: UserId> {
Apply(Box<Spec<U>>, ApplyOptions, Reply<Result<ApplyReport, ApplyError>>),
Update(Edit<U>, ApplyOptions, Reply<Result<ApplyReport, ApplyError>>),
Users(CompactString, UserEdit<U>, Reply<Result<bool, ApplyError>>),
Close(Selector<U>, Reply<usize>),
Sessions(Reply<Vec<SessionInfo<U>>>),
Target(CompactString, Reply<Option<Arc<Target>>>),
Shutdown(Duration, Reply<()>),
}
enum UserEdit<U> {
Set(BTreeMap<U, UserSpec>),
Upsert(U, UserSpec),
Remove(U),
}

Apply boxes its spec (apply_with sends Box::new(spec)): a spec is several times the size of every other command. Target answers from the actor’s targets map with the current version of an outbound or balancer, whether or not a route names it; http_ping runs the test itself after the reply, so a slow site never holds the actor.

Every call that goes through the actor uses one private helper, ask:

  1. Create a oneshot channel for the reply.
  2. send(command).await on the bounded mpsc channel. With 16 commands already queued, the caller waits here for room.
  3. Await the reply.

A failure at step 2 (the receiver is gone) or step 3 (the reply sender was dropped) becomes ApplyError::Stopped. close maps that to 0, sessions to an empty Vec, http_ping to PingError::Stopped, and shutdown ignores it.

Actor::run receives one command at a time and finishes it before taking the next:

Command Handled by Awaits inside
Apply(spec, options, reply) apply staging on the blocking pool (Actor::stage → off_runtime); creating a TUN device in prepare
Update(edit, options, reply) Actor::edited clones the running spec and runs edit on it on the blocking pool, then apply; with no running spec it answers Stopped the edit; then as Apply
Users(set, edit, reply) edit_users staging the edit on the blocking pool (stage_user_edit)
Close(selector, reply) close nothing
Sessions(reply) sessions nothing
Shutdown(grace, reply) shutdown, then replies and returns the grace, the tracked tasks, the background tasks

When commands.recv() returns None, every handle has been dropped without a shutdown, and the loop runs shutdown(Duration::ZERO) before it returns.

Consequences of the single task:

  • An apply and a user edit never interleave. No two applies prepare at once, so two prepares can never bind the same port against each other.
  • update’s edit runs on the blocking pool against the spec the actor holds, while the actor waits for it and takes no other command, so nothing can land between reading the running spec and applying the edited one.
  • close and sessions wait behind a running apply. The awaits inside an apply that can take time are the staging on the blocking pool and, in prepare, creating a TUN device (BindSpec::Tun(TunSource::Create(..)) → tun::open(spec).await). Planning, admitting users, building handlers and user tables, and working out the user limits run off the runtime (see Off the runtime); the DNS, outbound, balancer and route builders, adopting a supplied descriptor and the other binds run synchronously on the actor task.
  • The actor holds no lock of its own. Its state is owned by the task and mutated through &mut self; staging borrows it by moving it to the blocking pool and back. The locks it touches are held for synchronous sections only: the session registry’s mutex, the usage book’s RwLock, the tracker’s pacer map and the guard’s ban table (all parking_lot locks), a stream listener’s watch channel (send_replace, borrow), and the Hysteria 2 inbound’s endpoint lock, which set_quic and set_obfs take.

supervisor/src/system/blocking.rs keeps work that grows with what a node holds (its users, its sessions) off the runtime’s async workers. Its module doc gives the reason: a worker busy for long can leave the I/O driver unpolled, so every connection in the process waits, not just the caller.

supervisor/src/system/blocking.rs
pub(crate) async fn off_runtime<T, F>(work: F) -> T
where
F: FnOnce() -> T + Send + 'static,
T: Send + 'static;
pub(crate) fn drop_off_runtime<T: Send + 'static>(value: T);
  • off_runtime runs work on the blocking pool (spawn_blocking) and returns what it returns. A panic in it is resumed on the caller (std::panic::resume_unwind). Work the runtime cancels while shutting down never returns: the runtime drops the caller as well.
  • drop_off_runtime drops value on the blocking pool and does not wait for it, since what an apply replaces can hold every user of a node.

The supervisor uses them for the work whose cost grows with the users (the module doc of supervisor.rs lists it) and for the sampler and usage ticks:

Work How
Planning an apply and staging its users: admitting them, building handlers and user tables, working out the limits Actor::stage → off_runtime, with the running state lent (lend) and taken back (take_back)
Cloning and editing the spec for an update Actor::edited → off_runtime
Staging a user edit edit_users → off_runtime, running stage_user_edit with the running state lent
Dropping the user keys, published limits, admissions and spec an apply or edit replaced, and the staged spec and users of a refused apply drop_off_runtime
A sampler tick, the usage push task’s reconcile off_runtime

What staging needs from a running listener is taken at lend as a ListenerView: its bind, its circuit budget, and the UserTarget a user table is prepared against. A stream listener’s handler changes only at an apply’s commit, so the handler taken there is the one the table is stored into. The actor runs one command at a time, so nothing else reads the lent state meanwhile.

Reads that must stay cheap never queue behind an apply:

Read Reads Why it is safe without the actor
epoch() the plane cell, ArcSwap::load The plane is published with one atomic store.
tracker() the shared Tracker Flows register and deregister themselves; the registry is a concurrent map.
close_rerouted() the plane cell and the Tracker One load() of the plane, then kill_where over the registry; a kill is a flag and a wake.
take_usage(), usage_snapshot() the usage book The ledger has its own locks: one mutex over its account maps (Ledger::inner, holding accounts and active) and one mutex per account. The key-to-id names are appended by the actor under a RwLock.

All of them keep working after shutdown; close_rerouted then finds no flow and returns 0.

supervisor/src/supervisor.rs
struct Actor<U: UserId> {
state: RunningState<U>,
shared: Shared,
root: CancellationToken,
dns: Option<Dns>,
dns_target: Option<Arc<Target>>,
targets: HashMap<CompactString, Arc<Target>>,
probes: HashMap<CompactString, CancellationToken>,
routes: Option<Arc<CompiledRoutes>>,
listeners: Vec<Listener>,
admissions: HashMap<CompactString, Admissions<U>>,
keys: UserKeys<U>,
published_limits: HashMap<UserKey, NonZeroU64>,
usage: Arc<UsageBook<U>>,
sampler: Option<Sampler>,
background: Vec<JoinHandle<()>>,
background_stop: CancellationToken,
epoch: u64,
socket: SocketOptions,
}
Field Holds
state The spec last applied, and the latest version of every outbound and balancer tag, removed tags included (RunningState).
shared What every accept loop shares (serve.rs → Shared): the plane cell, the session registry, the flow Tracker, the TaskTracker, and the guard (Arc<Guard>).
root The root cancellation token.
dns, dns_target The resolvers built from the running DnsSpec, and the target the DNS service is reached through when the supervisor answers DNS itself.
targets The current version of each outbound and balancer tag, as Arc<Target>.
probes The probe token of each running balancer, by tag.
routes The compiled route table.
listeners Every bound listener with its serving task.
admissions Who each inbound admits, by inbound tag: user id → principal and credential.
keys The UserKey handed out for each admitted user id (build/users.rs → UserKeys).
published_limits The speed limits last published to the tracker, by key: what the next publish’s changes are worked out against.
usage The usage book shared with every handle.
sampler The sampler, until start spawns it.
background, background_stop The sampler, guard sweep and usage push tasks, and the token that stops them.
epoch Planes published so far.
socket The socket policy every outbound socket is opened under.

The usage book is the one piece of actor-built state a handle reads directly:

supervisor/src/supervisor.rs
struct UsageBook<U> {
ledger: Arc<Ledger>,
names: RwLock<Vec<U>>,
}

names lists every id a key was handed out for, in key order; the actor appends the ids of new keys as it commits them (commit_keys). take and totals translate each ledger key back to U. A key with no name would mean a session bound a key before it was published, and taken bytes cannot be put back, so UsageBook::name panics with {key:?} was bound before it was published rather than drop the delta.

sequenceDiagram
  participant FE as Front end
  participant H as Supervisor handle
  participant A as Actor task
  participant R as What runs
  FE->>H: apply_with(spec, options)
  H->>A: Command::Apply on the channel
  A->>A: stage on the blocking pool: plan (validating first), then the users
  alt the spec breaks a rule
    A-->>H: Err(DuplicateTag, UnknownReference, EmptyBalancer, UnprobeableMember or Invalid)
  else a Disrupt step without allow_disruptive
    A-->>H: Err(Disruptive), what staging built dropped
  else valid, and no Disrupt step or the flag is set
    A->>A: prepare: build and bind, nothing that runs changes
    alt prepare fails
      A-->>H: Err(Build or Bind), what staging and prepare built dropped
    else prepare succeeds
      A->>R: commit: keys, limits, guard policy, plane, revocations, listeners, drains
      A-->>H: Ok(ApplyReport)
    end
  end
  H-->>FE: the result
  • Plan and staging (Actor::stage). The actor lends its running state (lend: the running spec and versions, the admissions, the user keys, the published limits, and a ListenerView of each listener) to the blocking pool (off_runtime), and takes it back afterwards (take_back). There, plan (topology/spec_plan/plan.rs) validates the spec and compares it with RunningState; plan is pure: no sockets, no files, no clock. Then stage_users:

    1. admits each inbound’s users with admit on a staged copy of the user keys (lent.keys.clone()), collecting the principals no longer admitted together with the inbound’s removal policy;
    2. for an inbound the plan builds, builds its handler (build_handler). A Hysteria 2 handler rebuilt on a running listener keeps that listener’s circuit semaphore when max_circuits did not change (same_circuits, with the circuits from the ListenerView);
    3. for an inbound the plan keeps, whose listener is running, builds a new user table (user_table, then UserTarget::prepare) when its admissions differ from the running ones: another user, a replaced principal (Arc::ptr_eq) or another credential (same_admissions);
    4. works out every user’s limits (user_limits, see Speed and device limits).

    A handler or table that fails to build is kept as an error in the UserStage, to be reported by prepare.

  • Disruption check (Actor::apply): without allow_disruptive, the first Step::Disrupt refuses the spec as ApplyError::Disruptive, and the staged spec and users are dropped with drop_off_runtime.

  • Prepare (Actor::prepare, given the UserStage) does everything else that can fail, and changes nothing that runs: no listener starts, no plane is published, no user table, key or limit changes. In order:

    1. DNS: the running resolvers are kept unless the plan has Step::Build(Resource::Dns). Rebuilding them (build_dns) also builds the DNS service’s target, Target::outbound(internal_id(self.epoch + 1), Outbound::Dns(service)), when the spec has the supervisor answer DNS itself; otherwise the running dns_target is kept.
    2. Outbounds: build_outbound for each Step::Build, the running Arc<Target> for each Step::Reuse.
    3. Balancers: for each Step::Build, a Member per member outbound with its probe target, Balancer::new, and a Target::balancer under the planned version; the new balancer is kept with its probe_interval and probe_timeout for commit. A Step::Reuse keeps the running target.
    4. The route table: compiled again (compile_routes) only on Step::Build(Resource::Route).
    5. Inbounds, each in turn, in spec order. A staged error is returned here, as if built now. A staged handler is prepared for the running listener on its bind (prepare_swap), or, with no listener on that bind, a new one is bound (listener::bind) and paired with it and the inbound’s admission caps (Pending::pair, with the caps of a listener the inbound keeps, or new ones); check skips this last step. A staged user table is queued for its listener.

    Prepare returns a Prepared value (below). When a step fails, prepare returns its error, and what it built so far is dropped with it, listeners it bound included (a Unix or handoff listener’s staged socket file too; nothing at a socket’s path is touched, so a running listener’s file stays in place); Actor::apply drops the spec with drop_off_runtime.

  • Commit (Actor::commit) cannot fail. It adopts the user keys (commit_keys), publishes the limits (publish_limits), hands the spec’s policies to the guard (self.shared.guard.set_policy(&spec.policies)), and replaces the targets. When the plan has Step::PublishPlane, it publishes the plane (incrementing epoch), cancels the probe tokens of balancers the plan does not reuse, and starts the probes of new balancers under a child of the root, resolving with the new resolvers’ servers (dns.servers) and dialing with TcpDialer::new(self.socket.clone()); without a publish, probe tokens are left alone. It stores the route table, the resolvers and the DNS service target either way. It then revokes removed principals, starts new listeners (each with root.child_token() as its stop token, a Unix or handoff socket first moved from its staging name over its path), swaps handlers, stores user tables, and walks the steps: Step::StopAccepting stops the listener on that bind, Step::CloseSessions closes the inbound’s sessions, and Step::Drain hands the old version to its drain policy. It ends by storing the admissions and the running state, dropping the ones they replace with drop_off_runtime, and logs applied: {} built, {} reused, {} swapped, {} drained at debug level.

Every step, in order, is on Reconcile.

supervisor/src/supervisor.rs
struct Prepared<U: UserId> {
dns: Dns,
dns_target: Option<Arc<Target>>,
targets: HashMap<CompactString, Arc<Target>>,
new_balancers: Vec<(CompactString, Arc<Balancer>, Duration, Duration)>,
routes: Arc<CompiledRoutes>,
admissions: HashMap<CompactString, Admissions<U>>,
keys: UserKeys<U>,
revoked: Vec<(Vec<Arc<Principal>>, UserRemovalPolicy)>,
limits: UserLimits,
started: Vec<(BindSpec, Pending)>,
swaps: Vec<(usize, Swap)>,
users: Vec<(usize, Users)>,
}
Field Holds
dns, dns_target The resolvers and the DNS service target, rebuilt or carried over
targets Every outbound and balancer tag of the spec, by tag
new_balancers Each built balancer with its tag, probe_interval and probe_timeout, whose probes commit starts
routes The route table, compiled or carried over
admissions, keys Who each inbound admits, and the staged user keys, from staging
revoked Per inbound, the principals no longer admitted and the removal policy that applies to them
limits Every user’s speed and device limits, and the speed changes against published_limits (UserLimits), from staging
started New binds, each paired with its handler (Pending)
swaps Handlers prepared for running listeners, by listener index
users User tables prepared for running listeners, by listener index

A Prepared that is dropped without a commit, as check does with map(drop), releases what it holds the same way.

user_limits(spec, &keys, &lent.limits) ends the staging of an apply (stage_users) and of a user edit (stage_user_edit), on the blocking pool. It walks every user of every set in the spec and collects each user’s speed_limit and device_limit by UserKey. A user in several sets gets the smallest of each, and a user with no key (one no inbound has admitted) is skipped. It also lists the speed changes against published_limits: Some(rate) sets a limit and None lifts one; a limit equal to the published one is not listed (speed_limits_list_only_what_changed, speed_limits_of_an_unchanged_set_change_nothing).

Actor::publish_limits runs at the start of every commit and every user edit’s commit, never in prepare, so a refused change leaves live limits alone. It:

  1. hands the speed changes to Tracker::change_speed_limits, which sets the rate of each listed user’s pacer (creating one for a new limit) and leaves every other pacer as it is, so live flows feel a change on their next poll;
  2. replaces every device limit in the session registry (Sessions::set_device_limits). A lowered limit closes no session; it holds from the next session that binds;
  3. keeps the speed limits as the new published_limits, dropping the old map with drop_off_runtime.

An apply’s commit then hands spec.policies to the guard (Guard::set_policy); a user edit changes no policy. The pacers are on Tracking, the device limit on Users and sessions.

set_users, upsert_user and remove_user do not plan. Actor::edit_users lends the running state to the blocking pool, where stage_user_edit changes one user set in a copy of the running spec and builds only the user tables of the inbounds admitting that set; the actor then takes the state back and commits:

  1. Take the running spec (with none, the edit fails as ApplyError::Stopped) and find the set. A missing set is ApplyError::Invalid for Resource::UserSet, shown as user set {tag}: no such user set.

  2. Apply the edit. Removing a user who is not in the set returns Ok(false) and changes nothing. Set and Upsert always count as a change.

  3. Stage, for each inbound whose users names the set:

    • check the set against the inbound with validate_admission, the only validation rule a user edit runs;
    • admit its users on a staged copy of the keys (lent.keys.clone());
    • find the inbound’s listener by bind, with expect("every running inbound has its listener");
    • build the user table (user_table) and check that it is for the protocol the listener’s current handler serves (UserTarget::prepare → UserTable::fits, against the UserTarget taken by Listener::user_target at lend). A mismatch fails the edit as ApplyError::Build with the handler is not of the kind its listener serves (io::ErrorKind::InvalidInput).

    The first failure refuses the whole edit, and no table is stored. Staging ends with user_limits.

  4. Commit: adopt the keys (commit_keys), publish the limits (publish_limits), revoke the principals that are gone under each inbound’s removal policy, then store each table and its admissions, and keep the edited spec as the running one (state.spec.replace). The admissions and the spec it replaces are dropped with drop_off_runtime.

A user edit never publishes a plane, so epoch does not move. As in an apply, the principals of removed users are revoked before any new user table is stored, and a session whose first flow presents a revoked principal is closed under every policy (Session::admit). The details are on Users and sessions.

flowchart TB
  root["root token (Actor::new)"]
  stop["listener stop token, one per listener"]
  tun["TUN runtime token, one per runtime"]
  probe["probe token, one per new balancer"]
  session["session token, one per session"]
  bg["background_stop (independent)"]
  root --> stop
  stop --> tun
  root --> probe
  root --> session
Token Created by Parent Cancelled by Stops
root Actor::new none shutdown, once the grace is over Everything below it; drain grace timers; a Hysteria 2 endpoint still draining
Listener stop commit: root.child_token() per started listener root Step::StopAccepting, shutdown, or the root The accept loop; Sessions::open refuses new sessions for it; the TUN runtime under it
TUN runtime token run_tun_inbound: stop.child_token() per runtime its listener’s stop A restart sent by Listener::swap, its listener’s stop, or its listener handle being dropped (the restart channel closes) One TUN runtime and every flow it serves
Probe token commit: root.child_token() per newly built balancer root A publish that drops or rebuilds the balancer, shutdown, or the root That balancer’s probe tasks
Session token Sessions::open: root.child_token() root close, a removal policy, Step::CloseSessions, Session::admit refusing a revoked principal or a session over its user’s device limit, a gRPC carrier’s Carrier once carrier_max_failures of its streams in a row have failed (guard.rs → Report::failed → Carrier::close, on the carrier’s own token), the session’s own end (Session::drop), or the root On a stream listener, the tasks serving that connection; a Hysteria 2 connection, which the protocol closes; a removal grace timer waiting on it
background_stop Actor::new: CancellationToken::new() none shutdown, after every tracked task has ended The sampler, the guard sweep and the usage push task

Session tokens hang off the root, not off the listener that accepted the connection. That is what lets an apply stop, rebind or re-handle a listener without touching its connections: stopping a listener stops accepting and nothing else. What does end sessions is listed under What stays live across an apply.

A session ends when the last Arc<Session> drops. Session::drop cancels its token, which ends a removal grace timer still waiting on it; closes the session in its user’s ledger account, folding in the bytes not yet counted (Account::close); and removes it from the registry. Session ids count up from 1 (Sessions::next, AtomicU64::new(1)).

background_stop is deliberately not a child of the root. The sampler and the usage sink must outlive every connection so they count its last bytes, and cancelling the root is what ends the connections.

A drain uses no token. An outbound version’s flows are found in the tracker, where each flow is filed under the OutboundId of the version that carries it, and killed there (see Connections ended from outside).

The supervisor runs its tasks in three places: plain tokio::spawn, its one TaskTracker, and the blocking pool.

Task Spawned with Spawned by Ends when
The actor tokio::spawn SupervisorBuilder::start Shutdown has been handled, or every handle is gone
The sampler tokio::spawn, kept in background SupervisorBuilder::start background_stop, after one last tick
The guard sweep (guard::sweep_until) tokio::spawn, kept in background SupervisorBuilder::start background_stop. It calls Guard::sweep every SWEEP_INTERVAL (30 s), the first time 30 s after start, with MissedTickBehavior::Delay.
The usage push tokio::spawn, kept in background SupervisorBuilder::start, with a sink background_stop, after one last reconcile and take, reported if not empty
A stream accept loop TaskTracker::spawn Listener::start its listener’s stop. The select! is biased toward the stop, and the loop also leaves when Sessions::open finds the stop cancelled.
A stream listener’s hot-tier refresher (serve::refresh_hot_tiers) TaskTracker::spawn the stream accept loop, as it starts its listener’s stop. Every HOT_TIER_POLL (1 s) it refreshes a VMess handler’s hot tier when the validator says it is due, preparing it with off_runtime.
A Hysteria 2 listener TaskTracker::spawn Listener::start its stop and its last connection, or the root
A TUN runtime loop TaskTracker::spawn Listener::start, and a swap whose runtime already ended its listener’s stop, the listener handle being dropped, or its runtime ending on its own
One accepted socket TaskTracker::spawn under the socket’s session token (Shared::spawn_until → until_cancelled) the stream accept loop its transport ends, or its session token is cancelled. It runs the transport; a Unix socket’s connection is served on this task, and a handoff socket’s control connection is read on it before the TCP socket it carries is served.
One byte stream a TCP transport yields Shared::spawn_until (until_cancelled) under its session’s token the socket’s task, from the transport’s accept callback the stream’s runtime ends, or its session token is cancelled. The session is the socket’s own, or, on a gRPC carrier, one opened for the stream.
One Hysteria 2 connection a JoinSet inside its listener’s task (Hy2Inbound::run) the Hysteria 2 listener the connection ends, or the protocol closes it when its session token is cancelled
A balancer probe, one per member TaskTracker::spawn Balancer::spawn_probe, from commit its probe token
A removal grace timer TaskTracker::spawn Sessions::revoke under CloseAfter the grace passes, or the session token is cancelled
A drain grace timer TaskTracker::spawn drain under CloseAfter the grace passes, or the root is cancelled
A sampler tick, a ledger reconcile, an apply’s or user edit’s staging, an update’s edit, a VMess hot-tier prepare off_runtime (spawn_blocking, awaited) the sampler, the usage push, the actor, a hot-tier refresher the work is done
Dropping what an apply or user edit replaced, or a refused apply’s staged spec drop_off_runtime (spawn_blocking, not awaited) the actor, check the drop is done

There is one TaskTracker, created in Actor::new and shared by Shared and the session registry, so shutdown can wait for every accept loop, connection, probe and timer with one wait; a Hysteria 2 listener’s task waits for its own connections. Shared::spawn_until wraps each socket and each stream as

supervisor/src/serve.rs
self.tracker.spawn(until_cancelled(token, fut));
fn until_cancelled<F>(token: CancellationToken, fut: F) -> impl Future<Output = Option<()>>
where
F: Future<Output = ()>,
{
fut.with_cancellation_token_owned(token)
}

so cancelling a session token ends the task and drops the future, and its sockets with it; a future spawned under a token that is already cancelled never starts. The doc comment of until_cancelled says why it is a combinator rather than an async block over a select!: the block would keep both the future it captured and the copy it moves into the select!, making the task twice the size of the connection’s future. On a gRPC carrier each HTTP/2 stream is a session of its own. A Hysteria 2 connection that finishes its QUIC handshake after its listener’s stop was cancelled gets no session: run_hysteria_inbound hands it a token that is already cancelled, so no session escapes the close of a removed inbound. The accept loops and what they serve are on Serving.

supervisor/src/supervisor.rs
pub fn socket_options(self, socket: SocketOptions) -> Self;

SocketOptions (environment/src/dial/socket.rs) is the policy applied to every socket a dialer opens: a source bind_address (applied only to sockets of its own family), an interface, a packet mark (SO_MARK), a tcp_keepalive idle time, and a hook run on each socket after it is created and before it is bound or connected. The fields and platform support are on Dialers.

The builder stores the policy in the actor, which passes it to every builder that opens an outbound socket:

Socket Receives the policy through
Every dial of a proxy outbound, over TCP and its transports build/outbound.rs → Dialer::new(socket)
A REALITY inbound’s dial of its cover site build/inbound.rs → build_reality → Dialer::new(socket), passed down from stage_users through build_handler
A direct (freedom) flow’s TCP dial and UDP sockets build/outbound.rs → Dialer::new(socket), whose TCP and UDP halves both carry it
A SOCKS outbound’s UDP relay socket build/outbound.rs → UdpDialer::new(socket)
A Hysteria 2 outbound’s QUIC socket and, with port hopping, the socket of every hop Hy2Config::socket (protocols/src/hysteria/connection.rs → bind_socket)
A WireGuard outbound’s tunnel socket WgConfig::socket
Every query a resolver sends from this host build/dns.rs → ResolverOptions::socket
Balancer health probes commit → TcpDialer::new(self.socket.clone())
The UDP socket a TUN inbound’s IPv6 reachability question is asked through, under a direct outbound’s policy topology/outbound/freedom.rs → lacks_ipv6 → UdpDialer::source_toward (environment/src/dial/udp.rs)

The policy holds for the supervisor’s lifetime; an apply cannot change it. It does not reach:

  • listener sockets, which system::listener::bind opens with the standard library: the policy is for traffic this process originates;
  • the system resolver. getaddrinfo opens its sockets inside the C library, where no policy reaches them. A resolver whose backend is the system resolver (for example DnsSpec::Single(Backend::System)) uses it, and so does a REALITY inbound to look up its cover site.

This is where a mobile VPN keeps the proxy’s own traffic out of its tunnel. etemenanki-ffi passes a hook that calls the app’s Platform::protect(fd) (VpnService.protect on Android); a socket the platform refuses fails with the platform refused to protect the socket, and the dial it was for fails with it. A hook error of any kind fails the dial: nothing leaves on a socket the policy did not accept.

check builds with SocketOptions::default(): it never dials, so it needs no policy.

supervisor/src/supervisor.rs
pub async fn shutdown(&self, grace: Duration);

shutdown sends Command::Shutdown(grace, reply), and the actor runs Actor::shutdown:

  1. Cancel every listener’s stop token. Accept loops return and release their sockets (a Unix listener removes its socket file); Sessions::open refuses any session for them. A Hysteria 2 listener stops admitting and drains. A TUN runtime’s token is a child of its listener’s stop, so every TUN runtime and the flows it serves end here, before the grace: TUN flows are not sessions.
  2. Cancel every balancer’s probe token.
  3. Close the TaskTracker, so wait can finish once it is empty.
  4. Wait up to grace for every tracked task to end on its own: tokio::time::timeout(grace, tracker.wait()).
  5. Cancel the root. Every session token is a child, so every task still serving a stream connection ends at its next poll and the protocol closes every Hysteria 2 connection still open; removal and drain grace timers end; a Hysteria 2 listener closes its endpoint under the draining run, so a client still in its QUIC handshake (which has no session yet) ends too.
  6. Wait for every tracked task to end.
  7. Drop every Listener handle.
  8. Cancel background_stop and await every background task: the sampler, the guard sweep and the usage push task. Every connection has ended and folded its last bytes in, so the sampler runs its last tick, and the push task reconciles and takes once more and reports that batch if it is not empty. The guard sweep returns at once.

The actor then replies and returns, dropping the command receiver. grace is how long live connections may take to finish; Duration::ZERO closes them at once.

When every handle is dropped without a shutdown, the loop runs the same sequence with Duration::ZERO. A Tracker clone taken with tracker() does not keep the actor alive.

After shutdown:

Call Result
apply, apply_with, update, update_with Err(ApplyError::Stopped): the supervisor has shut down
set_users, upsert_user, remove_user Err(ApplyError::Stopped)
close 0
sessions an empty Vec
shutdown returns at once
epoch the last published epoch
tracker still readable; the last StatsSnapshot is the final tick’s
take_usage, usage_snapshot still work: what no sink took can still be taken

A command queued behind Shutdown gets Stopped too: its reply sender is dropped with the channel.

supervisor/src/entity/id.rs names what runs by value instead of by pointer. A fan-out UDP link keys each sub-link by the OutboundId it was opened on rather than by the address of an Arc, because outbounds outlive a single apply, one tag can have an old version draining beside a new one, and a freed allocation can be reused by its successor.

supervisor/src/entity/id.rs
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct OutboundId {
pub tag: CompactString,
pub version: u64,
}
pub enum ResourceKind { Inbound, Outbound, Balancer, UserSet }
pub enum Resource {
Inbound(CompactString),
Outbound(OutboundId),
Balancer(CompactString),
UserSet(CompactString),
Route,
Dns,
}
pub struct SessionId(u64);
pub struct UserKey(u64);
pub struct FlowId(u64);
pub struct RuleId(u32);
Id Names Assigned by First value Display
OutboundId One build of one outbound or balancer tag plan: 1 for a tag it has no version for, the previous version plus one on each rebuild 1 {tag}@v{version}
ResourceKind A kind of tagged resource, in DuplicateTag and UnknownReference — — inbound, outbound, balancer, user set
Resource One resource, in a Plan, an ApplyReport and an ApplyError — — inbound {tag}, outbound {tag}@v{n}, balancer {tag}, user set {tag}, route, dns
SessionId One accepted connection: a socket, a QUIC connection, or one stream of a gRPC carrier Sessions::open, an AtomicU64 counter 1 session#{n}
UserKey One user id an inbound has admitted UserKeys::key, called by admit the first time an inbound admits that id 1 none
FlowId One routed flow: a stream, or one sub-link of a UDP association the Tracker, an AtomicU64 counter 1 flow#{n}
RuleId The index of a rule in RouteSpec::rules of the plane that routed a flow CompiledRoutes, per rule 0 none
  • Versions never go back. RunningState::versions keeps the version of a removed tag, so a tag added back gets a version its old build, which may still be draining, never had. Balancers are versioned in the same map, and a balancer target carries an OutboundId too, although Resource::Balancer holds only the tag.
  • The empty tag is the supervisor’s own. Validation refuses an empty tag of any kind, so the supervisor uses it for the targets it makes itself (internal_id(version) in supervisor.rs builds an OutboundId with the empty tag): the empty plane’s blackhole (version 0, before the first apply) and the DNS service target. That target is built in prepare only when the DNS is rebuilt, with the epoch of the plane about to carry it (internal_id(epoch + 1)); otherwise the running one is kept. The sampler reports the DNS service under the empty outbound tag.
  • Session and flow ids are unique for the supervisor’s lifetime, never reused. SessionId, UserKey, FlowId and RuleId expose new(raw) and get() as const fn, so a front end can hand an id out and take it back, as the FFI does with FlowId::new(id) for close_connection.
  • A user’s key stays assigned after the user is removed, so sessions a Keep policy spared can still be found by Selector::User, and their last usage is still reported under the user’s id. A key is handed out only when an inbound admits the user, that is, when the user has the credential kind the inbound’s protocol takes; a user no inbound admits has no key. Key n names the nth id handed out (id_of: ids[n - 1]); UserKey(0) names nobody.
  • A rule id belongs to one plane. A flow’s rule (Routed::rule, FlowEntry::rule) is None for the default route and for a flow intercepted for the DNS service. The router turns the index into a u32 with expect("fewer than 2^32 rules").
etemenanki-app etemenanki-ffi katana
Code app/src/instance.rs → Core, Instance ffi/src/proxy.rs → Proxy, running the app’s Core src/manager/node.rs → NodeManager
U UserName (app/src/lower.rs → UserKey) UserName, through the app’s lowering Uid(i64), the panel’s user id; name() is UserName::Username of the number
Supervisors One per process One per started proxy One per node
Start Supervisor::builder() with every default Supervisor::builder().socket_options(...) with the protect hook Supervisor::start(spec) with every default
Changes apply(spec) on each reload apply(spec) through the app’s Core::reload_with apply_with(spec, ApplyOptions { allow_disruptive: true }), with a spec lowered from the panel’s answers
Reads tracker() for the REST API; close_rerouted() after a switch with close, and http_ping() for its delay endpoint tracker(): stats(), flows(), kill(FlowId); close_rerouted() and http_ping() take_usage() for traffic reports; tracker().subscribe() and sessions() for the client addresses a helium node reports
Shutdown shutdown(SHUTDOWN_GRACE), 5 s, on a signal shutdown(STOP_GRACE), 2 s, in Proxy::stop; then RUNTIME_GRACE, 3 s, for the runtime’s threads shutdown(Duration::ZERO) when the node stops, then one last take_usage for its final report
  • etemenanki-app lowers the TOML config and its subscribe file into a spec (lower), runs check behind --test and before the REST API’s route switch writes the edited config, and applies each reload with default options. It logs config loaded: {summary} after the first apply and config reloaded: {summary} after each applied reload, where the summary lists each non-empty group of the ApplyReport (built [...]; swapped [...], and so on) or reads nothing to run. A disruptive change is refused and logged as reload refused, keeping the running config: inbound {inbound}: {reason}, which would end its live connections; restart to apply it. Its lowering is described on etemenanki-app: from TOML to a spec, and its reload on etemenanki-app: running, reloading and shutting down. When the configured REST API cannot bind at start, the binary shuts the supervisor down with Duration::ZERO and exits.
  • etemenanki-ffi runs the app’s Core with a lowering of its own (ffi/src/client.rs → lowering). It refuses an inbound that serves a proxy protocol to others: every server protocol, and a SOCKS or HTTP inbound that is not on a loopback address or a Unix socket (ServerInbound). It refuses more than one TUN inbound, binds the TUN inbound to the descriptor the phone supplies as a BindSpec::Tun(TunSource::Fd(...)), and adds one tagged tun (TUN_TAG) with the protocol’s defaults (DEFAULT_MTU, DEFAULT_UDP_IDLE_TIMEOUT, DEFAULT_MAX_FLOWS, sniffing on) when the config has none. It does not serve the config’s [api]. Its socket policy is the one on this page. It is described on Mobile library (etemenanki-ffi).
  • katana runs one supervisor per node and applies a spec lowered from the panel’s node and user list (Lowering a node into a spec), with allow_disruptive set. The node is helium-node-api’s node config, the contract katana reads every panel’s node as (Node in src/api/mod.rs). katana then calls close(Selector::All) when the node differs from the one last applied in more than the supervisor applies to new connections alone (src/manager/node.rs → disruptive_part, which leaves out an AnyTLS padding scheme and a Hysteria 2 node’s rates and UDP idle timeout, so the port, transport, security and its keys, cipher, flow, sss chains, obfuscation, masquerade and whether a Hysteria 2 node relays UDP all count), or when a local config edit the panel’s answer cannot show asked for it. Its lowering sets Policies::ip_ban and Policies::carrier_max_failures from [node.controller] (src/lower/mod.rs → policies), the ban only under enable_device_limit, and each user’s device_limit from the panel under the same switch. On a WebSocket node it sets the inbound’s real_ip from real_ip_header and real_ip_from. With handoff_dir set, every stream node’s inbound gets a handoff socket, <handoff_dir>/<port>.sock (or <handoff_dir>/<listen_ip>-<port>.sock when listen_ip is one address rather than all), in the group handoff_group names. Its flow-event watcher, spawn_client_ips, subscribes with tracker().subscribe() and, on a helium node under enable_device_limit, records each user’s client address from FlowEvent::Opened. See Node manager and Traffic accounting.

The crate follows a few rules that the rest of this section relies on. The general principles are on Design principles.

  • One owner per rule. Every rule about how the typed pieces of a spec fit together lives in build/validate.rs. A front end owns what only it can know: parsing its strings, reading its files, the defaults of its schema, how it names users, and key combinations a spec could not express. As app/src/lower.rs puts it, a rule checked in a front end as well would be a second copy that drifts, and a rule checked only there would be missing for every other front end.
  • A typed spec compared with ==. A Spec holds typed values (a Destination, a Uuid, decoded key bytes, PEM bytes) rather than raw config text. That lets the planner decide what to keep and what to rebuild by equality, without knowing where either spec came from.
  • Prepare, then commit. Everything fallible happens before anything that runs changes, so a refused spec leaves the running state exactly as it was.
  • Read-copy-update routing. Routes and outbounds are published together with one atomic store, so a rule can never name a tag the plane lacks, and an old plane is freed when the last flow holding one of its pieces ends.
  • Identities instead of pointers. Outbound versions, sessions, users, flows and rules are named by value (see Entity ids).
  • Nothing that grows with the users runs on a runtime worker. Planning, admitting users, building handlers and user tables, working out the limits, and dropping what an apply replaced run on the blocking pool (see Off the runtime), so a change to a large user set does not stall the connections served on the same worker. Commit is left with pointer swaps.
  • A slow observer does not hold up the data plane. Each flow’s counters are its own; the sampler aggregates them once per tick on the blocking pool; flow events go out on a bounded broadcast channel (EVENT_CAPACITY, 1024), where a subscriber that falls behind loses events rather than holding up the sender; a usage sink runs on its own task. Nothing per byte goes through the actor.
Invariant Enforced by Pinned by
A refused spec changes nothing: listeners, the plane and users stay as they were Prepare changes nothing that runs; commit cannot fail; listeners bound by a refused prepare are released when it returns its error a_refused_spec_changes_nothing (a validation refusal and a failed bind)
Applies and user edits never run concurrently One actor task, one command at a time none directly
A connection survives an apply that keeps its inbound Session tokens are children of the root; an unchanged bind keeps its listener an_established_connection_survives_an_apply_that_keeps_its_inbound
A route change reaches the next flow, mux sub-flow and UDP packet, and does not re-route an open TCP flow The connector loads the plane once per connect; the fan-out routes per packet a_route_change_reaches_the_next_udp_packet_but_not_an_established_tcp_flow, a_route_change_reaches_the_next_mux_sub_flow_of_a_live_session
An apply closes no flow because its route changed; close_rerouted closes only the flows the current plane sends to another tag close_rerouted is a separate call; Plane::reroutes close_rerouted_closes_only_the_flows_a_route_change_moves
epoch counts published planes; a refused apply publishes none epoch += 1 only at Step::PublishPlane, in commit a_route_change_reaches_the_next_udp_packet_but_not_an_established_tcp_flow (epoch + 1), a_refused_spec_changes_nothing (unchanged)
The plane before the first apply drops everything Plane::empty, epoch 0 the_empty_plane_drops_everything
An outbound whose spec and resolvers are unchanged is carried over; a rebuilt one is a new version Step::Reuse inserts the running Arc<Target>; Step::Build takes the next version; a changed DnsSpec rebuilds every non-blackhole outbound an_unchanged_hysteria2_outbound_keeps_its_quic_connection_across_apply, each_rebuild_of_a_tag_takes_the_next_version, a_tag_added_back_takes_a_version_it_never_had
A Step::Disrupt change is refused without allow_disruptive Plan::disruptions checked after staging, before prepare a_hysteria2_obfuscation_change_needs_allow_disruptive
A protocol change on the same bind keeps the socket Listener keyed by BindSpec; handler swap a_protocol_change_on_the_same_port_keeps_the_socket_and_old_connections
Every session is a child of the root Sessions::open → root.child_token() cancelling_the_root_cancels_every_session
A stopped listener opens no session Sessions::open checks stop under the registry lock a_stopped_listener_opens_no_session
Removing an inbound closes its sessions Step::StopAccepting, then Step::CloseSessions removing_an_inbound_closes_its_sessions
close counts only what it closed Sessions::close skips cancelled tokens a_session_already_closed_is_not_counted_again
Shutdown does not wait out a grace timer Timers run on the TaskTracker; removal timers select on the session token and drain timers on the root, and shutdown cancels the root shutdown_ends_pending_grace_timers
Shutdown does not wait for a stalled Hysteria 2 handshake run_hysteria_inbound closes the endpoint once the root is cancelled shutdown_does_not_wait_out_a_stalled_hysteria2_handshake
The last usage batch is taken after the last session ended background_stop is cancelled after tracker.wait() a_usage_sink_gets_a_batch_per_interval_and_the_last_at_shutdown
A slow sink never stalls a connection The sink runs on its own task a_slow_usage_sink_does_not_stall_the_data_plane
Every outbound socket is opened under the socket policy The actor passes SocketOptions to every builder and to the probes direct_flows_are_dialed_on_hooked_sockets, a_socks_upstream_is_dialed_on_hooked_sockets, a_refusing_hook_fails_the_dial
A user key is published before any session can bind it commit_keys runs first in commit and in a user edit’s commit none; UsageBook::name panics otherwise
Speed and device limits change only when an apply or user edit commits, and the guard’s policies only when an apply commits publish_limits and Guard::set_policy are called only in commit a_speed_limit_change_keeps_the_users_sessions (the session is kept)
A publish changes only the speed limits that changed user_limits lists changes against published_limits speed_limits_list_only_what_changed, speed_limits_of_an_unchanged_set_change_nothing
Changing a large user set does not hold a runtime worker Staging runs through off_runtime; replaced values are dropped with drop_off_runtime changing_a_large_user_set_leaves_the_runtime_worker_free
A user’s sessions come from no more IPs than their device limit Session::admit refuses a session from a new IP once the limit is reached a_users_sessions_come_from_no_more_ips_than_their_device_limit
A banned IP is turned away, and other IPs are not serve.rs checks Guard::is_banned before the transport handshake an_ip_failing_to_authenticate_is_turned_away_and_others_are_not
A handed-over connection is served as one the inbound’s TCP listener accepted, with the client its header names serve.rs → Connection::serve_socket receives it with system/handoff.rs → receive and serves it through the shared serve_tcp a_handed_over_connection_is_served_with_the_client_its_header_names, tls_grpc_and_vmess_run_over_a_handed_over_connection
No spec tag is empty, so internal targets never collide with a spec’s unique_tags in validate no_tag_may_be_empty

start fails only through its first apply, with the errors a later apply could return, except Disruptive (nothing runs yet) and Stopped. Only the apply’s own blocking-pool work has run by then: the sampler, the guard sweep, the usage push and the actor are spawned only after the first apply commits. Listeners bound during the refused prepare are released when prepare returns its error. start asserts that sample_interval is not zero (the sample interval must not be zero).

A refused apply or user edit returns its ApplyError and leaves the running spec, versions, plane, listeners, user tables, keys, speed and device limits and the guard’s policies as they were. User keys handed out while staging live on a staged copy (lent.keys.clone() in stage_users and stage_user_edit) and are adopted only at commit, so a refused change hands out no key. The handlers and user tables staging built for a refused apply are dropped on the blocking pool.

Dropping the future of a call does not cancel the command once it has been sent: the actor finishes it and its reply is discarded (let _ = reply.send(...)). An apply whose caller gave up still commits. A future dropped while it waits for room in the channel sends nothing.

close, a removal policy, Step::CloseSessions and shutdown all end a connection through its session token, and so do a device-limit refusal in Session::admit and a gRPC carrier’s Carrier. On a stream listener every task serving the connection runs under that token through Shared::spawn_until, so cancelling it ends the task and drops its future. A Hysteria 2 connection is closed by the protocol when its session token is cancelled (see Serving).

Tracker::kill and Tracker::kill_where work per flow: they set the flow’s kill flag and wake it, so its next poll fails in either direction with the flow was killed (ConnectionAborted, track/mod.rs → killed); the session it belongs to is untouched. A drain policy works one level down from sessions, through the same kill: Target::close_flows(&shared.flows) sets the target’s drained flag and calls Tracker::kill_where(|f| *f.outbound() == id), so every tracked flow filed under that version fails like any killed flow. close_rerouted is the same kill over the flows Plane::reroutes picks (Closing what a route change moved). A flow still dialing when the drain comes is not registered yet; once it lands and is metered, Target::close_if_drained sees the flag and kills it (topology/plane.rs; supervisor.rs → drain(target, policy, root, shared)). The routing side is on Routing plane.

Every call through the actor returns Stopped, 0, an empty list or nothing, as listed under Shutdown. The reads that bypass the actor keep answering.

Limit Value Where Meaning
Command channel 16 commands SupervisorBuilder::start → mpsc::channel(16) A caller’s send waits while 16 commands are queued
DEFAULT_SAMPLE_INTERVAL 1 s track/sampler.rs The sampler’s tick, and so the lag of take_usage for live sessions
EVENT_CAPACITY 1024 events track/mod.rs Flow events a subscriber may fall behind before it loses some
SWEEP_INTERVAL 30 s guard.rs How often the guard drops the records of IPs neither banned nor inside their window
sample_interval must not be zero assert in SupervisorBuilder::start Panics otherwise
Outbound versions u64, from 1 per tag plan Kept for removed tags, so RunningState::versions has one entry per tag ever seen
SessionId, FlowId u64, from 1 Sessions::open, Tracker Unique for the supervisor’s lifetime
UserKey u64, from 1 UserKeys::key One per user id, assigned the first time an inbound admits it
RuleId u32 topology/router.rs Fewer than 2^32 rules per route table
etemenanki-app shutdown grace 5 s app/src/main.rs → SHUTDOWN_GRACE
etemenanki-ffi stop grace 2 s ffi/src/proxy.rs → STOP_GRACE
etemenanki-ffi runtime grace 3 s ffi/src/proxy.rs → RUNTIME_GRACE After the stop grace, for the runtime’s threads (shutdown_timeout)
katana shutdown grace 0 src/manager/node.rs

The per-inbound connection limits and the accept-error backoff belong to the serving layer; see Serving. The limits of the whole system are collected on Limits, timeouts and memory.

The integration tests start real supervisors on loopback sockets and drive them with minimal protocol clients from supervisor/tests/support/mod.rs:

Helper What it does
start_on, start_built Start a supervisor on a spec built for freshly picked ports; on ApplyError::Bind pick again, up to 5 retries, since a port can be taken between picking and binding. start_built takes a builder factory, which is how the socket-policy and usage-sink tests install theirs.
base_spec A direct freedom outbound, a block blackhole, the route defaulting to direct, DnsSpec::Single(Backend::System) and default policies.
QUIET, SOON 400 ms to wait for something that should not happen, 5 s for something that should.
Clients SOCKS connect and UDP associate, HTTP CONNECT, a VLESS mux client, echo servers, a self-signed certificate.

The hot-swap and tracking tests end with shutdown(Duration::ZERO) on every supervisor they started. The socket-policy tests drop their handles instead, which runs the same shutdown with a zero grace.

Test File Behaviour it pins
an_established_connection_survives_an_apply_that_keeps_its_inbound tests/hot_swap.rs A kept inbound is reused, not swapped or rebound, and its connection keeps transferring
a_route_change_reaches_the_next_udp_packet_but_not_an_established_tcp_flow tests/hot_swap.rs epoch moves by one; the next UDP packet and a new TCP flow follow the new route; the open TCP flow does not
a_route_change_reaches_the_next_mux_sub_flow_of_a_live_session tests/hot_swap.rs A mux sub-flow opened after the change takes the new route; the earlier one keeps its own
a_changed_outbound_keeps_its_old_flows_unless_drained_with_close tests/hot_swap.rs On the freedom outbound direct: drained names the old version; under Keep the flow opened on it keeps echoing, under Close it is closed; new flows use the new version
a_changed_outbound_drained_with_close_after_closes_its_flows_after_the_grace tests/hot_swap.rs Under CloseAfter, a flow on the old version keeps echoing during the grace and is closed after it; a new flow uses the new version
a_drain_with_close_ends_a_udp_sub_link_but_not_its_association tests/hot_swap.rs Under Close, a SOCKS UDP association’s sub-link on the old version is closed; the association’s next packet opens a sub-link on the new version
an_unchanged_hysteria2_outbound_keeps_its_quic_connection_across_apply tests/hot_swap.rs A reused Hysteria 2 outbound does not handshake again; a rebuilt one dials anew while the flow on the old one keeps echoing
a_hysteria2_obfuscation_change_needs_allow_disruptive tests/hot_swap.rs Disruptive without the flag; with it, restarted names the inbound and the port is kept
a_protocol_change_on_the_same_port_keeps_the_socket_and_old_connections tests/hot_swap.rs swapped, not rebound; a connection accepted before the swap is served by the old handler
a_refused_spec_changes_nothing tests/hot_swap.rs An unknown reference and a failed bind leave the epoch, the users and the routes; a listener bound during the refused prepare is released
removing_a_user_closes_only_their_sessions_and_refuses_them_after tests/hot_swap.rs On a SOCKS inbound, under Close and Keep: the removed user’s session is closed or kept by the policy, the other user’s is untouched, and a new login by the removed user is refused; close(Selector::User) returns 1
a_speed_limit_change_keeps_the_users_sessions tests/hot_swap.rs The session ids before and after a limit change are the same
removing_an_inbound_closes_its_sessions tests/hot_swap.rs removed names it; its connection closes; its port refuses; the other inbound is untouched
shutdown_ends_pending_grace_timers tests/hot_swap.rs One-hour removal and drain timers do not hold up shutdown(Duration::ZERO)
shutdown_does_not_wait_out_a_stalled_hysteria2_handshake tests/hot_swap.rs A client whose QUIC handshake never completes does not hold shutdown
changing_a_large_user_set_leaves_the_runtime_worker_free tests/hot_swap.rs On a runtime with one worker, an apply that replaces a thousand users of a large VMess user set and changes every speed limit leaves a task ticking beside it running: its longest gap stays under a quarter of the apply’s time
a_proxy_header_names_the_client_and_a_bare_connection_is_its_own tests/guard.rs A session sent with a PROXY v2 header has the header’s client as its source; one sent without has the socket’s peer
a_users_sessions_come_from_no_more_ips_than_their_device_limit tests/guard.rs With a device limit of 1, a second session from the same IP is served, one from another IP is closed, and that IP is served once the first IP’s sessions have ended
a_trusted_proxys_forwarded_header_names_the_websocket_client, a_forwarded_header_from_anyone_untrusted_is_ignored tests/guard.rs On a VLESS-over-WebSocket inbound with real_ip, a trusted proxy’s X-Forwarded-For names a session’s source, and a header from a client outside the trusted networks is ignored
the_device_limit_counts_the_forwarded_client, a_forwarded_client_failing_to_authenticate_is_banned_and_others_are_not tests/guard.rs The device limit counts, and a ban falls on, the client a trusted proxy names, not the proxy
an_ip_failing_to_authenticate_is_turned_away_and_others_are_not tests/guard.rs After three refused handshakes from one IP (max_failures 3), that IP is not served even with the right id; another IP is
a_grpc_connection_whose_streams_keep_failing_is_closed tests/guard.rs With carrier_max_failures 3, three streams with an unknown VLESS id on one gRPC connection close the connection
a_handed_over_connection_is_served_with_the_client_its_header_names, tls_grpc_and_vmess_run_over_a_handed_over_connection tests/handoff.rs A connection handed over on a handoff socket is served as the TCP listener serves one, VMess over gRPC and TLS included, with the client the header names as every session’s source
a_malformed_handoff_is_refused_and_the_connection_stays_the_relays, a_handed_over_client_failing_to_authenticate_is_banned tests/handoff.rs A handover that breaks the wire contract is answered 0x01 and leaves the relay its connection; a handed-over client is held to the IP bans
a_reload_adds_moves_and_removes_a_handoff_socket_and_keeps_the_tcp_listener, a_handoff_socket_takes_over_a_stale_file_and_is_set_up_as_asked tests/handoff.rs Adding, moving, changing and removing a handoff socket keep the TCP listener and every connection; a socket’s directory, mode and group are set as asked
a_refused_apply_keeps_a_running_handoff_socket_at_its_path, a_regroup_whose_chown_is_refused_keeps_the_running_handoff_socket, a_refused_apply_leaves_a_running_socket_that_looks_staged_in_place, unix_binds_whose_staging_names_could_meet_are_each_served_at_their_path tests/handoff.rs A refused apply leaves a running Unix or handoff socket at its path and nothing staged; each Unix bind is served at its own path by its own inbound
direct_flows_are_dialed_on_hooked_sockets tests/socket_policy.rs Direct TCP and UDP sockets pass the hook
a_socks_upstream_is_dialed_on_hooked_sockets tests/socket_policy.rs A SOCKS upstream’s control connections and its UDP relay socket pass the hook
a_refusing_hook_fails_the_dial tests/socket_policy.rs A hook error fails the SOCKS request
close_rerouted_closes_only_the_flows_a_route_change_moves tests/hot_swap.rs An apply that moves the default route closes nothing; close_rerouted then closes the flow it moved and keeps the one a rule still sends to the same outbound
the_delay_is_the_second_request_on_the_first_ones_connection, a_site_that_closes_gives_the_first_requests_time, failures_say_why_and_never_outlast_the_timeout, https_to_a_plain_server_fails_its_tls tests/ping.rs http_ping through a named outbound whatever the routing says, its two requests on one connection, and each failure’s PingError (Outbounds)
a_usage_sink_gets_a_batch_per_interval_and_the_last_at_shutdown tests/tracking.rs Batches arrive about one interval apart; with the shutdown batch they hold every byte; take_usage is then empty and usage_snapshot still answers
a_slow_usage_sink_does_not_stall_the_data_plane tests/tracking.rs A sink taking 1 s per report delays only its own batches
cancelling_the_root_cancels_every_session tests/unit/session.rs Bound and unbound sessions both end when the root is cancelled
a_stopped_listener_opens_no_session tests/unit/session.rs Sessions::open under a cancelled stop registers nothing
speed_limits_list_only_what_changed tests/unit/supervisor.rs user_limits lists a changed, a lifted and a new speed limit, and not an unchanged one
speed_limits_of_an_unchanged_set_change_nothing tests/unit/supervisor.rs No speed change is listed when every limit equals the published one
the_empty_plane_drops_everything tests/unit/plane.rs Epoch 0; TCP and UDP, port 53 included, go to the blackhole
the_first_plan_binds_and_builds_everything_then_publishes tests/unit/plan.rs The first plan has no disruptive step and ends with PublishPlane; every tag starts at version 1
each_rebuild_of_a_tag_takes_the_next_version, a_tag_added_back_takes_a_version_it_never_had tests/unit/plan.rs Version numbering
no_tag_may_be_empty tests/unit/validate.rs The empty tag stays the supervisor’s

Nothing asserts on the drop of the last handle (the socket-policy tests end that way without checking it), on ApplyError::Stopped, or on the command channel’s capacity. check is exercised through the app, for example: a_member_with_no_upstream_is_refused (app/tests/integration/e2e_balancer.rs) runs the binary’s --test on a balancer over a freedom outbound and expects a failure; a_backend_without_its_server_is_rejected (app/tests/integration/e2e_dns.rs) runs --test on a UDP DNS backend with no server and expects a failure; and every_lowered_node_builds_and_the_dns_setup_with_it (app/tests/unit/subscribe.rs) calls check on the lowered example subscribe file.

supervisor/benches/tracking.rs measures the cost of a usage take and the reconcile that feeds it over a server-sized ledger, and the per-byte overhead of a metered flow, alone and over many flows of one outbound on every worker (per_byte_parallel). Run it with cargo bench -p etemenanki-supervisor --bench tracking; see Tracking.