Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
5969dbc
A stop signal goes through the event loop: it is never lost, and a se…
Dyotson Nov 15, 2026
19a5ea7
tools/stop_loop.py: each stop's time to the moment the process ends, …
Dyotson Nov 15, 2026
6dbab8d
After the stop the signals are SIG_IGN: one as the interpreter ends n…
Dyotson Nov 15, 2026
0846305
tools/stop_loop.py: with more than one signal, how many stops logged …
Dyotson Nov 15, 2026
2778ad2
docs/operating.md: a stop signal while Python still loads the server'…
Dyotson Nov 15, 2026
6481eaa
The stop takes the HTTP port's listening sockets off the loop instead…
Dyotson Nov 15, 2026
4095cae
tests/server/test_stop.py: stop_accepting on Windows, where the port …
Dyotson Nov 15, 2026
e6c8782
The stop's saves no longer wait for a Discord status update under way…
Dyotson Nov 15, 2026
34798be
docs/operating.md: the Discord status at the stop, a flood of signals…
Dyotson Nov 15, 2026
e05dc3c
A held signal turns only itself to SIG_IGN: another one waiting in th…
Dyotson Nov 15, 2026
b07548a
tools/stop_loop.py --slow-discord, and docs/operating.md: a Discord u…
Dyotson Nov 15, 2026
3f1dabe
docs/operating.md: the stop's paragraphs reflowed, a stuck stop in a …
Dyotson Nov 15, 2026
8f49d38
end(): a SIGINT in the moment the loop gives Python's default handler…
Dyotson Nov 15, 2026
6ceed37
The stop signals' handler is a signal.signal one on every system, nev…
Dyotson Nov 15, 2026
b5e52c4
A stop signal a held event loop has not taken in 5 s breaks into what…
Dyotson Nov 18, 2026
a3775dc
The stop takes nothing new before its saves: the UDP port closes and …
Dyotson Nov 18, 2026
cee6198
docs/operating.md: after a second signal the boards' line is not the …
Dyotson Nov 18, 2026
19afa56
tools/stop_loop.py --held: the signal while the loop is held under or…
Dyotson Nov 18, 2026
0d4d39f
A stop begun by a break-in ends main()'s wait: no tick of the timers …
Dyotson Nov 18, 2026
3bf1508
A break-in as the loop frees itself no longer ends the stop with exit…
Dyotson Nov 19, 2026
23b6fd0
--stop-limit refuses inf, nan, a negative number and anything past 36…
Dyotson Nov 19, 2026
d943306
A break-in that lands in a connection's task is not logged as an erro…
Dyotson Nov 23, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ server on the player's own machine, and a server added by its address connects t
| `tools/server_probe.py` | checks a server from outside (status page, UDP HELLO -> WELCOME) |
| `tools/xtsw_decode.py` | decodes the requests a server saved with `--dump-requests` |
| `tools/event_admin.py` | manages community events on a server |
| `tools/stop_loop.py` | stops a busy server on this machine thousands of times and counts the stops that were lost or did not save (#78) |
| `accounts/` | the website accounts service for forza.dyotson.dev/api/ (Django, django-allauth, django-ninja): its own dependencies, image and tests ([`accounts/README.md`](accounts/README.md)) |
| `screening/` | the screening worker for community photos, videos and designs (its own Docker image, [`screening/README.md`](screening/README.md)) |
| `video/` | the video converter: the game's movie uploads to MP4 with ffmpeg, in a container with no network (its own Docker image, [`video/README.md`](video/README.md)) |
Expand Down
1 change: 1 addition & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Options are command line flags of `forza_live_server.py` (`--help` lists them al
| `--no-invites` | `FORZA_INVITES=0` | drop every game invite |
| `--stall-limit MS` | | log what holds the event loop longer than this, and count it on the status page (default 50; `0`: off) ([what holds the event loop](operating.md#what-holds-the-event-loop)) |
| `--stall-stack MS` | | also log where the loop was once it has been held this long (default 250; `0`: never) |
| `--stop-limit S` | | a stop signal the event loop has not taken after this many seconds (the loop is held) breaks into what holds it, so that the saves are made before the kill (default 5; `0`: never; at most 3600) ([stopping the server](operating.md#stopping-the-server)) |
| `--server-name HOST` | `FORZA_SERVER_NAMES` | the names players type to reach this server ([signed requests](#signed-requests)) |
| `--rebind-legacy` | `FORZA_REBIND_LEGACY=1` | let an XUID bound to a friends key move to the first signed game |
| `--raw-key-cutoff UNIX_TIME` | `FORZA_RAW_KEY_CUTOFF` | refuse friends keys sent raw after this time |
Expand Down
102 changes: 93 additions & 9 deletions docs/operating.md
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,89 @@ token, and let the accounts container reach the HTTP port directly, from an addr
`--trusted-proxy` (the routes refuse the tunnel and proxies, as moderation does). Without the token
the routes are off and the account page cannot delete.

## Stopping the server

`docker compose stop` (SIGTERM) and Ctrl+C in a terminal (SIGINT) stop the server the same way.
The signal only asks for the stop: the server ends what it is doing at that moment (a datagram, a
request, a tick of its timers) and then stops, in this order. A Discord status update under way
is not waited for: the saves come first.

1. `stop: SIGTERM: the server stops` in the log (`SIGINT` for Ctrl+C).
2. The server takes nothing new from here: no new connection on the HTTP port, no datagram on
the UDP port (no player joins), and the requests of the connections already open end where
they are, unanswered. Nothing a player does after the first line can change a store after
its save.
3. The [event loop's watchdog](#what-holds-the-event-loop) is turned off: the saves hold the loop,
on purpose.
4. The saves: the registrations (`live_state.json`), the co-op ledger and friends, then
`notifications.json` and the boards' `scoreboards.json` last, each with its own line
([scores on disk](#scores-on-disk), [notifications on disk](#notifications-on-disk)).
5. When the Discord status is set up: an update that was under way when the signal came is given
up to 5 s more to end, then the status says the server is offline (with its own line only
when Discord cannot be reached). Then the process ends with exit status 0.

So a stop ends with the notifications' line and then the boards' (after a second signal, one
more line, below):

stop: SIGTERM: the server stops
notifications: notifications.json written whole at the stop in 0.14 s: it has every change: 29310 notifications for 3998 players, 5 broadcasts
scoreboards: scoreboards.json written whole at the stop in 0.32 s: it has every change: 412 boards, 50123 rows, 812 unranked rows, 1500 players in the 1000 Club

A stop signal that comes while the server starts (while it reads its files, or while Python is
still loading its code) is noted: the start reads every file whole, and the server then stops as
above, the first line being `stop: SIGTERM during the start: the server stops now that it is
done`. A start that refuses to run (a file it cannot read) still ends with exit status 1 and its
own line. A volunteer relay (`--relay-only`) has nothing to save: it logs the first line and ends
with exit status 0.

**A second signal changes nothing.** Once the stop has begun, every further SIGTERM or SIGINT (a
second `docker compose stop`, Ctrl+C pressed again) is noted and ignored until the process ends:
no save is cut short, and the log says so once for each part of the stop it came in, the first
line before the notifications' line, the second after the boards' line (the stop's last line
then). After the first signal of each part the server ignores the others outright, so even a
flood of them does not slow the saves:

stop: SIGTERM again while the registrations, co-op ledger and friends were written: ignored until they were
stop: SIGTERM again while notifications.json and scoreboards.json were written: ignored until they were

The exit status stays 0, even for a signal that comes as the process ends.

The stop takes a fraction of a second, most of it those last two writes (a slow disk can make them
take a few seconds), and on a bad network a few more for the Discord status after them; the
compose file gives it 20 s (`stop_grace_period`). Only a kill ends it earlier: Docker's at the end
of that time, `docker compose kill`, or SIGKILL. A kill costs the boards and the notifications
nothing (their journals have every change) and the other stores what changed since their last
save; and it leaves `scoreboards.json` and `notifications.json` complete only together with their
journals, which matters before a rollback ([scores on disk](#scores-on-disk)).

A Discord update stuck on the network past its 5 s does not hold the saves, which are made by
then, but the process ends only once the update gives up: past the 20 s Docker kills it (exit
status 137), at no cost to what was saved. Since every signal after the first is ignored, a stop
that does not end is not ended by a second Ctrl+C either: in a terminal, Ctrl+\ (SIGQUIT) or
`kill -9` ends it.

**A held event loop.** The stop runs on the event loop, so a loop held by a bug (code that runs
on and on: the [watchdog's](#what-holds-the-event-loop) lines say what) takes the signal only
once it is free. Free within 5 s of the signal, it stops then, as above. If not, the stop breaks
into what holds the loop, as versions before this one did for every signal, and runs as above
with every save, exit status 0. The saves take well under a second, so they end long before
Docker's 20 s. The log says so first:

stop: SIGTERM came 5 s ago and the event loop has not taken it: something holds the loop (the watchdog's lines say what); the stop breaks into it

What held the loop is cut short where it was. A try that lands where Python only prints errors
(`Exception ignored in ...`, `BreakIn`) is followed by another a tenth of a second later.
Inside a call that cannot be broken into (C code that waits, such as a lock or a library's
call), the stop waits for that call to return and then runs at once. The log says
`stop: the event loop is still held 5 s later: inside a call that cannot be broken into; the
stop waits for it`. If the call has not returned by Docker's kill, nothing can save there (exit
status 137). `--stop-limit S` sets the 5 s (0: never break in; at most 3600). Keep it well under the 20 s, so
that the saves still have their time after it.

Versions before this one stopped through an exception raised wherever the server was when the
signal came; inside code whose errors Python only prints (`Exception ignored in ...` in the log),
the stop was lost: the server ran on until Docker killed it 20 s later, without the stop's saves.

## Backups and rollback

Everything the server keeps (players, friends, clubs, scores, photos, designs, moderation lists) is in the data folder. Stop the server for a moment so every file is complete, copy the folder, and start it again (the commands are in the [hosting guide](hosting.md#keeping-it-running)). To restore a backup, stop the server, replace the data folder with the backup's, and start it.
Expand Down Expand Up @@ -796,13 +879,9 @@ does (rolling back, below).
The stores that have no journal (the registrations in `live_state.json`, the co-op ledger and
friends) are written first, each whatever became of the others (an error in one is logged as
`stop: the friends could not be written` with that save's own traceback, and the others and the
boards are still written). While they are written a second stop signal (a second `docker compose
stop`, Ctrl+C) is ignored and logged (`stop: SIGTERM again while the registrations, co-op ledger
and friends were written: ignored until they were`); once they are, a signal ends the stop again,
which costs the boards nothing. The notifications' last write comes between those stores and the
boards: a signal while it runs ends it (`stop: KeyboardInterrupt while notifications.json was
written whole: not written, its journal has every change; the boards are written all the same`, a
warning) and the boards are still written; only a signal after that one cuts theirs short.
boards are still written). The notifications' last write comes between those stores and the
boards. A second stop signal (a second `docker compose stop`, Ctrl+C) during any of them is noted
and ignored, and no write is cut short ([stopping the server](#stopping-the-server)).

**A `scoreboards.json` that cannot be read.** When the file is there but cannot be read (a read
error, permissions) the boards come from its `.bak` and the journal, as above. When the `.bak`
Expand Down Expand Up @@ -831,8 +910,13 @@ it held`.

What that means for you:

- **Stopping**: one `docker compose stop`, and let it end. The boards are written last, after
everything else the server saves.
- **Stopping**: `docker compose stop`, and let it end. The boards are written last, after
everything else the server saves. A second `docker compose stop` (or Ctrl+C) while it stops is
harmless: it is logged and ignored, and the stop goes on as for one. What ends a stop early is a
kill (`docker compose kill`, or the 20 s running out), which leaves `scoreboards.json` complete
only with its journal: the boards' line at the stop (`scoreboards: ... at the stop`, the last
line, or the one before a line about a signal ignored meanwhile) is there only when the stop
wrote them.
- **Backups**: the whole data folder, with the server stopped. A copy of `scoreboards.json`
taken while the server runs is not complete without `scoreboards.journal` beside it: by
itself it lacks up to a minute of scores. If you must copy from a running server, copy
Expand Down
Loading
Loading