Skip to main content

Module lifecycle

Module lifecycle 

Source
Expand description

Flow lifecycle: monitors, links, registry, WAITING_SEND.

Byteflow coordinates exit in one place: finalize_flow (iterative work list — not recursion). The VM only produces FlowOutcome; the runtime revokes Caps, delivers DOWN, propagates links, and sweeps the registry.

FlowExitReason is an explicit argument to finalize, not inferred from the join string. A linked kill therefore reports Link on DOWN, not Fault.

§Identity vs address

TypeRole
FlowIdIdentity. Never reused. Host monitor / link take this.
CapIdAddress. 128-bit CSPRNG token. Bytecode uses Caps.

There is no generation on Caps: a dead flow’s Caps stop resolving (CapTable::revoke_flow bumps the epoch and sweeps every entry held by or targeting that flow). A guessed CapId never grants Send/Ask. LINK / MONITOR require those bits on the addressing Cap; ADMIN is a scheduler Cap, never a default spawn grant.

§Monitors (A ──monitor──> B)

Runtime::monitor(owner, target) or bytecode Fn::monitor(cap) creates a MonitorRef. When target exits, owner receives an Atomic Hop:

tag        = TAG_SYS_DOWN (0xFF01)
request_id = MonitorRef
sender     = target FlowId  (identity, not a Cap)
payload    = FlowExitReason as u64

Selective receive: receive_match_imm(TAG_SYS_DOWN).

Abnormal exit (Fault, Link, …) kills the peer (cooperative: parked flows are taken out of the mailbox; running flows see a kill signal at the next quantum). Normal (clean return / Exit) only drops the link.

With Fn::set_trap_exit / Runtime::set_trap_exit enabled on a peer (BEAM process_flag(trap_exit, true)), every exit of the linked flow — including Normal — delivers a TAG_SYS_EXIT (0xFF02) hop (Message::linked_exit) instead of killing that peer.

Link / Unlink ids fit in i64 (bytecode Int). The counter fails closed (RuntimeError::LinkIdExhausted) instead of wrapping. Insert confirms both endpoints are still in the directory after the link mutex is released (link_if_live), so a peer that already finalized does not leave a zombie row. Lookup is O(1) on the pair; teardown is O(degree).

§exit/2 (ExitSignal, 0x6A)

Fn::exit_signal(cap, reason) sends an exit signal to the flow named by cap. The Cap must carry LINK (a spawn Cap does: ADDRESSING includes it). reason is FlowExitReason as Int (0..=6); anything else traps the caller.

ReasonEffect
Normal (0)No-op
Killed (2)Cooperative kill. trap_exit does not catch it
any other abnormaltrap_exit → TAG_SYS_EXIT hop; otherwise kill with that reason

Host Runtime::kill / admin_kill stay the untrappable Killed path and do not need a Cap. Sample: samples::exit_signal.

§Process dictionary (0x6B–0x6D)

Per-flow, on the VM (not the scheduler). Keys are Int. Values are Unit, Bool, Int, Float, Str, or Bytes. Str and Bytes stay charged on the flow heap quota while the dictionary holds them. List, Message, Cap, and Pid are rejected. Missing key → Unit.

OpcodeFnBEAM
DictPutdict_put(key, value) → previous or Unitput/2
DictGetdict_get(key)get/1
DictErasedict_erase(key) → removed or Uniterase/1

Sample: samples::process_dict (no natives).

§Bytecode supervisor (StartSupervisor 0x72, StartChild 0x6F)

Fn::start_supervisor(strategy) starts a supervisor owned by this flow. The immediate is 0 one-for-one, 1 one-for-all, 2 rest-for-one. Later Fn::start_child(function, RestartPolicy) calls attach to it and write an addressing Cap.

Without StartSupervisor, StartChild uses the host Supervisor that started the caller. A flow from Runtime::spawn that has not called StartSupervisor has no supervisor, so StartChild fails that flow. Sample: samples::supervised_tree (host Supervisor::start_child on root, then bytecode start_child).

§Registry

register_name(name, CapId) stores a Cap, never a FlowId. Host whereis returns that stored Cap. Bytecode Whereis mints a SEND Cap for the caller (mint_or_reuse) so discovery does not bypass the holder model. Finalize unregisters every name for the dead flow. Bytecode RegisterName only publishes the calling flow and requires SEND on self-authority.

§Ask vs target death

An Ask / AskTimeout parked for a reply is indexed on the target. When that flow finalizes, the asker is taken off its mailbox and resumed with TAG_SYS_EXIT (Message::linked_exit: sender = dead FlowId, payload = FlowExitReason). This is not a hang and is distinct from AskTimeout writing Unit.

Process-wide, outstanding Ask waiters are capped by RuntimeConfig::max_ask_waits (0 = unlimited). A park that would exceed the cap fails the asker closed.

§WAITING_SEND

Bytecode Send / Ask against a full Reject inbox park the sender in the target mailbox. Each pop admits one waiter (no wake storm). The hop’s quota charge is released when the waiter is admitted, or on finalize if the flow dies still parked. That charge is not forwarded to the process MemoryBudget. Host Runtime::send still returns SendError::MailboxFull.

Mailbox::close runs before directory unregister so a sender that lost the Full/park race cannot park on a dead inbox (that would leak the flow).

§Kill

Runtime::kill(id) and bytecode ExitSignal with reason Killed are cooperative: a parked receiver / WAITING_SEND finalizes immediately (FlowExitReason::Killed); a running flow dies at the next quantum. ExitSignal aimed at self while the flow is on a worker finalizes that flow immediately (the worker already holds it, so the parked-extract path cannot see it).

§Supervisor strategies

Host Supervisor supports OneForOne, OneForAll, and RestForOne. Sibling abort uses FlowExitReason::Supervisor and an expected_shutdown flag so those exits do not start another cascade. Intensity still counts the triggering restart, not the sibling kills.

§Resource governor

RuntimeConfig::max_flows (0 = unlimited) is checked on every host and bytecode spawn. Over the cap → SpawnError::FlowLimit. Outstanding Ask waiters are capped by RuntimeConfig::max_ask_waits; outstanding HostAwait operations by max_host_awaits; live links / monitors / registry names by max_links / max_monitors / max_registry_names (0 = unlimited; sandbox sets finite values). Accounting for relation tables lives inside each store under the same mutex as the table.

Each flow also carries a FlowQuota from RuntimeConfig::quota: remaining CPU (distinct from the scheduler quantum), heap charge (Str/Bytes on register store, with release-on-overwrite delta accounting), and spawn/send token buckets. Process-wide heap is capped by RuntimeConfig::max_runtime_bytes via MemoryBudget. Default is QuotaConfig::permissive; QuotaConfig::sandbox is the isolation starting point. Exhaustion fails closed (QuotaError). An ADMIN Cap can top up CPU, heap limit, or the send bucket.