My test fixture leaked a Postgres volume on every run, and I wrote a comment saying it wouldn't
About a month ago I wrote a test fixture that starts a Postgres container for each test and removes it when the test ends. Every test that used it left one anonymous Docker/Podman volume on the machine. Nothing ever cleaned those volumes up. Nobody noticed until a disk-usage audit of a shared build machine found hundreds of orphaned volumes. We then checked other machines and found the same thing on several of them, more than a thousand volumes in total.
All the tests passed the whole time. The leak was only visible in the state of the machine, and nothing I wrote ever looked at that.
What I wrote
The fixture started a container like this:
let name = format!("test-postgres-{}-{nanos}", std::process::id());
Command::new("docker")
.args(["run", "--detach", "--rm", "--name", &name])
.args(["--env", &format!("POSTGRES_PASSWORD={PASSWORD}")])
.args(["--publish", "5432", IMAGE])
The teardown in Drop looked like this:
// A container left behind is removed by its `--rm` once it is
// stopped; a failure here leaves it for the runtime to reap.
let _ = Command::new("docker")
.args(["rm", "--force", container])
.output();
That comment is the bug. It’s my wrong assumption, written down as if it were a fact.
Why it leaked
- The official Postgres image declares
VOLUME /var/lib/postgresql/data. That means everydocker runof it creates an anonymous volume, whether you ask for one or not. I was thinking about the container and never considered what the container creates alongside itself. rm --forcewithout--volumesleaves that volume behind. Our machines run Podman behind thedockercommand. In Podman,rmremoves anonymous volumes only when you pass-v/--volumes.--rmdidn’t help.--rmcleans up only when the container exits on its own. MyDropforce-removed the container first, so that path never ran. I had reasoned about Docker’s documented behaviour and never checked the runtime that actually runs our tests.- A container per test multiplied the leak. Each container got a unique name from the pid and a timestamp, so nothing ever reused or reaped it. Similar fixtures elsewhere in our codebase start one container per crate and give each test its own database inside it. That’s faster, and here it would have kept the leak to one volume instead of one per test.
- Interrupted runs leaked too. If a fixture was dropped before
initdbfinished, it left an empty volume behind. We found dozens of those.
What I should have written
The fixture holds throwaway test data, and fsync is already off. It never needs a volume:
.args(["run", "--detach", "--rm", "--name", &name])
.args(["--tmpfs", "/var/lib/postgresql/data"])
And the teardown should remove everything the container owns, whether or not I expect a volume:
.args(["rm", "--force", "--volumes", container])
The frustrating part is that our local dev stack script already used rm --force --volumes. The correct version was in the same codebase when I wrote this, and I didn’t look for it.
What I’m changing
These are the rules I’ll follow for any fixture that starts a container, and what I’ll check for in review:
- A fixture owns everything its container creates.
docker runcan create more than a container: anonymous volumes from the image’sVOLUMEdeclarations, published ports, sometimes networks. Teardown removes all of it. --rmis not teardown. It only covers the normal-exit path. A fixture that force-removes its container, gets killed by a test timeout, or is dropped during a panic is not on that path. Always pass--volumestorm. It does nothing when there’s nothing to remove.- Test data goes on
--tmpfs. If a test container doesn’t need to keep anything after its run, mount its data directory on tmpfs so there’s no volume to leak. - A fixture tests its own cleanup. One test that starts the fixture, drops it, and asserts that no container, volume or network it created is left would have caught this the day I wrote it.
- Fix every similar fixture, not just the reported one. Several other crates run the same image in a similar way. Even the ones that start one container per crate still create an anonymous volume, so they all get
--tmpfsand--volumes. Anything left unchanged gets named, with a reason. - Check against the runtime that actually runs the code. Docker and Podman handle anonymous volumes differently. If code shells out to a container runtime, I verify its behaviour on the runtime our machines use, not the one in the docs I happened to read.
The bigger pattern
This isn’t a one-off. We’ve had the same kind of bug several times with git branches and worktrees. Someone, often an automated agent, creates a resource on a shared machine for the length of a task. They write cleanup for the happy path. They don’t account for resources created as a side effect, or for the exit paths where the happy-path cleanup never runs. The run that causes the leak can’t see it. It only shows up later at the machine level, and by then there are hundreds of them.
In every case the tests checked that the resource was created and used, and nothing checked what was left on the machine afterwards.
So now, for any code that creates a worktree, container, volume, temp directory, network or process on a shared machine, I’ll ask four questions:
- What does this create besides the thing it names?
- On which exit paths does it get removed?
- What will the count look like after a month of normal use?
- Which test proves nothing is left?
I didn’t ask any of those here. I wrote a comment that answered the second question wrong, and then I called the fixture done.