go-linux

namespace design decisions

Part of the accepted-design index.

See the usage guide for calling contracts and verification for runner requirements.

Own namespace references, preserve Go process creation

An nsfs descriptor pins a namespace independently of numeric process identity. The handle owns that reference and exposes concrete inspection, comparison, and entry operations. Descriptor access would transfer syscall and lifetime obligations to callers, so file adoption consumes ownership while all native plumbing remains private. NSFS_MAGIC, namespace-type ioctl support, descriptor mode, and close-on-exec checks prevent accidental adoption of unrelated resources. Related-namespace ioctls acquire separate close-on-exec references, following the Linux 7.0 nsfs implementation. Comparison pins both descriptors before comparing device and inode, so concurrent close cannot turn a sampled identity into a comparison against a recycled namespace inode.

The standard library already owns safe Go process creation, including namespace clone/unshare flags and ID mappings before exec. Duplicating it would add raw fork hazards and another process lifecycle API. Unshare instead expresses the distinct mechanism of creating namespace state on a dedicated existing thread. The typed mask keeps Linux combinations in one syscall; Current accepts only a single type. User namespace creation remains useful through standard child startup, even though Linux refuses it from an ordinary multithreaded Go runtime.

PID creation makes Go’s lazy process initialization observable: its pidfd capability probe clones and reaps a child, which must not become the new namespace’s init. Unshare therefore initializes process support through the public os.FindProcess API before changing PID child membership and releases that temporary reference before unshare. Owning this Go integration requirement keeps ordinary child startup safe without requiring applications to know runtime probe details or introducing raw clone operations. Invalid masks are rejected before any preparation, and masks without PID do not trigger it.

Path acquisition uses O_NONBLOCK to avoid blocking on invalid FIFO inputs, then clears it before constructing an os.File owner. Namespace operations require no polling; registering the descriptor with Go’s poller would introduce unrelated allocation and fatal poller-initialization failure under descriptor exhaustion. Adoption preserves the supplied file’s flags because its wrapper and any polling state already belong to that transferred owner.

Make thread retirement explicit

A scoped callback suggests that returning restores the caller’s prior environment. Linux does not provide that general guarantee: PID transitions constrain descendants, user reentry is forbidden, filesystem sharing can prevent entry, and time transitions affect the clock domain and require a single-threaded process. Partial setup can also leave related thread context changed. Restoration failure would need to prevent Go from scheduling unrelated work on the affected thread, and a callback could start goroutines that never entered the namespace.

Explicit operations on a caller-owned locked goroutine make this boundary visible without inventing a scheduling abstraction. The required lifecycle leaves the goroutine locked through all exits, letting Go retire its thread. The startup thread is a special case that the runtime permanently parks instead of terminating; neither path returns the changed thread to unrelated goroutines. A parked startup thread can retain namespace references until process exit, so thread retirement is not a promise of immediate namespace destruction. No automatic locking, unlocking, restoration, goroutine inheritance, or process-wide transition is promised. Tests reserve the startup thread and exercise the lifecycle inside bounded subprocesses, including actual creation/entry, failure, recovered panic, independent thread observations, and worker-thread teardown.

Enter supplies the handle’s actual namespace type rather than accepting an independent mask that could disagree with its identity. It does not silently unshare filesystem state: that is an additional observable change, so callers request it explicitly. Time and user entry retain kernel errors instead of pretending a locked goroutine is a single-threaded process. The supported ioctl and CLONE_NEW* values use maintained x/sys bindings and exist in the Linux 7.0 UAPI. Mount discovery borrows a mount namespace handle through a module-private representation and preserves the handle’s ownership and concurrent-close semantics. See the mount discovery decision.