Files
claudeandClaude Opus 5.5 f87d5eae0d H4: Long-run stability (make stability), and two memory leaks fixed
make stability (tools/stability.sh, StabilityReport) runs one dedicated
server for 50 bot matches back to back, unthrottled, sampling its resident
memory, and fails on a crash, hang, engine or script error, a match with
no winner, or memory that grows after the first five matches.

The first run had no crash, hang or error, but showed two leaks:
- ServerLoop kept every tick's duration in an array (8 bytes a tick,
  about 21 MB a day at 30 Hz) and copied and sorted all of it for every
  progress line. TickTimes keeps exact statistics in fixed memory: a
  one-microsecond histogram up to 65 ms, plus the rare slower ticks.
- Resident memory grew about 28 MB a match (905 MB after 42 matches):
  glibc gives each thread its own malloc arena, and every next island is
  built on a worker thread. The server launchers (packaging/server,
  packaging/dist, so also the .deb, Docker image and systemd unit) and
  tools/godot.sh for --server now set MALLOC_ARENA_MAX=1 unless already
  set. Tick times were unchanged (p50 501 vs 507 us, p99 1635 vs 1625).

After: 50 matches (seed 4242) in 756 s, all with a winner, exit 0, no
errors; static memory +1.6 KiB and resident +145 KiB a match, flat at
204 MB from match 26 on. No match crashed, so there is no crash seed to
turn into a regression test.

Tests: TickTimes against a sorted list, its fixed memory, the launchers'
setting, and StabilityReport. make check, dist-check and
server-package-check pass.

Closes #57

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 07:15:23 -05:00

53 lines
2.2 KiB
Bash
Executable File

#!/usr/bin/env bash
# Runs a Tidewall dedicated server and turns SIGTERM or SIGINT into a clean stop: the
# server tells everyone connected it is shutting down, logs its statistics and exits 0.
# The engine can't catch signals itself, so this wrapper creates the server's
# --stop-file when one arrives, and kills the server only if it hasn't gone after
# TIDEWALL_STOP_SECONDS (default 15).
#
# tidewall-server [server options...] e.g. --config=/etc/tidewall/server.cfg
#
# Where things are, overridable from the environment:
# TIDEWALL_HOME the install: engine/godot and game/ (default /opt/tidewall)
# TIDEWALL_ENGINE the engine binary (default $TIDEWALL_HOME/engine/godot)
# TIDEWALL_GAME the game's project directory (default $TIDEWALL_HOME/game)
# TIDEWALL_STATE a writable directory for the engine's own files and the stop file
# (default $RUNTIME_DIRECTORY under systemd, else /tmp/tidewall-server)
set -u
home="${TIDEWALL_HOME:-/opt/tidewall}"
engine="${TIDEWALL_ENGINE:-$home/engine/godot}"
game="${TIDEWALL_GAME:-$home/game}"
state="${TIDEWALL_STATE:-${RUNTIME_DIRECTORY:-/tmp/tidewall-server}}"
grace="${TIDEWALL_STOP_SECONDS:-15}"
stop_file="$state/stop"
# glibc gives each thread that allocates its own malloc arena and rarely hands the
# memory back. The server builds every next island on a worker thread, so with the
# default arenas its resident memory grew about 28 MB a match, forever (H4, #57).
# One arena keeps it flat at no measured cost to tick time.
export MALLOC_ARENA_MAX="${MALLOC_ARENA_MAX:-1}"
mkdir -p "$state/data" "$state/config" "$state/cache"
rm -f "$stop_file"
# The engine writes its user data and caches here, so the install can stay read-only.
export XDG_DATA_HOME="$state/data" XDG_CONFIG_HOME="$state/config" XDG_CACHE_HOME="$state/cache"
"$engine" --headless --path "$game" -- --server --stop-file="$stop_file" "$@" &
child=$!
request_stop() {
touch "$stop_file"
# If it hasn't stopped in time, stop it the hard way.
( sleep "$grace"; kill -KILL "$child" 2>/dev/null ) &
}
trap request_stop TERM INT
wait "$child"
code=$?
# A trapped signal interrupts wait; keep waiting until the server has really gone.
while kill -0 "$child" 2>/dev/null; do
wait "$child"
code=$?
done
exit "$code"