tobogganSe connecter

louise

The Louise language: compiler, runtime, std, tooling

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

141 lignes · 12208 octetsBrut · Historique

ADR 0060: OS signals, graceful shutdown, and supervised applications

Status: Proposed (2026-10-23, issue #306); needs engineering review (#44), in particular the signal thread's interplay with the scheduler (Signals).

Context

A Louise program could not learn that it was asked to stop: std/os and std/process had no signal API. toki2's stress test, in fedora-minimal:44 with podman, measured both consequences:

podman stop takesExit code
no init10.3 s137 (SIGKILL after the grace period)
--init0.2 s143 (killed by SIGTERM, no cleanup)
  • As PID 1, SIGTERM was ignored: Linux drops a signal sent to PID 1 when its disposition is the default.
  • Otherwise it killed the program at once: in-flight requests were cut, nothing stopped accepting first, pooled connections were not closed.

Reading std/supervisor for a fix showed gaps too: the restart limit had no time window (toki2's 11th crash in weeks would take the server down), a supervisor could not be told to stop its children in order, every child was restarted the same way, and nesting supervisors was not tested.

Decision

Signals

  • One thread takes them. tl_main and tl_main_program start it before any other runtime thread (louise_rt/src/signals.rs):
    1. They install a handler for SIGTERM, SIGINT and SIGHUP. It never runs; its only job is to make the kernel deliver the signals to PID 1.
    2. They block the three signals. Every later thread inherits the mask: the scheduler's threads, the blocking pools and the console writer. So no Louise code, C callback or blocking call is interrupted, and nothing is EINTR'd.
    3. The louise-signals thread takes them with sigwait.
  • Delivery is a message. os.signals([Term, Interrupt, Hangup]) -> Subject<Signal> registers the calling actor's new subject; several actors may subscribe.
    • The signal thread never touches the scheduler. It records the signal and wakes the reactor, as a finished blocking call does.
    • A scheduler thread then sends it to every live subscriber, as a nullary Signal constructor (signals::collect). It does so between slices and when idle, so a busy scheduler delivers within a slice.
  • Nobody subscribed (or every subscriber stopped) means the program ends as the default action would:
    • the console is flushed first;
    • the status is 128 + the signal's number: 143 for SIGTERM, 130 for SIGINT, 129 for SIGHUP;
    • this holds as PID 1 too, so podman stop takes milliseconds without --init.
  • Deadlock detection treats a live subscriber as something the outside can still wake. A program whose only actor waits for SIGTERM is waiting, not deadlocked.
  • The interpreter accepts subscriptions and delivers nothing, as it runs blocking calls inline: signals need --native. Ctrl-C keeps ending louise run as before.
  • What is left to the default: other signals (SIGQUIT, SIGUSR1...) keep their dispositions. SIGPIPE is not touched either: TLS writes avoid it with MSG_NOSIGNAL (ADR 0038).

process.stop and Shutdown

  • ExitReason gains Shutdown, added last so the runtime's constructor numbers hold: Normal 0, Killed 1, Crashed 2, Shutdown 3.
  • process.stop(pid), a new proc operation (the fifteenth, PROC_OPS), asks an actor to stop:
    • an actor that traps exits gets ExitSignal(self(), Shutdown), to clean up and return;
    • any other stops at once with reason Shutdown, which links propagate like any abnormal exit and monitors see.
  • A root that stops with Shutdown exits 0: it was asked to stop, and did.
  • This is OTP's exit(Pid, shutdown). kill stays the brutal kill.

Draining an HTTP server

http.serve_until(server, settings, handler, stop) serves like serve_with until any message arrives on stop, a subject of any message type that the caller owns. That can be os.signals([Term]) itself, process.exits() for a main that traps exits under [application], or a subject another actor sends on. Then:

  1. The acceptor gets a Stop event, and the listener is closed (net.close_listener, new):
    • the socket is shut down, so new clients are refused at once;
    • an actor parked in accept is woken (Shared::wake_token), and accept fails with "the listener is closed".
  2. The acceptor asks every open connection to close. This reuses ADR 0012 Revision 3's drain: idle connections close at once, busy ones after their current response, with Connection: close. Connections accepted just before the stop are asked as their watchdogs report them.
  3. It waits until they have ended, for settings.drain_timeout at most (new, default 10 s), then kills the connection actors still running.
  4. serve_until returns. A crash of the acceptor is serve_until's crash.

The accept loop is unchanged until a stop, so serving costs the same.

Supervisors

  • Spec(strategy, children, intensity, period_ms) and supervisor.run(spec):
    • more than intensity restarts within period_ms milliseconds make the supervisor give up;
    • giving up stops the other children first, then crashes;
    • a negative period_ms counts over the supervisor's life.
    • supervise(strategy, children, max_restarts) stays as it was, a lifetime count, so it still needs no clock.
  • Child kinds, as OTP's, via ChildWith(name, start, restart, shutdown_timeout):
    • Permanent is restarted after a crash and after it returns;
    • Transient only after a crash; a return or a Shutdown means it is done;
    • Temporary is never restarted.
    • Child(name, start) keeps its meaning: transient, which is what the supervisor did before, with 5 s to stop.
  • Ordered shutdown. A supervisor told to stop (a Shutdown exit signal from a non-child) stops its children last-started first:
    • each one gets process.stop, then is killed if it is not done within its shutdown_timeout;
    • the supervisor returns once they are all done.
    • A supervisor child of another (ChildWith(..., fn() { supervisor.run(...) }, ...)) stops its own children the same way, so a tree stops leaves first.

[application]: a supervised main

[application]
restart = "transient"        # permanent | transient | temporary
intensity = 5                # at most 5 restarts...
period_ms = 60000            # ...within 60 s, else exit non-zero
shutdown_timeout_ms = 10000  # on a signal, main's time to stop
signals = ["term", "int"]    # term | int | hup: start a shutdown
  • Programs keep a plain main; the policy is deployment configuration, so it lives in the manifest. The section is the root package's; a dependency's is ignored. Without it, nothing changes: main runs once, and the program exits when it returns or panics.

  • With it, louise run and louise build add a generated module, +application, which no file can be named or import. Its main is the program's entry, and calls supervisor.application(Application(...), fn() { main.main() }). The package's main must therefore be pub. Both back ends get the same behaviour from plain Louise.

  • supervisor.application traps exits, subscribes to the listed signals, and runs main linked:

    • main returns: the program exits 0, for every kind. Whether permanent should run main again after a normal return is left open; it does not.
    • main crashes: temporary ends the program with that panic (status 101). transient and permanent restart main, unless that is more than intensity restarts within period_ms; then the program ends with the last panic, and systemd, podman or Kubernetes take over.
    • One of the signals arrives: main is stopped (process.stop). A main that traps exits gets Shutdown, can drain (serve_until) and clean up, then returns. One that does not stops at once, with what it linked. After shutdown_timeout_ms it is killed. The program then exits 0.
  • Boot is part of main, so a restart re-runs it. That is fine when boot steps are idempotent:

    • settings are read again;
    • migrations use CREATE ... IF NOT EXISTS;
    • a crashed main's listener is released, so http.listen binds again.

    The intensity bounds a crash loop at boot: a broken DATABASE_URL gives intensity quick restarts, then a non-zero exit with the panic printed.

  • Tooling: louise check reports an invalid section, with its place in the manifest: unknown keys, intensity or period_ms below 1, a negative timeout, an unknown kind or signal. louise test ignores the section; tests have their own harness.

  • Precedence: the manifest is the only source today. An operator override (LOUISE_RESTART_INTENSITY, say) is left for when someone needs it. So is louise check warning about permanent with a main that obviously returns: since a normal return always exits, it would warn about nothing.

Consequences

  • Containers stop promptly: podman stop takes milliseconds as PID 1, with or without --init. With [application] or serve_until, in-flight requests finish first.
  • Breaking changes:
    • ExitReason has a fourth constructor, so a match on it without a wildcard needs a Shutdown arm (E0301 says so).
    • Settings has a ninth field, drain_timeout; Settings(..http.defaults(), ...) is unaffected.
    • proc has a fifteenth operation. A handler of proc written in Louise (none in the repository) needs a stop clause.
  • The process signal mask: blocking the three signals in every runtime thread means a C library that relies on receiving SIGINT or SIGTERM itself no longer does. No binding in the repository does. A child process started by C code inherits the mask and should reset it.
  • A signal arriving once main has returned, while the program writes its last output and exits, is not delivered: a live subscriber's message would have no scheduler to carry it. Without a subscriber, it ends the program at once, as before.

Verification

  • crates/louise/tests/signals.rs:
    • a subscriber gets SIGHUP and SIGTERM as messages, cleans up and exits 0;
    • without one, SIGTERM, SIGINT and SIGHUP end the program in under a second with 143, 130 and 129;
    • as PID 1 (a new PID namespace, unshare -Urpf), SIGTERM still does.
  • http_drain.rs, on SIGTERM:
    • an idle kept-alive connection is closed;
    • new clients are refused;
    • a slow request completes with Connection: close;
    • serve_until returns, and the program exits 0 in well under a second;
    • a request past the 2 s drain timeout is cut at 2 s.
  • tests/run/supervisor_shutdown.lse, in both back ends (natively on 1, 4 and 22 threads, without leaks):
    • stop on trapping and non-trapping actors;
    • more than 3 crashes within a second give up, while the same crashes spread over more than the window keep going;
    • temporary and permanent children;
    • a tree stopped last-started first, a nested supervisor's leaf first, and a stubborn child killed after its timeout.
  • application.rs:
    • a main crashing twice then returning runs three times and exits 0, natively and in the interpreter;
    • three crashes with intensity = 2 end the program with the last panic, 101;
    • SIGTERM to a main that traps exits runs its cleanup and exits 0, as PID 1 too;
    • a main ignoring the shutdown is killed after 300 ms and exits 0.

Alternatives considered

  • A signal handler that sends the message itself: a handler may call only async-signal-safe functions, and delivering a message takes locks and allocates. A self-pipe registered with the reactor would work, but it ties delivery to the reactor's loop. sigwait on a thread of its own is simpler and needs no async-signal-safety.
  • main returning a supervisor.Spec: this was the issue's first idea, a new signature for main. The manifest keeps main plain and puts deployment policy with deployment configuration (the issue's refinement).
  • Restarting main blindly, without a window: a boot error would loop forever.
  • A field for the drain timeout on serve_until instead of in Settings: the other limits of a server are in Settings, and a record update keeps call sites short.