From 04e3a5249d13548cdc6c2fd6cafe00cc48696e8e Mon Sep 17 00:00:00 2001 From: Timothy Date: Fri, 17 Jul 2026 15:36:18 +0200 Subject: [PATCH] ci(406): disable swap on the capped mysql; fix peak-step placement and failure mode [decisions-edit] Review fixes from the cold adversarial pass on PR #411. Verdict was MERGEABLE, but one HIGH is a genuine defect worth the round-trip. [decisions-edit] rationale: the 12 "modified" lines in docs/decisions.md are lines THIS PR added in its own previous commit (e33205e8), not a settled entry on main. The hook diffs against HEAD; net against origin/main this file is still a pure insertion (65 added, 0 deleted -- verified, and CI's decisions-guard diffs against the PR base). It is also the sanctioned reason regardless: the entry as first written contained a factual error (see HIGH below). HIGH -- `--memory=2g` alone silently grants 2g of SWAP as well. Docker defaults an unset --memory-swap to 2x --memory. Verified live on bumblebee: `--memory=2g` alone gives memory.max=2147483648 AND memory.swap.max=2147483648; adding `--memory-swap=2g` gives memory.swap.max=0. Capping RAM while permitting swap is close to the worst outcome on the one host whose swap thrash is the entire reason for the cap -- and a swapping mysqld mid-DDL is exactly the pathology behind the known `Command Timeout expired` migrations flake, so the naive cap could have made that flake worse rather than better. The comment promising "if this ever OOM-kills the service" described a failure mode that would not have happened first: it would have swapped, silently. Now --memory-swap=2g (swap off); prefer a loud OOM over silent degradation. The same 2x applies to the runners' 10g job slots (10 GiB RAM + 10 GiB swap each), which is a plausible direct mechanism for the incident's 21 GiB swapped. That is #604's boundary -- reporting it there. MEDIUM -- `if: always()` does not make a step advisory. It controls whether the step RUNS, not whether its failure fails the job, and defaults.run.shell: bash means -e is on, so a failed cat/redirect in an informational step could redden a green test job. Use continue-on-error: true, matching the neighbouring Coverage summary step. MEDIUM -- the peak step must run LAST. memory.peak read at step N reports the peak only up to N, so sitting before Coverage summary excluded reportgenerator's memory -- under- reporting the very number #604 sizes caps on. Moved after it. LOW -- dependency-scan.yml gets the env too; workflow env does not cross workflow files, and that one runs unattended on a cron against the prod media host. NIT -- Dockerfile ENV moved below the openapi-generator wget: an ENV invalidates every layer under it, and that ~30MB download is deliberately ordered early to stay cached (#190). Nothing between it and restore compiles, so this placement is free. NIT -- docs no longer imply this shrinks `format`. dotnet format loads Roslyn in-process via MSBuildWorkspace and never spawns csc, so its 3.95 GiB is untouched; sizing the small lane on the assumption it shrank would be a trap. Also record honestly that 543 MiB is init+idle rather than the 787-migration replay (so 2g is a measured floor + headroom, not a measured ceiling -- the migrations job going green is what validates it) and that --cpus=2 is judgement with no measurement behind it. The entry criticises dressing estimates as measurements; it should not do that itself. Co-Authored-By: Claude Opus 4.8 (1M context) --- .gitea/workflows/dependency-scan.yml | 11 ++++ .gitea/workflows/docker-build.yml | 81 ++++++++++++++++++---------- docker/Dockerfile | 31 ++++++----- docs/ci-cd.md | 47 +++++++++++++--- docs/decisions.md | 51 +++++++++++++----- 5 files changed, 162 insertions(+), 59 deletions(-) diff --git a/.gitea/workflows/dependency-scan.yml b/.gitea/workflows/dependency-scan.yml index c2c68ab29..a971293e5 100644 --- a/.gitea/workflows/dependency-scan.yml +++ b/.gitea/workflows/dependency-scan.yml @@ -22,6 +22,17 @@ concurrency: group: ersatztv-depscan cancel-in-progress: true +# No persistent MSBuild/Roslyn servers (ersatztv#406). Workflow `env:` does not cross workflow +# files, so docker-build.yml's copy of these does not apply here and this has to be repeated. +# Smaller stakes than the build pipeline — `dotnet restore` + `dotnet list` are MSBuild-driven and +# never invoke csc, so this is lingering worker nodes (hundreds of MiB), not a 7.8 GB VBCSCompiler. +# Worth setting anyway: this runs unattended on a Monday 06:00 cron against the same host that runs +# prod media, and node reuse keeps workers alive ~15 min after the job. +env: + UseSharedCompilation: "false" + DOTNET_CLI_USE_MSBUILD_SERVER: "0" + MSBUILDDISABLENODEREUSE: "1" + jobs: scan: name: NuGet vulnerable packages diff --git a/.gitea/workflows/docker-build.yml b/.gitea/workflows/docker-build.yml index c9b933263..8b17aea0d 100644 --- a/.gitea/workflows/docker-build.yml +++ b/.gitea/workflows/docker-build.yml @@ -151,30 +151,6 @@ jobs: dotnet test --configuration Release --no-build --blame-hang-timeout "2m" --verbosity normal --collect:"XPlat Code Coverage" --settings coverlet.runsettings --results-directory ./coverage - # Peak memory of THIS job container, reported every run (ersatztv#406, server-management#604). - # #604 sizes the runners' per-job caps on this number, and until now it was inherited rather - # than measured: the 10g cap traces back to server-management#570 observing the image build - # peg 5.999/6 GiB, which is a different job entirely. `memory.peak` is the cgroup's - # high-water mark since the container started, so reading it once at the end of the job is - # exactly the peak — no polling needed. cgroup v2 first, v1 fallback; purely informational, - # so it must never fail a build (`always()` + tolerated absence). - - name: Report peak container memory - if: always() - run: | - for f in /sys/fs/cgroup/memory.peak /sys/fs/cgroup/memory/memory.max_usage_in_bytes; do - if [ -r "$f" ]; then - bytes=$(cat "$f") - printf 'peak container memory: %s MiB (%s bytes, from %s)\n' \ - "$(( bytes / 1048576 ))" "$bytes" "$f" - if [ -n "${GITHUB_STEP_SUMMARY:-}" ]; then - printf '**Peak container memory (test job):** %s MiB\n' \ - "$(( bytes / 1048576 ))" >> "$GITHUB_STEP_SUMMARY" - fi - exit 0 - fi - done - echo "No cgroup peak-memory file readable in this container -- skipping." - # Coverage reporting (ersatztv#15 scope item 4): coverlet.collector emits a Cobertura report # per test project (via --collect above); ReportGenerator merges them into a human-readable # summary printed to the log and the job step summary. No floor is enforced yet ("decide on a @@ -204,6 +180,38 @@ jobs: cat coverage/report/SummaryGithub.md >> "$GITHUB_STEP_SUMMARY" fi + # Peak memory of THIS job container, reported every run (ersatztv#406, server-management#604). + # #604 sizes the runners' per-job caps on this number, and until now it was inherited rather + # than measured: the 10g cap traces back to server-management#570 observing the image build + # peg 5.999/6 GiB, which is a different job entirely. `memory.peak` is the cgroup's + # high-water mark since the container started, so reading it once is exactly the peak — no + # polling needed. It must therefore run LAST: `memory.peak` read at step N only reports the + # peak up to N, and this sits after Coverage summary so reportgenerator's own memory (the + # job's last real workload) is counted. #604 sizes caps on this number; under-reporting it + # would size them too low. cgroup v2 first, v1 fallback; purely informational, + # so it must never fail a build (`always()` + tolerated absence). + - name: Report peak container memory + # `always()` controls whether this step RUNS, not whether its failure fails the job — and + # `defaults.run.shell: bash` means `-e -o pipefail` is on, so a failed `cat`/redirect here + # would redden a green test job. `continue-on-error` is what actually makes it advisory, + # the same guarantee the Coverage summary step above uses. + if: always() + continue-on-error: true + run: | + for f in /sys/fs/cgroup/memory.peak /sys/fs/cgroup/memory/memory.max_usage_in_bytes; do + if [ -r "$f" ]; then + bytes=$(cat "$f") + printf 'peak container memory: %s MiB (%s bytes, from %s)\n' \ + "$(( bytes / 1048576 ))" "$bytes" "$f" + if [ -n "${GITHUB_STEP_SUMMARY:-}" ]; then + printf '**Peak container memory (test job):** %s MiB\n' \ + "$(( bytes / 1048576 ))" >> "$GITHUB_STEP_SUMMARY" + fi + exit 0 + fi + done + echo "No cgroup peak-memory file readable in this container -- skipping." + migrations: name: EF migration integrity (SQLite + MySql) runs-on: ubuntu-latest @@ -231,13 +239,28 @@ jobs: # service reported `mem=0 nanocpus=0`, i.e. unbounded. So every migrations run was adding # an uncapped MySQL to an already-tight host (ersatztv#406, server-management#604). # - # 2g is sized on measurement, not inheritance: a mysql:8.4 container with this exact env - # peaked at 543 MiB during init and settled at 481 MiB idle (probed on bumblebee - # 2026-07-17). Applying the migrations is DDL-heavy rather than data-heavy and the InnoDB - # buffer pool defaults to 128M, so 2g leaves ~4x headroom. If this ever OOM-kills the - # service, raise it deliberately — do not remove the cap. + # `--memory-swap=2g` is NOT redundant with `--memory=2g` — it is the point. Docker defaults + # an unset `--memory-swap` to *twice* `--memory`, so `--memory=2g` alone would grant 2g RAM + # **plus 2g of swap** (verified on bumblebee: `--memory=2g` alone → memory.max=2147483648 + # AND memory.swap.max=2147483648; with `--memory-swap=2g` → memory.swap.max=0). Setting it + # equal to --memory disables swap for this container. That matters more here than anywhere: + # swap thrash on this host is the whole reason this cap exists, and a swapping mysqld mid-DDL + # is precisely the pathology behind the known `Command Timeout expired` migrations flake. We + # want a loud OOM over silent swapping — an OOM is a clear signal to raise the cap. + # + # 2g is sized on measurement rather than inheritance, but honestly: a mysql:8.4 container + # with this exact env peaked at 543 MiB during init and settled at 481 MiB idle (probed on + # bumblebee 2026-07-17). That is init+idle, NOT the 787-migration replay, which grows caches + # idle never touches — so treat 2g as a measured floor with headroom, not a measured + # ceiling. The migrations job going green is what validates it. If this OOM-kills the + # service, raise it deliberately — do not remove the cap, and do not re-enable swap. + # + # `--cpus=2` is a ceiling, not a reservation, and is the one number here with no measurement + # behind it: 787 sequential DDL statements on one connection are ~1-core-bound, so 2 is + # judgement. Revisit if the apply step's tail latency grows. options: >- --memory=2g + --memory-swap=2g --cpus=2 --health-cmd="mysqladmin ping -h 127.0.0.1 -uroot -persatztv --silent" --health-interval=5s diff --git a/docker/Dockerfile b/docker/Dockerfile index 8cf8395de..1cefcf67f 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -21,18 +21,6 @@ RUN apt-get update && \ # https://hub.docker.com/_/microsoft-dotnet FROM mcr.microsoft.com/dotnet/sdk:10.0-noble-amd64 AS build - -# Disable the persistent Roslyn/MSBuild compiler servers for the in-image build (ersatztv#406, -# server-management#604). The workflow sets these as env for the runner-side dotnet jobs, but this -# stage compiles inside `docker build`, so the workflow's env does NOT reach it — the restore and -# the two publishes below would otherwise spin up their own VBCSCompiler and hold its heap. This -# is the `build` job that server-management#570 measured pegging 5.999/6 GiB. -# Build-stage only: the final image is FROM runtime-base, so none of this lands in the shipped -# image or affects runtime. -ENV UseSharedCompilation=false \ - DOTNET_CLI_USE_MSBUILD_SERVER=0 \ - MSBUILDDISABLENODEREUSE=1 - RUN apt-get update && apt-get install -y ca-certificates gnupg default-jre-headless python3-pip WORKDIR /source @@ -68,6 +56,25 @@ COPY ErsatzTV.Infrastructure/*.csproj ./ErsatzTV.Infrastructure/ COPY ErsatzTV.Infrastructure.Sqlite/*.csproj ./ErsatzTV.Infrastructure.Sqlite/ COPY ErsatzTV.Infrastructure.MySql/*.csproj ./ErsatzTV.Infrastructure.MySql/ COPY ErsatzTV.Scanner/*.csproj ./ErsatzTV.Scanner/ + +# Disable the persistent Roslyn/MSBuild compiler servers for the in-image build (ersatztv#406, +# server-management#604). The workflow sets these as env for the runner-side dotnet jobs, but this +# stage compiles inside `docker build`, so the workflow's env does NOT reach it — the restore and +# the two publishes below would otherwise spin up their own VBCSCompiler and hold its heap. This is +# the `build` job that server-management#570 measured pegging 5.999/6 GiB, so it is the one that +# most needs this. +# +# Placed here rather than at the top of the stage on purpose: an ENV invalidates every layer below +# it, and the wget of the ~30MB openapi-generator jar above is deliberately ordered early to stay +# cached (ersatztv#190). Nothing between that wget and this line compiles, so this is the earliest +# point where the ENV is free. +# +# Build-stage only: the final image is FROM runtime-base and only COPY --from=build /app (files, +# not ENV), so none of this lands in the shipped image or affects runtime. +ENV UseSharedCompilation=false \ + DOTNET_CLI_USE_MSBUILD_SERVER=0 \ + MSBUILDDISABLENODEREUSE=1 + RUN dotnet restore -r linux-x64 ErsatzTV/ErsatzTV.csproj # copy everything else and build app diff --git a/docs/ci-cd.md b/docs/ci-cd.md index c305e5f0a..f35ccf3d5 100644 --- a/docs/ci-cd.md +++ b/docs/ci-cd.md @@ -139,7 +139,17 @@ So the workflow's top-level `env:` disables the servers for every runner-side do These are MSBuild properties set as environment variables so they apply to every `dotnet` invocation without touching each call site (MSBuild surfaces env vars as properties, and -`UseSharedCompilation` is only defaulted to `true` when empty, so the env var wins). +`UseSharedCompilation` is only defaulted to `true` when empty, so the env var wins). Setting +`UseSharedCompilation` also disables the Razor build server, which falls back to it. + +**What this does and does not shrink.** It helps the jobs that *compile* — `test`, `migrations`, +`api-docs` on an API-touching PR, and the in-Docker `build`. It does **not** help `format`: `dotnet +format` loads Roslyn in-process via MSBuildWorkspace and never spawns `csc`, so its measured 3.95 +GiB is unaffected. Don't size the `small` lane expecting `format` to have shrunk — it hasn't. + +The same three are repeated in `dependency-scan.yml`; workflow `env:` does not cross workflow files. +That one is lower-stakes (restore/list are MSBuild-driven, so it's lingering worker nodes rather +than a 7.8 GB VBCSCompiler) but it runs unattended on a cron against the prod media host. **The workflow `env:` does not reach the `build` job's compilation**, which happens inside `docker build` — the same three are set as `ENV` in the SDK stage of `docker/Dockerfile`. That is @@ -158,11 +168,36 @@ to `services:`**. Verified on a live `migrations` job: the job container reporte `HostConfig.Memory=10737418240`; its `mysql:8.4` service reported `mem=0 nanocpus=0` — unbounded. So `migrations` runs added an uncapped MySQL to an already-tight host. -The mysql service now sets `--memory=2g --cpus=2`, sized on measurement rather than inheritance: a -`mysql:8.4` container with this exact env peaked at **543 MiB** during init and settled at 481 MiB -idle (probed on bumblebee, 2026-07-17). Applying the migrations is DDL-heavy rather than -data-heavy and the InnoDB buffer pool defaults to 128M, so 2g leaves ~4× headroom. **Any new -`services:` container needs its own explicit cap** — it will not inherit one. +The mysql service now sets `--memory=2g --memory-swap=2g --cpus=2`. + +**`--memory-swap` is the part that matters, and it is easy to get wrong.** Docker defaults an unset +`--memory-swap` to **twice** `--memory`, so `--memory=2g` *alone* grants 2g RAM **plus 2g of swap**. +Verified on bumblebee: + +| Options | `memory.max` | `memory.swap.max` | +|---|---|---| +| `--memory=2g` | 2147483648 | **2147483648** ← 2 GiB of swap | +| `--memory=2g --memory-swap=2g` | 2147483648 | **0** ← swap disabled | + +Setting `--memory-swap` equal to `--memory` disables swap for the container. On this host that is +the whole point: swap thrash is what took prod down, and a swapping mysqld mid-DDL is precisely the +pathology behind the known `Command Timeout expired` migrations flake. **Prefer a loud OOM over +silent swapping** — an OOM is a clear signal to raise the cap; swapping just degrades everything. + +⚠️ **The same 2× applies to the runners' `container.options: --memory=10g`** — each job slot is +really 10 GiB RAM *plus* 10 GiB swap. The "6 slots × 10 GiB = 60 GiB on a 25 GiB host" framing +understates the promise by 2×, and it is a plausible direct mechanism for the incident's 21 GiB of +swap. Fixing that is server-management#604's call (reported there). + +**On the 2g figure, honestly:** a `mysql:8.4` container with this exact env peaked at **543 MiB** +during init and settled at 481 MiB idle (probed on bumblebee, 2026-07-17) — but that is init+idle, +**not** the 787-migration replay, which grows table/definition caches idle never touches. So 2g is a +measured *floor* plus headroom, not a measured ceiling; the `migrations` job going green is what +validates it. `--cpus=2` has no measurement behind it at all — 787 sequential DDL statements on one +connection are ~1-core-bound, so it is judgement; revisit if the apply step's tail latency grows. + +**Any new `services:` container needs its own explicit cap** — it will not inherit one, and it needs +`--memory-swap` set alongside `--memory` or it silently gets 2× in swap. ### `test` job diff --git a/docs/decisions.md b/docs/decisions.md index 25f995885..6f72e7d11 100644 --- a/docs/decisions.md +++ b/docs/decisions.md @@ -1264,11 +1264,30 @@ spike forces is what starves the lanes. (`--cpus=4 --memory=10g`) applies to the **job container only**. Verified by inspecting a live `migrations` job: the job container reported `HostConfig.Memory=10737418240`, its `mysql:8.4` service reported `mem=0 nanocpus=0` — **unbounded**. Every migrations run was adding an uncapped -MySQL to an already-tight host. Now `--memory=2g --cpus=2`, sized on **measurement, not -inheritance**: a `mysql:8.4` container with this exact env peaked at **543 MiB** during init and -settled at 481 MiB idle (probed on bumblebee). The migrations workload is DDL-heavy rather than -data-heavy and the InnoDB buffer pool defaults to 128M, so 2g is ~4× headroom. **Standing rule: any -new `services:` container needs its own explicit cap** — it will not inherit one. +MySQL to an already-tight host. Now `--memory=2g --memory-swap=2g --cpus=2`. + +**Why `--memory-swap` is not redundant** (cold-review catch, and the sharpest thing in this change): +Docker defaults an unset `--memory-swap` to **twice** `--memory`, so `--memory=2g` alone grants 2g +RAM **plus 2g of swap** — verified live: `--memory=2g` → `memory.max=2147483648` **and** +`memory.swap.max=2147483648`; `--memory=2g --memory-swap=2g` → `memory.swap.max=0`. Capping RAM +while silently permitting swap is close to the worst outcome **on the host whose swap thrash is the +entire reason for the cap**, and a swapping mysqld mid-DDL is exactly the pathology behind the known +`Command Timeout expired` migrations flake — i.e. the naive cap could have made that flake worse. +**Standing rule: prefer a loud OOM over silent swapping.** An OOM is an unambiguous "raise the cap" +signal; swapping just degrades everything and blames something else. + +**The same 2× applies to the runners' 10g job slots** — each is really 10 GiB RAM *plus* 10 GiB +swap, so the "60 GiB promise on a 25 GiB host" understates by 2× and is a plausible direct mechanism +for the incident's 21 GiB swapped. That is server-management#604's boundary; reported there. + +**On 2g, honestly**: 543 MiB is init+idle, **not** the 787-migration replay (which grows caches idle +never touches), so 2g is a measured *floor* plus headroom, not a measured ceiling — the `migrations` +job going green is what validates it. `--cpus=2` has no measurement behind it at all; 787 sequential +DDL statements on one connection are ~1-core-bound, so it is judgement. Recording that rather than +dressing a guess as measurement — the same failure this entry criticises below. + +**Standing rule: any new `services:` container needs its own explicit cap** — it will not inherit +one, and `--memory` without `--memory-swap` silently grants 2× in swap. **3. #390's `small`-lane move for `api-docs`/`format` reversed.** #390 moved them to dodge a ~29 min `ubuntu-latest` queue. The queue was real, but the lane was the wrong fix, and **#390's own comment @@ -1280,13 +1299,21 @@ are not small jobs: a live `docker stats` caught the `format` job container at * (48 GiB ci-runner at capacity 4 + a bumblebee overflow slot) and fixes the queue at the source. This one is **order-coupled with #604**: the small lane's caps can't tighten until it lands. -**Measurement is now continuous, not a one-off.** The `test` job ends with a **Report peak container -memory** step reading the cgroup's `memory.peak` high-water mark (`always()`, tolerates absence, -never fails a build). #604 sizes both runners' caps on that number, and until now it was *inherited* -rather than measured — the 10g cap traces back to #570 observing a different job entirely. The -lesson generalizes: this repo's CI perf issues keep stating numbers from plausibility rather than -measurement (see #390's "2–4min" apt-ffmpeg estimate; real 110s, and not load-bearing). Read the -peak off a recent run instead of re-deriving it by hand. +**Measurement is now continuous, not a one-off.** The `test` job's **last** step reports the +cgroup's `memory.peak` high-water mark (`continue-on-error`, tolerates absence). #604 sizes both +runners' caps on that number, and until now it was *inherited* rather than measured — the 10g cap +traces back to #570 observing a different job entirely. Two things that look like details but are +the whole point: it must run **last** (`memory.peak` read at step N reports the peak only up to N, +so an earlier placement silently excludes the job's later workload and would size the cap too low), +and `continue-on-error` — not `if: always()` — is what makes it advisory (`always()` controls +whether a step *runs*, not whether its failure fails the job, and `defaults.run.shell: bash` means +`-e` is on). The lesson generalizes: this repo's CI perf issues keep stating numbers from +plausibility rather than measurement (see #390's "2–4min" apt-ffmpeg estimate; real 110s, and not +load-bearing). Read the peak off a recent run instead of re-deriving it by hand. + +**What this does NOT shrink**: `format`. `dotnet format` loads Roslyn in-process via +MSBuildWorkspace and never spawns `csc`, so its measured 3.95 GiB is untouched by any of this. Do +not size the `small` lane expecting otherwise. **Not addressed here**: the 12–35 min queue waits (server-management#604) and the redundant triple-build (#398).