absurdum reductus

The Command Returned 0 and Deleted 800 Gigabytes

The command returned 0. It had done nothing. The next line deleted around 800 gigabytes out from under a running daemon, and for the following hours every dashboard said the node was healthy. Nothing in that exchange was a lie. The script was just listening to a number instead of to the words.


A procedure that had just worked#

We were switching the container runtime’s snapshotter on a set of Kubernetes nodes, one at a time. The procedure was short: stop the runtime, delete its state directory, start it again with the new setting. It had been run on one node earlier that night and had gone cleanly. The node came back, the pods came back, the numbers were right.

The next node was different in one way that nobody had written down. It was a worker joined to the cluster, not one of the control-plane nodes. Same distribution, same binary, same command. Different answer.

The command that refused and said yes#

On a worker-joined node, this distribution’s stop command does not stop anything. It prints two lines and exits. The wrapper script, from the project’s own source:

if [ -e ${SNAP_DATA}/var/lock/clustered.lock ]
then
    echo "This MicroK8s deployment is acting as a node in a cluster."
    echo "Use 'snap stop microk8s' to stop services on this node."
    exit 0
fi

That is a courteous refusal. It names the problem and the right command to use instead. It also exits 0, which is the one thing a script can see. Ours read the 0 as “stopped” and ran rm -rf against the runtime’s state directory while the runtime was still running.

We are not going to argue with the wrapper. A human reading those two lines would have stopped. The script was never going to read them, and we had not told it to check anything else.

It is a hell of a thing, deleting a file from under a process#

Deleting a file does not take it away from a process that has it open. You take away its name and everything it was ever going to have, and the process does not notice. It keeps its descriptor, the kernel keeps the blocks, and reads keep working until the last descriptor closes.

#!/bin/sh
# A daemon stand-in holds a file open. We delete the file. Then we look.
dd if=/dev/zero of=store.bin bs=1M count=64 status=none
sleep 300 3< store.bin &
pid=$!
echo "before rm: $(stat -c %s store.bin) bytes, link count $(stat -c %h store.bin)"
rm -f store.bin
echo "after rm:  $(ls store.bin 2>&1)"
echo "the process still holds it:"
ls -l /proc/$pid/fd/3 | sed 's/.*-> //'
echo "and can still read all $(wc -c < /proc/$pid/fd/3) bytes of it"
kill $pid
before rm: 67108864 bytes, link count 1
after rm:  ls: cannot access 'store.bin': No such file or directory
the process still holds it:
/home/example/rig/store.bin (deleted)
and can still read all 67108864 bytes of it

Everyone knows this. It is in the first chapter of every Unix book. Here is what it looks like at scale.

The runtime kept serving every image layer and every container filesystem it already had open. The node stayed Ready. Ninety-four pods stayed Running. Every probe was green, and so was every dashboard, because nothing had failed yet. The content store, the metadata database and the snapshots were gone from the directory tree and still fully present to the one process that mattered.

The failure arrived later, one pod at a time. A liveness probe killed a container. The kubelet asked the runtime to create a new one. The runtime went to its store for the image and found nothing:

FailedCreatePodSandBox ... NotFound
error unpacking image: blob sha256:...

No event pointed at the delete. Each pod died of what looked like its own problem, hours after the cause, on a node that had reported healthy the whole time. A wipe under a live daemon does not fail fast. It fails later, which is why the guard has to be up front.

Then the second act, wearing someone else’s clothes#

The wipe had a consequence the procedure never named. It did not stop the running containers. It orphaned them.

Each container runs under its own shim process, detached from the runtime daemon by design, so the daemon can be restarted without killing workloads. The runtime’s own documentation says it plainly: when containerd boots it reconnects to shims. But it reconnects using its metadata store, and we had just deleted that. The new daemon came up with an empty store, had no record of any shim, and could never reclaim them. The kubelet saw no containers and recreated every pod.

