Skip to content

fix: remove the session pid file on exit - #53

Merged
raphaelcoeffic merged 1 commit into
mainfrom
fix/leaked-pid-file
Nov 28, 2026
Merged

raphaelcoeffic merged 1 commit into
mainfrom
fix/leaked-pid-file

Conversation

@raphaelcoeffic

Copy link
Copy Markdown
Owner

Bug

Every session leaves its pid file in <state>/rootful/pids (or rootless/pids): the parent dive process exits with std::process::exit(), which skips destructors, so PidFile::drop never runs.

The next session removes pid files of dead processes, so they don't pile up, but if the PID has been reused, enter_shared_mount_namespace enters the mount namespace of that unrelated process — as root — where the overlay is not mounted: the container gets an empty /nix.

Fix

  • Drop shared_mount (and with it the pid file) in the parent before exit().
  • Comment on the child branch: it holds a forked copy of the pid file and must only leave through exit(), or it would remove the parent's pid file.
  • CI smoke test: fail if a pid file is left after the sessions. Without the fix, the last session's pid file is always left.

Testing

Real rootful sessions against an alpine:3.22 container, scratch state directory, with sudo:

  • before the fix: a root-owned pid file is left after every run
  • after the fix: none left after 3 sequential sessions; exit codes preserved (1, 2, 3); the pid file exists while a session runs
  • overlapping sessions: A and B run together (2 pid files); after A exits, 1 left; C joins the shared mount namespace through B's pid file; 0 left at the end

Not addressed

A pid file can still be left when dive is killed (SIGKILL, OOM), so the PID reuse issue remains possible after a crash. Checking that the process behind a pid file is dive before entering its namespace would cover it; the clean-up PR (#52) has a similar check (/proc/<pid>/comm starts with dive) that could be shared.

The parent exits with std::process::exit(), which skips destructors:
the pid file was never removed, and every session left one behind.
The next session removes pid files of dead processes, but when the PID
has been reused, it enters the mount namespace of an unrelated process,
where the overlay is not mounted, and the container gets an empty /nix.

Drop the shared mount, and with it the pid file, before exiting. Check
in the CI smoke test that no pid file is left after the sessions.
@raphaelcoeffic
raphaelcoeffic merged commit f4c160d into main Nov 28, 2026
2 checks passed
@raphaelcoeffic
raphaelcoeffic deleted the fix/leaked-pid-file branch November 28, 2026 07:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant