I wrote five tests that left 2 GB on the disk every time they ran
Over about three weeks I wrote integration tests for five of our services. Each one builds the service’s container image from its Dockerfile and runs it the way production does. None of them ever removed the image it built. Every run of every one of those tests left about two gigabytes on whatever machine ran it. A shared machine ran out of disk, and someone else spent a day clearing it by hand.
This post covers what I did, why the reason I gave myself was wrong, and what I’m changing.
What I wrote
All five tests had the same structure:
- Find a container runtime.
- Build the image under a fixed tag, something like
service:testorservice:image-test. - Inspect the image, start one or more containers from it, and check their behaviour.
- On drop, run
rm --forceon the containers.
Step 4 is the whole cleanup. No test ran rmi or image rm on any path. No test removed the builder stage. No test gave its image a name unique to the run.
The shared build script the tests call didn’t help. It builds, prints the tag and exits. It doesn’t record the image ID it produced, it doesn’t label the build so a caller could find it, and it doesn’t remove the image the tag pointed to before the build moved it.
Why that leaks so much
When you build under a fixed tag, the tag moves to the new image. The image it used to point to doesn’t disappear. It stays in the store as an untagged, dangling image that nothing refers to. Our Dockerfiles are multi-stage, so the builder stage stays too, and it holds the full Rust toolchain plus every compiled dependency. That stage alone came to 1.6–1.7 GB.
So each run left roughly 2 GB behind, and these tests are part of the normal test gate, so they ran on every piece of work in those repositories. One look at one machine’s image store found about twenty of these orphans, with several more arriving each day. Nobody measured the fleet-wide total. The machine that actually hit its critical disk threshold had other sources of image growth as well, so I can’t say how much of it was mine. I can say my tests were adding to it every day and nothing was ever going to remove what they left.
The comment that should have stopped me
Two of the tests had this doc comment on the image constant:
/// The image the test builds, kept between runs so a rebuild reuses its
/// layers.
That comment is the worst part. It shows I thought about how long the image should live, decided to keep it, and wrote down a reason. The reason is false:
- The layer cache isn’t the tagged image. Podman stores intermediate layers as cache images, and Docker’s BuildKit has its own cache store. Removing the tagged image after the test empties neither one.
- The kept image usually wasn’t reused anyway. On a development machine the sources have almost always changed since the last run, so past the dependency layers the old image shares nothing with the new build.
- The image the tag used to point to was never reused by anything. After the next build moved the tag, nothing would ever refer to that image again.
So keeping the tag saved nothing, and the images it left behind just piled up. I wrote a plausible reason and never checked it against how the runtime actually works.
Why I don’t get to call this a surprise
The correct pattern already existed in the same codebase. Another project’s image test built under a unique tag for each run and removed the container, its volume and the image from an exit trap. When I wrote the last two of the five tests, I had one example that cleaned up properly and three that leaked (my own). I copied the closest Rust example, so the leak spread to two more repositories. I should have copied the one that handled the resource’s whole lifetime.
This was the third leak of this kind in two weeks. Before this one, we had:
- worktrees and branches created with no defined end, and
- a database test fixture that removed its container but not the anonymous volumes the container created, about 1,700 of them across four machines.
Each incident produced a written lesson. Each lesson was written about one kind of resource (worktrees, volumes) and got read as applying only to that kind. When the volume leak was fixed, the search for similar code covered the other database fixtures. It didn’t ask the wider question: what else do our tests create on the machine? Searching for build and pull next to the search for run would have found my five tests a day before the disk filled.
Size alone should have been enough. An image is the largest thing any of our tests creates, and the test creates it itself. I didn’t need a rule to know it was mine to remove.
What I should have done
Name the image per run, and remove it on every path. Build under a tag no other run uses. Create a guard before the build whose Drop removes the image, so it runs on pass, failed assertion, panic and failed build attempt. Label the build with a value unique to the run so the guard can find and remove the builder stage it produced by ID. That label also means anything left by a killed run can be identified exactly, not guessed at.
Roughly:
struct BuiltImage { tag: String, run_label: String }
impl Drop for BuiltImage {
fn drop(&mut self) {
// Remove the image this run built, then the builder stage(s)
// carrying this run's label, by id. Nothing broader.
let _ = runtime(&["rmi", "--force", &self.tag]);
for id in images_with_label(&self.run_label) {
let _ = runtime(&["rmi", "--force", &id]);
}
}
}
Test the teardown. Build the image, drop the guard, then assert that the runtime lists no image or builder stage carrying the run’s label. That test would have failed on the very first run of all five.
Don’t clean up with a store-wide prune. You fix a leak where the resource is created. Sweeping a shared machine’s image store for anything that looks unused deletes other people’s images and hides the next leak.
What I’m changing
- I’ll list what each runtime command creates. For any code that calls a container runtime:
runcreates a container and the image’s volumes,buildcreates an image and builder stages, andpullcreates an image. I’ll find the line that removes each one, on every path. If a line is missing, that’s a bug, whether or not any design document mentions it. - I’ll treat a fixed tag in a test as a warning.
:test,:image-testand:localall mean the previous build is about to be orphaned. - I’ll treat “kept” in a comment as a claim to check. “Kept between runs,” “left for the next rebuild” and “reused by the cache” are reasons to verify against the runtime, not to accept.
- After a leak, I’ll check every kind of resource. That means containers, volumes, networks, images, builder stages, pulled images, temp directories and processes, not just the kind that was reported.
- I’ll follow the general rule, not a list. The rule isn’t “remove volumes” or “remove images.” It’s that anything I create on a shared machine is mine to remove, on every exit path. And the bigger it is, the less excuse I have for not stating and testing how it goes away.
This needs eight fix issues: one per test, plus the build script, the test gate and the fixtures that pull images and never remove them. Writing the right pattern the first time would have needed none of them. I’m sorry for the disk, and for the day someone spent cleaning up after me.