Skip to content

macOS: get_interfaces() segfaults when the process has run out of file descriptors #202

Done

Description

@GeiserX

What happens

On macOS, netdev::get_interfaces() crashes the process with SIGSEGV when it has no free file descriptors (EMFILE). We hit it in a long-running iroh endpoint, which calls netdev through netwatch's network monitor, after a descriptor leak in our own code. The process died instead of getting an error.

Why

interfaces() in os/macos/interface.rs first reads the SystemConfiguration plists. With no free descriptors that read fails. It then falls back to get_sc_interface_map(), which calls SCNetworkInterfaceCopyAll and SCNetworkInterfaceGetLocalizedDisplayName. In that state SystemConfiguration cannot set up its number formatter, and the unified log shows Couldn't map preferences shmem. CFNumberFormatterCreateNumberFromString then reads a NULL pointer.

The crash itself is in Apple's code. A Swift program that calls the same two functions after using up all descriptors crashes the same way. But netdev reaches that code only on its fallback path, so netdev can avoid it.

Backtrace (faulting thread)

EXC_BAD_ACCESS (SIGSEGV) KERN_INVALID_ADDRESS at 0x0000000000000008
0 CoreFoundation      __CFCheckCFInfoPACSignature + 4
1 CoreFoundation      CFNumberFormatterCreateNumberFromString + 112
2 SystemConfiguration localizeNumberString + 120
3 SystemConfiguration copy_display_name + 288
4 SystemConfiguration SCNetworkInterfaceGetLocalizedDisplayName + 108
5 repro               netdev::os::macos::sc::get_sc_interface_map + 408
6 repro               netdev::os::macos::interface::interfaces + 100
7 repro               repro::main

In production the caller was netwatch::netmon::Monitor::new.

Versions

  • macOS 27.2.1 (26B131), Mac16,7: seen in production with netdev 0.46.2 (netwatch, iroh 1.4.1).
  • macOS 26.6.1 (25G76), Mac mini: reproduced with netdev 0.47.0.
  • netdev 0.48.0 has the same fallback path. Not tested.

Repro (netdev = "=0.47.0")

fn main() {
    let mut hold = Vec::new();
    while let Ok(f) = std::fs::File::open("/dev/null") { hold.push(f); }
    eprintln!("holding {} fds", hold.len());
    let ifs = netdev::get_interfaces();
    eprintln!("ok: {} interfaces", ifs.len());
}

ulimit -n 256; cargo run --release prints holding 253 fds, then the process exits with 139 (SIGSEGV). Without the descriptor loop it works, and repeated calls do not leak descriptors.

Swift control (exits 139 the same way; works without the loop):

import SystemConfiguration
import Foundation
var hold: [FileHandle] = []
while let h = FileHandle(forReadingAtPath: "/dev/null") { hold.append(h) }
let all = SCNetworkInterfaceCopyAll() as! [SCNetworkInterface]
for i in all { _ = SCNetworkInterfaceGetLocalizedDisplayName(i) }
print("ok")

Suggestion

When the plist read fails with EMFILE or ENFILE (raw_os_error() 24 or 23), skip the SystemConfiguration fallback. Return the interfaces without the SC metadata, or return an error. The caller can then log and recover instead of crashing.

Activity

  1. GeiserX commented on Nov 27, 2026

    @GeiserX
    ContributorAuthor

    @shellrow I will provide a PR in a sec, feel free to further modify it if convenient, but it's working for me in the 2 instances that I have running right now thanks to the change

    Btw, thanks a lot for this software. Extremely useful

  2. shellrow commented on Nov 28, 2026

    @shellrow
    Owner

    Thanks for the clear report! I’ve confirmed the issue and will merge #203 later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions