Chris Low-level systems developer
← Back to all posts

The One Call That Closes a Whole Range

ISA

The piece reasons about a syscall contract: flags, range bounds, shared descriptor tables, and error rules. It names no instruction, no encoding, and no register or trap convention, so nothing in the argument hangs on what the hardware does with an opcode.

MICROARCH

No pipeline, cache, or timing observation enters the argument. The race under discussion is threads opening descriptors between a /proc listing and a close loop; the fix is one kernel pass over a range, which is a software contract, not a hardware property.

FIRMWARE

No machine-mode service, boot step, or firmware handoff takes part. The call goes from the process into the kernel's descriptor-table code and back; firmware never appears in the mechanism.

KERNEL

The entire mechanism lives in the kernel's descriptor table: close_range walks the caller's table in one pass, applies CLOEXEC marks without closing, copies the table first under UNSHARE, ignores per-descriptor errors, and returns EINVAL on bad flags or first greater than last. The workbench tests proved each behavior against a live kernel.

ALGORITHM

The insight is a contract with three behaviors, not a procedure to analyze. The article states what one call does to a range and what two flags do to its meaning; it derives no steps, states no invariant, and measures no complexity.

Every shell, daemon, and container runtime does the same cleanup before exec: close every file descriptor the child must not inherit, except 0, 1, and 2. The old way walks /proc/self/fd and closes each number in a loop, one close per descriptor [1]. The loop has a race: a thread can open a new descriptor between the listing and the close, and the new descriptor escapes.

close_range(2) closes a whole span in one call: close_range(4, 6, 0) closes every descriptor from 4 through 6 [1]. On the workbench, five descriptors sat open: two plain files, a pipe pair, and one more file. The call closed the range and left neighbors 3 and 7 untouched, one motion instead of a listing and a loop [3].

A matrix of small toggle switches on an electrical test panel, viewed at an angle
A matrix of toggle switches on an electrical test panel. close_range flips a whole row in one motion: no listing, no loop, one call.
Photo: Martin Birkin, CC0, via Public Domain Pictures

Then the two flags that change the call's meaning. CLOSE_RANGE_CLOEXEC closes nothing; it marks every descriptor in the range close-on-exec, so they close at exec time instead [1]. CLOSE_RANGE_UNSHARE first detaches the caller's descriptor table from every thread that shares it, conceptually unshare(CLONE_FILES) followed by close_range [1]; the kernel does it by copying the table before applying the closes [2]. In the workbench test the unsharing thread's close left the main thread's copy open; the same close without the flag closed it for everyone [3].

The three forms, in three lines:

close_range(4, 6, 0);                     /* the range, one motion */
close_range(4, 6, CLOSE_RANGE_CLOEXEC);   /* mark the range, close nothing */
close_range(3, ~0U, CLOSE_RANGE_UNSHARE); /* private table first, then 3..MAX */

The contract has sharp edges. Errors on individual descriptors are ignored, so one dead descriptor in the middle does not abort the range [1]. Bad flags, or first greater than last, return EINVAL [1]. The pre-exec idiom is close_range(3, ~0U, CLOSE_RANGE_UNSHARE): everything past stderr, privately, in one motion. The call arrived from FreeBSD, landed in Linux 5.9 with glibc 2.34, and the CLOEXEC flag joined in 5.11 [1].

Sources

  1. 01
    close_range(2)man-pages project

    The contract this run: flag semantics, per-descriptor errors ignored, EINVAL on bad flags or first greater than last, and the close_range(3, ~0U, CLOSE_RANGE_UNSHARE) pre-exec idiom.

  2. 02
    fs/file.c: sys_close_rangetorvalds/linux

    The kernel implementation the UNSHARE table-copy behavior was cross-checked against.

  3. 03
    run.txtdillingerstaffing/closerange-demo

    Three runs of the firsthand workbench, all PASS: the range close, the CLOEXEC marking, and the UNSHARE table detach.

Stuck on a board or a crash? Fixed-price firmware work: crash triage $250/symptom, bare-metal bring-up from $500, C audit from $350. .

Choose how to get in touch

The project brief is filled in for you.

Compose in Gmail Use my email app