# Listener generations: changing listeners without restarting the controller

URL: https://scalws.com/docs/adaptive-runtime/listener-generations/
Source: `docs/listener-generations.md` in the ScalWS repository

Design: ADR-0037. Applies to `scalws-controller` with `listen: inherited` (the default).

## What a reload can change

| Change | Socket | Effect |
|---|---|---|
| TLS certificates, names, PROXY protocol, routes, tenants | kept | rolling reload only |
| add a port / address (TCP) | bound **before** the generation is published | new instances serve it; old ones never did |
| remove a port / address | kept until the last instance of a generation that uses it has left | then closed |
| change a bind address (`127.0.0.1:8081` → `127.0.0.2:8081`) | new bound, old closed after the drain | |
| add / remove `http3: true` | UDP socket bound / closed the same way; the QUIC demux is restarted with the new UDP set (ADR-0039) | |
| IPv6 | like IPv4 | |
| pairing of `[::]:p` with an IPv4 listener on `p` (its `IPV6_V6ONLY` mode) | TCP: a new socket of the new mode; the old one stays with the generations that have it until they drain. UDP: the reload is refused (a UDP address cannot be bound twice) | |

A socket is identified by transport and bound address. The same address in the old and
the new generation is the same socket and the same accept queue: no gap, no reset, as long
as its IPv6-only mode is the one the new generation needs.

Every holder of a socket (the controller, each instance, the demux) shares its state, and
any of them can shut it down for all. The controller checks its TCP sockets on every tick:
one that no longer listens (`shutdown(SHUT_RD)` by a holder) listens again, with a warning
and `listeners.last_error`. Its UDP sockets too: one a holder connected to a single peer is
disconnected (`connect(AF_UNSPEC)` keeps its port), one put in a reuseport group is closed to
new members, a shrunk receive buffer is set back to the demux's 8 MiB, and a socket filter,
which cannot be removed once locked, is reported once (sandboxed instances cannot attach one:
their seccomp filter refuses it). A UDP socket shut down for sending (`SHUT_WR`) or filtered
cannot be repaired or bound again while the others hold it; see the accepted limits of the
sandbox design.

## Failure handling

* **Bind failure** (port in use, address not local, permission): the reload is rejected
  with the reason (`listener alt (tcp 127.0.0.1:8090): Address in use`); the generation
  number does not change; sockets opened for it are closed again; `status` shows the
  error in `listeners.last_error`.
* **New generation never becomes ready** (for example an unreadable certificate): its
  instances fail and are retried with backoff; the previous generation keeps serving.
  A corrected `reload` recovers.
* **Controller restart in the middle of a transition:** the new controller takes the
  sockets back from the running instances (`<runtime_dir>/<id>/listen-handback.sock`,
  0600, same user only), so there is still one socket per address; it binds only
  addresses no instance holds. An instance is a lower-trust peer: a handed-back TCP socket
  is kept only if it listens, a UDP one only if it is unconnected, and only if every
  instance handed back the same socket (`SO_COOKIE`); otherwise a new one is bound, with a
  warning.
* **QUIC demux left running** (`systemctl stop` and OpenRC's `stop` end it, so HTTP/3 is
  no longer answered; the instances that keep serving after such a stop, on systemd 254
  and later and on OpenRC, keep their handed-out UDP sockets bound until they end or a
  new controller takes them back; the demux can still
  outlive a controller that was killed or stopped outside the packaged unit and init
  script): it holds the HTTP/3 UDP sockets and cannot hand them
  back. When such a socket cannot be bound at the start, the controller ends the adopted
  demux (only handshakes in progress are lost, ADR-0040) and binds it; a new demux is
  started. A generation whose sockets still cannot be bound is logged and retried every
  tick instead of stopping the controller.

## Status

`scalws-controller status` → `listeners.sockets[]`: `socket` (`tcp 127.0.0.1:8080`),
`names`, `state` (`active` = used by the current generation, `draining` = only by older
ones), `generations`; `listeners.last_error`.

## Test

`scripts/controller-listeners.sh`: add a port, add HTTPS, add IPv6, change a bind
address, remove a listener, port in use, invalid certificate and recovery, controller
restart mid-transition: all under continuous keep-alive and new-connection load on the
address that stays.
