Skip to content

Fix macOS IPC cleanup in builder#14459

Merged
Mic92 merged 3 commits intoNixOS:masterfrom
jfroche:fix/macos-ipcs
Nov 6, 2025
Merged

Fix macOS IPC cleanup in builder#14459
Mic92 merged 3 commits intoNixOS:masterfrom
jfroche:fix/macos-ipcs

Conversation

@jfroche
Copy link
Contributor

@jfroche jfroche commented Nov 3, 2025

In Linux, IPC objects are automatically cleaned up when the IPC namespace is destroyed. On Darwin, since there are no IPC namespaces, IPC objects may persist after the build user's processes are killed, leading to resource leaks.

This PR modifies the builder cleanup logic to enumerate and remove leftover IPC objects associated with the build user using Darwin's sysctl interface (kern.sysv.ipcs.shm), following the pattern from Apple's ipcs.c implementation.

Changes in this PR:

  • Add cleanupSysVIPCForUser() function that uses Darwin's sysctl API to enumerate and remove IPC objects owned by the build user
  • Call cleanup in killSandbox() after processes are terminated

Closes: #12548
References:

@jfroche jfroche requested a review from edolstra November 4, 2025 15:33
@Ericson2314 Ericson2314 added this pull request to the merge queue Nov 4, 2025
@Ericson2314
Copy link
Member

I'm merging this, but it would be nice if we could make a test for this

@Ericson2314 Ericson2314 removed this pull request from the merge queue due to a manual request Nov 4, 2025
Copy link
Member

@Ericson2314 Ericson2314 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to got once the docs are added

In Linux, IPC objects are automatically cleaned up when the IPC namespace is destroyed.
On Darwin, since there are no IPC namespaces, the IPC objects may sometimes persist after the build user's processes are killed.

This patch modifies the cleanup logic to use sysctl calls to identify and remove left over shm segments associated with the build user.

Fixes: NixOS#12548
Previously, only shared memory segments were cleaned up.
This could lead to leaked message queues and semaphore sets when builds use System V IPC, exhausting kernel IPC limits over time.

This commit extends the cleanup to all three System V IPC types:
1. Shared memory segments
2. Message queues
3. Semaphores

Additionally, we stop removing IPC objects during iteration, as it could corrupt the kernel's iterator state and cause some objects to be skipped. The new implementation uses a two-pass approach where we list first and then remove them in a separate pass.

The IPC IDs are now extracted during iteration using actual system calls (shmget, msgget, semget) rather than being looked up later, ensuring the objects exist when we capture their IDs.
@Mic92
Copy link
Member

Mic92 commented Nov 6, 2025

Tested and works.

@Mic92 Mic92 enabled auto-merge November 6, 2025 09:27
@Mic92 Mic92 added this pull request to the merge queue Nov 6, 2025
Merged via the queue into NixOS:master with commit 3f18cad Nov 6, 2025
17 checks passed
@edolstra edolstra mentioned this pull request Dec 9, 2025
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.

System V IPC objects not cleaned up with Darwin Sandbox

4 participants