mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 12:27:28 +01:00
Why apps hang while systemd calls them active. Every app unit has MemoryHigh=768M (MemoryMax=1G): above 768M the kernel slows the process instead of stopping it. Node does not read that. It sizes its heap from the whole machine and lets garbage pile up past the mark before it collects. Measured on Solon's production build (a database with no law acts), 930 requests eight at a time: no limit 775 MB peak (855 and 990 on other runs), 364 MB idle 512 MB heap 585 MB peak, same answers, same speed On 2026-11-28 solon stopped answering three times while "active", a favicon took 4 s, and Loki, OrangeCat and Substrata on the same box were fine; the reconciler also caught substrata, heidi and sites not answering within the same day. Not proven from the box (no session can read it; forge-ops#29 prints memory.events), but it is the mechanism the unit file itself sets up. launch.sh.tmpl: start node with --max-old-space-size at 65 % of the lower of the unit's memory.high and memory.max (768M -> 499). NODE_HEAP_MB or an own --max-old-space-size in the app's .env wins; a unit with no limit gets no flag. Setting it from inside the app does not work (v8.setFlagsFromString after start: 849 MB peak, no better than none). deploy.sh: launch.sh was written once by sync-infra and never again, so a template change reached no running app. Each deploy now renders it again with the generator's own function (lib.sh render_launch, which sync-infra uses too), keeping the box copy's port, checked with bash -n before it replaces the old file. The old file is kept and put back on a rollback. test-launch-template.sh: 8 new behavioural checks. test:ops passes. Claude-Session: https://claude.ai/code/session_019MDtmvTCDbJAYNKQpNyKzz Co-authored-by: Claude <noreply@anthropic.com>
192 lines
8.7 KiB
Bash
Executable File
192 lines
8.7 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Behavioural test for scripts/hetzner/launch.sh.tmpl.
|
|
#
|
|
# This does NOT grep the template for "pwd -P" — a gate that asserts a string is
|
|
# present passes for a hundred wrong reasons and proves nothing about behaviour.
|
|
# It builds the real /opt layout (a release directory plus an `app` SYMLINK into
|
|
# it, which is what the box actually has) and checks that the template's own
|
|
# server.js discovery finds the file through that symlink.
|
|
#
|
|
# Mutate `pwd -P` back to `pwd` in the template and this test must go red. That
|
|
# is the whole point: the plain-`pwd` version is what crash-looped vitareba
|
|
# (2026-07-17) and sbb-fundbuero (2026-09-08), and both times it was invisible
|
|
# until an app failed to boot in production.
|
|
set -euo pipefail
|
|
|
|
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
|
|
TMPL="$HERE/launch.sh.tmpl"
|
|
fails=0
|
|
|
|
check() {
|
|
local name="$1" expected="$2" actual="$3"
|
|
if [ "$expected" = "$actual" ]; then
|
|
echo " ✓ $name"
|
|
else
|
|
echo " ✗ $name" >&2
|
|
echo " expected: $expected" >&2
|
|
echo " actual: $actual" >&2
|
|
fails=$((fails + 1))
|
|
fi
|
|
}
|
|
|
|
echo "launch.sh.tmpl: behavioural checks"
|
|
|
|
[ -f "$TMPL" ] || { echo "✗ $TMPL missing" >&2; exit 1; }
|
|
|
|
# --- Fixture: the releases layout, exactly as deploy.sh builds it on the box ---
|
|
work="$(mktemp -d)"
|
|
trap 'rm -rf "$work"' EXIT
|
|
|
|
mkdir -p "$work/releases/r1/.next" "$work/shared" "$work/releases/r1/node_modules/next/dist"
|
|
echo '// real entrypoint' > "$work/releases/r1/server.js"
|
|
# A decoy inside node_modules: the search must skip it, or a deploy would boot
|
|
# the framework's own server instead of the app's.
|
|
echo '// decoy' > "$work/releases/r1/node_modules/next/dist/server.js"
|
|
ln -s "$work/releases/r1" "$work/app"
|
|
|
|
# Render the template the way sync-infra.sh does, and install it the way the box
|
|
# has it: the real file in shared/, reached through a symlink in the release.
|
|
sed "s|__PORT__|4099|g" "$TMPL" > "$work/shared/launch.sh"
|
|
chmod +x "$work/shared/launch.sh"
|
|
ln -s "$work/shared/launch.sh" "$work/releases/r1/launch.sh"
|
|
|
|
# --- 1. Does it find server.js at all, invoked through the app symlink? ---
|
|
# Run the template with node/exec stubbed out so it reports what it WOULD run.
|
|
stub="$work/stub"; mkdir -p "$stub"
|
|
cat > "$stub/node" <<'STUB'
|
|
#!/usr/bin/env bash
|
|
echo "WOULD_RUN=$1"
|
|
echo "AI_APP_ID=${AI_APP_ID-}"
|
|
echo "AI_USAGE_DIR=${AI_USAGE_DIR-}"
|
|
STUB
|
|
chmod +x "$stub/node"
|
|
|
|
# The template hardcodes /usr/bin/node, so rewrite just that for the test.
|
|
sed 's|/usr/bin/node|node|' "$work/shared/launch.sh" > "$work/shared/launch-test.sh"
|
|
chmod +x "$work/shared/launch-test.sh"
|
|
ln -sf "$work/shared/launch-test.sh" "$work/releases/r1/launch-test.sh"
|
|
|
|
resolved="$(PATH="$stub:$PATH" bash "$work/app/launch-test.sh" 2>&1 || true)"
|
|
check "finds server.js through the app symlink" \
|
|
"WOULD_RUN=$work/releases/r1/server.js" \
|
|
"$(echo "$resolved" | grep '^WOULD_RUN=' || echo "$resolved")"
|
|
|
|
# --- 1b. The box's usage log: named after the app, only where the directory exists ---
|
|
check "names the app after its directory, for the usage log" \
|
|
"AI_APP_ID=$(basename "$work" | tr 'a-z-' 'A-Z_')" "$(grep '^AI_APP_ID=' <<<"$resolved")"
|
|
check "sets no usage directory where the box has none" \
|
|
"AI_USAGE_DIR=" "$(FLEET_USAGE_DIR="$work/none" PATH="$stub:$PATH" bash "$work/app/launch-test.sh" 2>&1 | grep '^AI_USAGE_DIR=')"
|
|
mkdir -p "$work/usage"
|
|
check "points every app at the box's usage directory when it exists" \
|
|
"AI_USAGE_DIR=$work/usage" "$(FLEET_USAGE_DIR="$work/usage" PATH="$stub:$PATH" bash "$work/app/launch-test.sh" 2>&1 | grep '^AI_USAGE_DIR=')"
|
|
printf 'AI_APP_ID=MINE\nAI_USAGE_DIR=/elsewhere\n' > "$work/releases/r1/.env"
|
|
check "an app's own .env wins for both" \
|
|
"AI_APP_ID=MINE AI_USAGE_DIR=/elsewhere" "$(FLEET_USAGE_DIR="$work/usage" PATH="$stub:$PATH" bash "$work/app/launch-test.sh" 2>&1 | grep -E '^AI_(APP_ID|USAGE_DIR)=' | tr '\n' ' ' | sed 's/ $//')"
|
|
rm -f "$work/releases/r1/.env"
|
|
|
|
# --- 2. Does it skip the node_modules decoy? ---
|
|
if grep -q 'node_modules' <<<"$resolved"; then
|
|
echo " ✗ picked a server.js inside node_modules" >&2
|
|
fails=$((fails + 1))
|
|
else
|
|
echo " ✓ skips node_modules"
|
|
fi
|
|
|
|
# --- 3. Is the port substituted? ---
|
|
check "substitutes __PORT__" "1" \
|
|
"$(grep -c 'PORT=4099' "$work/shared/launch.sh")"
|
|
check "leaves no __PORT__ placeholder" "0" \
|
|
"$(grep -c '__PORT__' "$work/shared/launch.sh" || true)"
|
|
|
|
# --- 4. The negative control: prove this test can actually fail. ---
|
|
# Build the same fixture with the pre-fix `pwd` and assert discovery BREAKS. If
|
|
# this ever passes, the test has stopped testing anything and every check above
|
|
# is decorative.
|
|
sed 's|&& pwd -P)|\&\& pwd)|; s|/usr/bin/node|node|' "$TMPL" \
|
|
| sed "s|__PORT__|4099|g" > "$work/shared/launch-broken.sh"
|
|
chmod +x "$work/shared/launch-broken.sh"
|
|
ln -sf "$work/shared/launch-broken.sh" "$work/releases/r1/launch-broken.sh"
|
|
broken="$(PATH="$stub:$PATH" bash "$work/app/launch-broken.sh" 2>&1 || true)"
|
|
|
|
if grep -q '^WOULD_RUN=' <<<"$broken"; then
|
|
echo " ✗ negative control: plain 'pwd' still found server.js —" >&2
|
|
echo " this test cannot detect the regression it exists for" >&2
|
|
fails=$((fails + 1))
|
|
else
|
|
echo " ✓ negative control: plain 'pwd' fails to find server.js"
|
|
fi
|
|
|
|
# --- 5. HOSTNAME: loopback by default, BIND_HOST in .env overrides it. ---
|
|
# Run the rendered script for real and read the HOSTNAME the server would be
|
|
# exec'd with — the stub node reports it. Solon 500'd for ~30 min (2026-09-25)
|
|
# on a hard-coded 127.0.0.1 that its .env could not override.
|
|
cat > "$stub/node" <<'STUB'
|
|
#!/usr/bin/env bash
|
|
echo "WOULD_RUN=$1"
|
|
echo "HOSTNAME_SEEN=$HOSTNAME"
|
|
STUB
|
|
chmod +x "$stub/node"
|
|
|
|
hostname_seen() { # $1 = launch script name inside the release
|
|
env -u BIND_HOST -u HOSTNAME PATH="$stub:$PATH" bash "$work/app/$1" 2>&1 \
|
|
| sed -n 's/^HOSTNAME_SEEN=//p'
|
|
}
|
|
|
|
rm -f "$work/releases/r1/.env"
|
|
check "no BIND_HOST -> HOSTNAME=127.0.0.1" "127.0.0.1" "$(hostname_seen launch-test.sh)"
|
|
|
|
# On the box release/.env is a symlink to shared/.env; mirror that.
|
|
printf 'BIND_HOST=0.0.0.0\n' > "$work/shared/.env"
|
|
ln -sf "$work/shared/.env" "$work/releases/r1/.env"
|
|
check "BIND_HOST=0.0.0.0 in .env -> HOSTNAME=0.0.0.0" "0.0.0.0" "$(hostname_seen launch-test.sh)"
|
|
|
|
# Negative control: the pre-fix hard-coded line must IGNORE the override, or
|
|
# the check above cannot tell the fix from the bug.
|
|
sed 's|HOSTNAME="${BIND_HOST:-127.0.0.1}"|HOSTNAME=127.0.0.1|; s|/usr/bin/node|node|' "$TMPL" \
|
|
| sed "s|__PORT__|4099|g" > "$work/shared/launch-hardcoded.sh"
|
|
ln -sf "$work/shared/launch-hardcoded.sh" "$work/releases/r1/launch-hardcoded.sh"
|
|
check "negative control: hard-coded HOSTNAME ignores BIND_HOST" "127.0.0.1" \
|
|
"$(hostname_seen launch-hardcoded.sh)"
|
|
rm -f "$work/releases/r1/.env" "$work/shared/.env"
|
|
|
|
# --- 6. Node's heap limit follows the unit's memory. ---
|
|
# The stub node reports the NODE_OPTIONS it would be started with. A fake
|
|
# cgroup tree stands in for the unit's: /proc/self/cgroup names the path, and
|
|
# memory.high / memory.max hold the unit's limits in bytes ("max" = none).
|
|
cat > "$stub/node" <<'STUB'
|
|
#!/usr/bin/env bash
|
|
echo "NODE_OPTIONS_SEEN=${NODE_OPTIONS-}"
|
|
STUB
|
|
chmod +x "$stub/node"
|
|
cg="$work/cgroup"; mkdir -p "$cg/system.slice/x-app.service"
|
|
printf '0::/system.slice/x-app.service\n' > "$work/proc-cgroup"
|
|
opts_seen() { # extra env assignments as arguments
|
|
env -u NODE_OPTIONS -u NODE_HEAP_MB LAUNCH_CGROUP_ROOT="$cg" LAUNCH_PROC_CGROUP="$work/proc-cgroup" "$@" \
|
|
PATH="$stub:$PATH" bash "$work/app/launch-test.sh" 2>&1 | sed -n 's/^NODE_OPTIONS_SEEN=//p'
|
|
}
|
|
limits() { printf '%s\n' "$1" > "$cg/system.slice/x-app.service/memory.high"; printf '%s\n' "$2" > "$cg/system.slice/x-app.service/memory.max"; }
|
|
|
|
limits 805306368 1073741824
|
|
check "MemoryHigh=768M, MemoryMax=1G -> a 499 MB heap (65 % of the lower)" \
|
|
"--max-old-space-size=499" "$(opts_seen)"
|
|
limits max 1073741824
|
|
check "no MemoryHigh -> 65 % of MemoryMax" "--max-old-space-size=665" "$(opts_seen)"
|
|
limits max max
|
|
check "a unit with no limit gets no flag" "" "$(opts_seen)"
|
|
limits 104857600 max
|
|
check "never below a heap an app can start in" "--max-old-space-size=256" "$(opts_seen)"
|
|
limits 805306368 1073741824
|
|
check "NODE_HEAP_MB is the app's own number" "--max-old-space-size=700" "$(opts_seen NODE_HEAP_MB=700)"
|
|
check "an app's own --max-old-space-size is left alone" "--max-old-space-size=900" \
|
|
"$(opts_seen NODE_OPTIONS=--max-old-space-size=900)"
|
|
check "other NODE_OPTIONS are kept" "--enable-source-maps --max-old-space-size=499" \
|
|
"$(opts_seen NODE_OPTIONS=--enable-source-maps)"
|
|
printf '12:memory:/x\n11:cpu:/x\n' > "$work/proc-cgroup"
|
|
check "no cgroup v2 line (an old kernel, a laptop) -> no flag, and it still starts" "" "$(opts_seen)"
|
|
|
|
if [ "$fails" -gt 0 ]; then
|
|
echo "launch.sh.tmpl: $fails check(s) FAILED" >&2
|
|
exit 1
|
|
fi
|
|
echo "launch.sh.tmpl: ok"
|