So the node ran two copies of everything: the new set the kubelet had just made, and the old set nobody could see, still holding its network filesystem mounts and its file locks.

It surfaced as a different product’s bug. A git forge on the same node went into a crash loop, ten restarts, always on the same line:

unable to lock level db at /data/queues/common: resource temporarily unavailable

That reads as a forge problem, or a storage problem. It was neither. A POSIX lock is released by the kernel when its holder dies. A lock still held across ten restarts over several minutes means something is alive and holding it, and that something was the pre-wipe copy of the forge, invisible to the runtime, running fine.

Ask what a destructive step does to the things it does not name. “Wipe the image store” sounds bounded. Its blast radius included the identity of every running container on the node, and the symptom arrived attached to a component that had nothing to do with it.

Four lines, and the first version was also a bug#

The fix is short. After the stop, poll until the runtime daemon is actually gone. If it is not gone, delete nothing, say why, exit 1.

The first version of that guard would have been its own outage. It checked for the daemon with a pattern match on the command line:

pgrep -f "/snap/microk8s/"

On a correctly stopped node that still matches. Every surviving shim carries the runtime’s path in its command line, and so did at least one unrelated container’s entrypoint. The guard would have found processes on every healthy stop, refused to wipe, and blocked the very recovery it existed to make safe.

#!/bin/sh
# A daemon and two of its detached per-container shims, by name and path.
# (stand-ins: scripts that sleep, so only the process table is real)
B=$PWD/fake/snap/microk8s/9063/bin
$B/containerd &
$B/containerd-shim-runc-v2 -namespace k8s.io -id 7c1e &
$B/containerd-shim-runc-v2 -namespace k8s.io -id 9a40 &
sleep 1
show() { echo "$1  pgrep -f /snap/microk8s/ -> $(pgrep -f /snap/microk8s/ | wc -l)   pgrep -x containerd -> $(pgrep -x containerd | wc -l)"; }
show "daemon running:"
pkill -x containerd        # the stop: the daemon goes, the shims stay, by design
sleep 1
show "daemon stopped:"
pkill -x containerd-shim
daemon running:  pgrep -f /snap/microk8s/ -> 3   pgrep -x containerd -> 1
daemon stopped:  pgrep -f /snap/microk8s/ -> 2   pgrep -x containerd -> 0

pgrep -x containerd matches the process name exactly. A shim’s name is containerd-shim, so it cannot collide. The guard that shipped uses that, and it was caught before shipping only because someone read it against a real post-stop process list rather than against the pattern as written.

Which is the point. A check is a claim about the world too. Check your check against the thing it is supposed to see.

The orphan scan, sampled twice#

The second guard runs after the restart. It lists the shims whose container identifiers the new runtime does not know about and fails the run if there are any, printing both remediations: restart the guest, or kill exactly those process ids. It also says, in so many words, that the snapshotter switch itself is correct, so the failure is not misread as a bad switch and reverted.

It samples twice with a gap between, and counts only identifiers unknown in both samples. Right after a restart the kubelet is still creating containers, and one caught mid-creation looks orphaned for a second. A guard that cannot survive the condition it polls is just a louder version of the thing it watches.

Deserve has nothing to do with it#

The node did not deserve it. The procedure was not careless; it had worked earlier that night. Every fact in this story was already known to the person who wrote the script: that an exit code is not evidence, that a deleted file lives on in an open descriptor, that resetting a daemon’s state orphans whatever the daemon was tracking.

None of it was encoded at the line where the destructive step happened. That is the whole gap. Three guards and seventeen tests now live at that line, and each one is a sentence someone could have said out loud beforehand and nobody did.

So the rule is not “be more careful”. It is narrower than that, and cheaper. A destructive step is gated on the effect of the step before it, never on its exit status. If the effect cannot be verified, nothing is deleted and the script says so. And before the delete, you ask one question about what the thing you are deleting was tracking, because that is where the damage will surface, hours later, under some other name.