{"record":{"id":"25a95ac9906cc262","repo":"ClementTsang/bottom","slug":"ioservicegetmatchingservices-failed-error-code-r","errorCode":null,"errorMessage":"IOServiceGetMatchingServices failed, error code {result}","messagePattern":"IOServiceGetMatchingServices failed, error code (.+?)","errorType":"error_code","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"src/collection/disks/unix/macos/io_kit/io_disks.rs","lineNumber":22,"sourceCode":"use super::{IoIterator, bindings::*};\n\npub fn get_disks() -> anyhow::Result<IoIterator> {\n    let mut media_iter: io_iterator_t = 0;\n\n    // SAFETY: This is a safe syscall via IOKit, all the arguments should be\n    // safe.\n    let result = unsafe {\n        IOServiceGetMatchingServices(\n            kIOMasterPortDefault,\n            IOServiceMatching(kIOMediaClass.as_ptr().cast()),\n            &mut media_iter,\n        )\n    };\n\n    if result == kern_return::KERN_SUCCESS {\n        Ok(media_iter.into())\n    } else {\n        bail!(\"IOServiceGetMatchingServices failed, error code {result}\");\n    }\n}\n","sourceCodeStart":4,"sourceCodeEnd":25,"githubUrl":"https://github.com/ClementTsang/bottom/blob/b77d3175028849824e987c35177e8f61450d72e7/src/collection/disks/unix/macos/io_kit/io_disks.rs#L4-L25","documentation":"get_disks enumerates physical disks on macOS via the IOKit function IOServiceGetMatchingServices (matching IOBlockStorageService). When the call returns anything other than KERN_SUCCESS, the kern_return_t error code is surfaced in this bail.","triggerScenarios":"Calling disk I/O stats collection (io_stats -> get_disks) on macOS when IOKit cannot enumerate matching block storage services, e.g. due to IOKit master-port issues or sandboxing.","commonSituations":"Running in restricted environments (sandboxed apps, containers on macOS), IOKit service exhaustion, or permission restrictions on IOKit device registry access.","solutions":["Check the numeric error code against IOKit kern_return_t values","Run outside sandboxed/containerized environments with IOKit access","Verify the process has entitlements/permissions for IOKit device queries","Fall back to alternative disk enumeration (e.g. statfs on mount points)"],"exampleFix":"// before\nlet disks = io_stats()?;\n// after\nlet disks = match io_stats() {\n    Ok(d) => d,\n    Err(e) => { log::warn!(\"iokit disk enumeration failed: {e}\"); Vec::new() },\n};","handlingStrategy":"try-catch","validationCode":"// Pre-check that IOKit is reachable (outside sandbox) before harvesting\nlet has_iokit = std::path::Path::new(\"/System/Library/Extensions\").exists();","typeGuard":null,"tryCatchPattern":"match get_io_usage(&collector) {\n    Ok(h) => h,\n    Err(e) if e.to_string().starts_with(\"IOServiceGetMatchingServices failed\") => {\n        log::warn!(\"IOKit unavailable: {e}\"); IoHarvest::default()\n    },\n    Err(e) => return Err(e),\n}","preventionTips":["Avoid running disk-stat collection inside macOS sandboxes/containers","Check kern_return_t codes when debugging","Provide fallback disk metrics via statfs on mount points","Retry the sweep; IOKit failures are often transient"],"tags":["macos","iokit","disks","system-api"],"backgroundTag":"api-error-response","analyzedSha":"b77d3175028849824e987c35177e8f61450d72e7","analyzedAt":"2026-09-07T14:53:21.246Z","contentChangedAt":"2026-09-07T14:53:21.246Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}