Bitácora

Buscando la syscall con la que Docker mata un contenedor

· Mariano Rac

En el proyecto 2 del laboratorio de Sistemas Operativos 1, un daemon en Go elimina contenedores con docker rm -f, y yo quería confirmar desde el kernel, con eBPF, que cada contenedor realmente recibía la señal de terminación. Parecía sencillo: enganchar una sonda a la syscall kill.

Capa 1: kill()

La sonda sobre sys_enter_kill capturaba eventos todo el tiempo, pero siempre con signal=0. Señal 0 en kill() es la forma estándar de preguntar "¿este proceso existe?", y la usan systemd y otros procesos de fondo constantemente. Al ejecutar docker rm -f no aparecía ningún evento nuevo.

Capa 2: tgkill()

Probé la hipótesis de que Docker le mandara la señal a un hilo específico. Mismo resultado: solo ruido, sobre todo de systemd-udevd reciclando sus workers, y nada relacionado con mis contenedores.

Capa 3: bpftrace

Antes de escribir otra sonda completa intenté una prueba rápida con bpftrace sobre sys_enter_pidfd_send_signal. Falló por un problema de tipos: no lograba resolver la estructura siginfo_t del tracepoint contra el BTF disponible.

La respuesta: strace

Dejé de adivinar y seguí al proceso real. Con strace -f sobre el containerd-shim del contenedor, siguiendo a sus hijos, ejecuté docker rm -f desde otra terminal. Ahí apareció sin ambigüedad:

pidfd_send_signal(pidfd, SIGKILL, NULL, 0)

runc no usa kill(2). Usa pidfd_send_signal, que trabaja sobre un descriptor de archivo que apunta al proceso en lugar de sobre su PID numérico. Así evita una condición de carrera: si el proceso muere y su PID se recicla entre que lo encuentra y le manda la señal, con un PID podría matar a otro proceso; con un pidfd, no.

La sonda final se engancha a sys_enter_pidfd_send_signal, filtra solo SIGKILL y publica cada evento en un ring buffer. El daemon espera hasta 3 segundos esa confirmación antes de registrar la eliminación como exitosa. El repositorio conserva también la sonda de kill, como evidencia del proceso.

Lo que me llevo

Cuando una sonda "funciona" pero no ve lo que espero, la pregunta no es cómo filtrar mejor el ruido sino qué hace realmente el programa. strace respondió en un minuto lo que tres sondas no pudieron.

Notes

Hunting for the syscall Docker uses to kill a container

· Mariano Rac

In project 2 of the Operating Systems 1 lab, a Go daemon removes containers with docker rm -f, and I wanted to confirm from the kernel, with eBPF, that each container really got the kill signal. It looked simple: attach a probe to the kill syscall.

Layer 1: kill()

The probe on sys_enter_kill captured events all the time, but always with signal=0. Signal 0 in kill() is the standard way of asking "does this process exist?", and systemd and other background processes use it constantly. Running docker rm -f produced no new events.

Layer 2: tgkill()

I tested the idea that Docker sent the signal to a specific thread. Same result: only noise, mostly systemd-udevd recycling its workers, and nothing related to my containers.

Layer 3: bpftrace

Before writing another full probe I tried a quick test with bpftrace on sys_enter_pidfd_send_signal. It failed on types: it couldn't resolve the tracepoint's siginfo_t structure against the available BTF.

The answer: strace

I stopped guessing and followed the real process. With strace -f on the container's containerd-shim, following its children, I ran docker rm -f from another terminal. There it was, unambiguous:

pidfd_send_signal(pidfd, SIGKILL, NULL, 0)

runc doesn't use kill(2). It uses pidfd_send_signal, which works on a file descriptor pointing to the process instead of its numeric PID. That avoids a race: if the process dies and its PID gets recycled between finding it and signaling it, a PID could kill a different process; a pidfd can't.

The final probe attaches to sys_enter_pidfd_send_signal, keeps only SIGKILL and publishes each event to a ring buffer. The daemon waits up to 3 seconds for that confirmation before logging the removal as successful. The repository also keeps the kill probe as evidence of the process.

What I take from it

When a probe "works" but doesn't see what I expect, the question isn't how to filter the noise better but what the program actually does. strace answered in a minute what three probes couldn't.