diff --git a/frontend/content/blog/visual-diff-oom-killed.md b/frontend/content/blog/visual-diff-oom-killed.md new file mode 100644 index 00000000..85cd11a4 --- /dev/null +++ b/frontend/content/blog/visual-diff-oom-killed.md @@ -0,0 +1,86 @@ +--- +title: "Our visual diff was OOM-killed three times, and the queue only said 'too many tries'" +description: A build of 578 screenshots failed in production with one line about a retry limit. Kubernetes had killed the worker three times, out of memory, because comparing one tall full-page screenshot held four copies of it at once. How we found the kills, why kubectl top missed the peak, and how comparing a row at a time took peak memory from 665 MiB to 82. +date: 2026-12-09T12:00:00+13:00 +author: Nathan Broadbent +draft: true +--- + +On 5 December a VisualCI build failed in production with this error, and nothing else: + +``` +builds.process has been attempted more than its 3 tries allow +``` + +There was no panic, no stack trace and no earlier error. The build had 578 screenshots, and it was our own: VisualCI's CI screenshots its own website on every pull request. + +## Finding the kills + +Sentry had one event, from the fourth try. The first three left nothing. A try that fails with an error records it before the job is retried, so three tries that left no error hadn't failed: they had been stopped from outside. + +The job's payload in the failed-jobs table gave the build's ID, and the build gave the time. Kubernetes had the rest. Both worker pods showed a last state of `OOMKilled`, exit code 137, a minute and a half apart, just before the job failed. Each try had started comparing the build's screenshots, run out of memory partway through and been killed. Nothing in the process got to write an error. The queue saw a job that never finished, handed it out again, and on the fourth pickup failed it on the try counter alone, before it did any work. + +So the only error we had was about the retry limit, and it was true. It just said nothing about why the tries had run out. + +## What used so much memory + +The workers had 512 MiB each. The build had 12 changed screenshots, all tall full-page PNGs: the landing page at 1440 × 7162, the changelog at about 1440 × 3800, and 2880-pixel-wide copies of some pages taken at twice the pixel density. + +We raised the limit to 1 GiB and retried. The build completed, its comparisons took 43.9 seconds, and the worker peaked at 714 MiB. `kubectl top` never showed anything near that: it samples every few seconds, and the peak lasted less. The cgroup's own counter, `memory.peak` in the container's cgroup directory, records the highest it has ever been, and that's where the 714 came from. To find a short peak, read `memory.peak`, not `top`. + +VisualCI compares one snapshot at a time, so the peak was one comparison. The code did what most image diffs do: + +1. Decode both images whole, and convert each to RGBA with a copy. +2. Build a full-size RGBA overlay, the image that marks the changed pixels, and two full-size maps of which pixels are masked and which changed. +3. Encode the overlay as a PNG. + +For a 2880 × 14000 page, one RGBA copy is 161 MB, and three or four full-size buffers were alive at once. Memory grew with the height of the page, and full-page screenshots get very tall. + +Two more things added to it. A later step that matches changes against ones already approved kept every decoded image for the whole build. And comparison work runs on several threads at once, and glibc gives each thread its own memory arena. For a 1440-wide page the two full-size maps are about 10 MB each, under glibc's threshold for giving a large allocation its own mapping, so each thread's arena kept its own copies. In our harness, eight comparisons of a 1440 × 7162 page on eight threads peaked at 368 MiB. With `MALLOC_ARENA_MAX=2`, which caps the number of arenas, the same eight peaked at 190. + +## A row at a time + +A larger limit would only move the line: a taller page would cross it again. The fix ([#302](https://github.com/DocSpring/visualci/pull/302)) makes memory stop growing with the page's height. + +- **Decode a row at a time.** Browser screenshots are 8-bit, non-interlaced PNGs, which the png crate can decode one row at a time. Anything else (JPEG, WebP, 16-bit or interlaced PNGs) is still decoded whole, one image at a time across the process. +- **Diff through a five-row window.** The test for anti-aliased pixels looks two rows up and down, so the diff keeps five rows of each image. In place of the two full-size maps, it keeps one map of which 16-pixel tiles changed. +- **Encode the overlay as it goes.** Each overlay row goes straight to the PNG encoder. + +The last one needed care. The png crate's streaming writer gave the same pixels with different bytes, and every stored diff would have changed its SHA-256. So the new encoder reproduces the old one exactly: the same adaptive filter for each row, the same compressor fed one row at a time, and the same fallback when compressing doesn't pay. + +To check it, we replayed 55 real pages from four production builds through the old code and the new: the build that failed, a later one with the changelog and docs pages, and two from another project, browser screenshots and PDF pages. Each page ran in two comparison modes, 110 results in all. Every one matched in every field, down to the overlay PNG's SHA-256. All 53 pages that had a stored overlay reproduced it byte for byte. + +## The numbers + +Peak memory, measured as the process's high-water mark (`VmHWM`) by a harness that runs the server's comparison code: + +| | Before | After | +|---|---|---| +| A 2880 × 14000 page, 4 comparisons | 665 MiB | 82 MiB | +| A 1440 × 7162 page, 8 comparisons | 368 MiB | 36 MiB | +| The real landing page from the failed build, 8 comparisons | 421 MiB | 14 MiB | +| The real landing page, 1 comparison | 153 MiB | 7 MiB | + +A 2880 × 14000 comparison also got faster, from about 1.1 seconds to 0.9. Of the 82 MiB left in the first row, 58 MB is the two input PNGs held in memory (that synthetic page compresses badly) and 21 MB is the overlay PNG the comparison returns. The diff itself holds a few rows. + +With the fix, the arena cap barely matters (36 MiB against 24), so we haven't set it. We tried jemalloc too: after the fix it was worse than glibc in every case we measured, so we kept glibc. + +A test keeps it that way. It compares a 1440 × 20000 change under a counting allocator and fails if the peak passes 8 MiB plus twice the overlay's size. The peak today is 1.9 MiB. One RGBA copy of that page is 110 MiB, so a full-size buffer coming back fails the test at once. + +## Killed tries now say so + +The other half of the fix is the error. Each try now writes a mark while it holds the build's processing lock and clears it when it ends, however it ends: finished, waiting for renders, stopped for a deploy, or failed. A try that takes the lock and finds a mark knows the one before it died, because a live try would still hold the lock. After three deaths the build and the job both fail with: + +``` +the worker ran out of memory (or was killed) processing build #N, 3 times +``` + +A try cut off by the job's timeout or a caught panic doesn't count, because its mark is cleared when the try is dropped. Only a process that dies outright leaves the mark behind. + +## What we'd tell ourselves + +- **"Too many tries" can hide a kill.** When a job's earlier tries left no error, look at how the process ended (`OOMKilled`, exit 137) before the job's own logs. +- **Diff memory grows with page height.** A full-page screenshot of a long page can be 7,000 pixels tall or more, and at twice the pixel density a 2880 × 14000 page is 161 MB per RGBA copy. Code that holds a few whole copies is fine for 1280 × 800 and fails here. +- **Measure peaks with `memory.peak`.** Sampling tools miss a peak that lasts less than their interval. +- **Check arenas before you switch allocators.** For us, capping glibc's arenas halved the peak before the fix, and jemalloc made it worse after. +- **Prove a rewrite with real data.** Replaying production comparisons through the old and new code, byte for byte, is what let us ship a new decoder and encoder without changing a single stored diff.