From 3a7ecfa5cfdfc102730debfee695bc9fa5720550 Mon Sep 17 00:00:00 2001 From: sundarshahi Date: Mon, 16 Nov 2026 11:36:01 +0545 Subject: [PATCH] Engine: an --init container's PID 1 keeps none of the agent's descriptors (#248) With --init, a container's PID 1 is Kiri's own code. It is a clone of the agent and never execs, so close-on-exec never applied to it. It kept a copy of every descriptor the agent had open when it started, for as long as the container ran: - each network's DNS socket. After `docker network rm`, that copy still held the gateway's port 53, so the next network on the same subnet failed every start with "dns bind: Address in use". - its own container's stdin pipe, so `echo x | docker run -i --init alpine cat` never saw the end of its input. - other containers' pipes, the port forwards' connections and the link to the daemon. PID 1 now closes everything above stdio once it has forked the program. It needs none of them. The program never had them: they close when it execs. --- crates/kiri-init/src/linux/pid1.rs | 2 ++ tests/e2e.sh | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/crates/kiri-init/src/linux/pid1.rs b/crates/kiri-init/src/linux/pid1.rs index 77872619..79d43c1e 100644 --- a/crates/kiri-init/src/linux/pid1.rs +++ b/crates/kiri-init/src/linux/pid1.rs @@ -68,6 +68,8 @@ pub fn run(exe: &CStr, argv: *const *const libc::c_char, envp: *const *const lib } super::sys::exec_fail(2, exe, err, true); } + // The agent's descriptors (DNS sockets, other containers' pipes) stay out of PID 1. + libc::syscall(libc::SYS_close_range, 3u32, u32::MAX, 0u32); let wake = libc::timespec { tv_sec: 1, tv_nsec: 0 }; loop { diff --git a/tests/e2e.sh b/tests/e2e.sh index 46888f9c..ade8936e 100755 --- a/tests/e2e.sh +++ b/tests/e2e.sh @@ -318,6 +318,20 @@ code network-dup 1 $D network create appnet $D rm -f db svc >/dev/null t network-rm "appnet" $D network rm appnet code network-rm-builtin 1 $D network rm bridge +# A network removed while an --init container runs frees its DNS address +# for the next network on that subnet (#248). +$D network create initnet >/dev/null +initsub=$($D network inspect initnet -f '{{range .IPAM.Config}}{{.Subnet}}{{end}}') +$D run -d --name initnetc --network initnet alpine sleep 60 >/dev/null +$D run -d --name initkeep --init alpine sleep 60 >/dev/null +$D rm -f initnetc >/dev/null; $D network rm initnet >/dev/null +$D network create initnet2 >/dev/null +t network-rm-init-same-subnet "$initsub" $D network inspect initnet2 -f '{{range .IPAM.Config}}{{.Subnet}}{{end}}' +code network-rm-init-dns 0 $D run --rm --network initnet2 alpine true +$D rm -f initkeep >/dev/null; $D network rm initnet2 >/dev/null +# --init's PID 1 doesn't keep its own stdin open: cat sees the end of it +# (a held one would leave cat to the timeout, exit 143). +code init-stdin-eof 0 bash -c "echo x | $D run -i --rm --init alpine timeout 10 cat" # --internal: no default route, so anything beyond the network fails at once # (as with Docker), while the network itself still works. $D network create --internal intnet >/dev/null