Repository navigation
Engine: an --init container's PID 1 keeps none of the agent's descriptors (#248) - #363
Open
sundarshahi wants to merge 1 commit into
Open
sundarshahi wants to merge 1 commit into
sundarshahi wants to merge 1 commit into
Conversation
…tors (libracoder#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.
sundarshahi
force-pushed
the
fix/248-init-holds-dns-sockets
branch
from
November 16, 2026 15:34
1ad66cc to
3a7ecfa
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this changes and why
On a Linux host, removing a network while an
--initcontainer is running (Composeinit: true) leaves the network's DNS listener on<gateway>:53. The next network that gets the same subnet then fails every container start withdns: dns bind: Address in use (os error 98). Docker frees the address when the network is removed.The cause is the
--initcontainer's PID 1. It's a clone of Kiri's agent that never execs, so it keeps a copy of every descriptor the agent had open when it started. That includes each network's DNS socket. The fix closes them in PID 1 once it has forked the program.Refs #248. This fixes the
--initcause of #248, which I reproduced exactly. @dels07 @severinlindenmann, did either of you have an--initorinit: truecontainer running when it happened? The reports don't say. The other paths I checked don't leak:kill_child).The same descriptor copy also caused a second bug, also reproduced below: with
--init,echo x | docker run -i --init alpine catnever ends. PID 1 keeps the write end of its own container's stdin open, socatnever sees end of input. Other containers' stdin pipes are held the same way.Problem
spawnstarts a container withclone3withoutCLONE_FILES(crates/kiri-init/src/linux/container.rs:313), so the child gets a copy of every descriptor the agent has open.--init, the program execs, and close-on-exec drops all of them. The agent opens every descriptor close-on-exec:dns.rs:115,sys.rs:65, the signalfd, relays and transport.--init,finish_childcallspid1::run(container.rs:1313). That code stays as PID 1 and only its child execs, so PID 1 keeps everything for the container's life:docker network rm→RemoveBridge→Dns::forget_bridge(dns.rs:214) closes only the agent's own copy.Fix
pid1::run, right afterfork, PID 1 callsclose_range(3, ~0U, 0). After that it only waits for signals withsigtimedwaitand reaps children. It needs nothing above stdio:waitpid, not through a descriptor PID 1 holds.close_rangeis the callcopy.rs:177already uses for the same purpose. On a kernel without it (5.7–5.8, sinceCLONE_INTO_CGROUPneeds 5.7), the call fails with ENOSYS and behaves as main does today.clone3. The agent's descriptors have to stay open there, and no clone flag stops descriptors being inherited.How you tested it
Linux, GitHub's
ubuntu-24.04runner. main and this branch are built as static musl binaries, as the Speed check builds them. Each engine runs in its own privileged container.The issue's case, 3 times on each. Create
n1, start a container on it, startkeeperwith--init, remove that container andn1, createn2(same subnet), start a container onn2:On main,
/procshowed thekeeper'skiri-initPID 1 as the extra holder. Oncekeeperwas removed,n2worked again.Stdin.
catreads from a pipe that closes at 4 s, while an--initcontainer started at 2 s is running:An
--initcontainer's own stdin, 3 times on each:--initstill behaves the same:tests/e2e.sh:network-rm-init-same-subnet: guards the test itself; the second network has the first one's subnet.network-rm-init-dns: the issue's case.init-stdin-eof:echo x | docker run -i --init alpine cat.init-stdin-eofrunscatunder the container's owntimeout 10, so a regression fails with 143 instead of hanging the suite.tests/e2e.sh. Both runs completed.network-rm-init-dnsandinit-stdin-eof.*.kiri.localhostchecks, andhost.docker.internalinside a nested engine.cargo clippy -p kiri-init(Linux, musl) has the same warnings on main and on this branch.rustfmtonpid1.rsshows no new differences.Tested on: Linux (Ubuntu 24.04, GitHub runner's kernel). The Mac and Windows run the same
kiri-initin their VMs. I didn't run them by hand.How you found it
AI tools used to write this change: yes. An AI coding assistant helped write and test it. I reviewed and tested every change.
Checklist
cargo fmt,cargo clippyand the tests pass