{"record":{"id":"8f52ef04d8837d1d","repo":"prometheus/node_exporter","slug":"couldn-t-get-memory-statistics-host-statistics-re","errorCode":null,"errorMessage":"couldn't get memory statistics, host_statistics returned %d","messagePattern":"couldn't get memory statistics, host_statistics returned (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"collector/meminfo_darwin.go","lineNumber":54,"sourceCode":"// NewMeminfoCollector returns a new Collector exposing memory stats.\nfunc NewMeminfoCollector(logger *slog.Logger) (Collector, error) {\n\treturn &meminfoCollector{\n\t\tlogger: logger,\n\t}, nil\n}\n\nfunc (c *meminfoCollector) getMemInfo() (map[string]float64, error) {\n\thost := C.mach_host_self()\n\tinfoCount := C.mach_msg_type_number_t(C.HOST_VM_INFO64_COUNT)\n\tvmstat := C.vm_statistics64_data_t{}\n\tret := C.host_statistics64(\n\t\tC.host_t(host),\n\t\tC.HOST_VM_INFO64,\n\t\tC.host_info_t(unsafe.Pointer(&vmstat)),\n\t\t&infoCount,\n\t)\n\tif ret != C.KERN_SUCCESS {\n\t\treturn nil, fmt.Errorf(\"couldn't get memory statistics, host_statistics returned %d\", ret)\n\t}\n\ttotalb, err := unix.Sysctl(\"hw.memsize\")\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tswapraw, err := unix.SysctlRaw(\"vm.swapusage\")\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tswap := (*C.xsw_usage_t)(unsafe.Pointer(&swapraw[0]))\n\n\t// Syscall removes terminating NUL which we need to cast to uint64\n\ttotal := binary.LittleEndian.Uint64([]byte(totalb + \"\\x00\"))\n\n\tvar pageSize C.vm_size_t\n\tC.host_page_size(C.host_t(host), &pageSize)\n","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/prometheus/node_exporter/blob/17ddd77c59ba27e1508e9f7894b1e55b44d6aed3/collector/meminfo_darwin.go#L36-L72","documentation":"This error comes from the macOS memory collector when the Mach call host_statistics64 (via cgo, host_statistics in the message) fails to fill the vm_statistics64 structure. The kernel returns a kern_return_t other than KERN_SUCCESS, and its integer value is embedded in the message. It indicates the process lacks the host privilege needed for HOST_VM_INFO64 or the Mach host port is invalid.","triggerScenarios":"getMemInfo calls C.host_statistics64(C.host_t(host), C.HOST_VM_INFO64, ...) and the returned kern_return_t != KERN_SUCCESS — e.g. the host port was obtained incorrectly, the requested flavor is not accepted by the kernel, or a sandbox/severely restricted environment blocks the host_info call.","commonSituations":"Running node_exporter under a macOS sandbox or hardened runtime without the required host info entitlement; running on very old macOS kernels where HOST_VM_INFO64 is unavailable; embedding the collector in a process whose task self/host ports were manipulated; failure after macOS version upgrades that changed mach port behaviors.","solutions":["Verify the process can call host_statistics64 with HOST_VM_INFO64 outside the sandbox (run a minimal test binary); if sandboxed, add the entitlement or disable sandboxing","Check that the host port is obtained via mach_host_self() and passed correctly to C.host_t(host)","Log the numeric kern_return_t value and look it up in mach/kern_return.h (e.g. KERN_INVALID_ARGUMENT=4) to identify the precise cause","Fall back to a different memory source (e.g. vm_stat CLI output parsed externally, sysctl vm.swapusage) or disable the meminfo collector on affected hosts","Ensure the macOS SDK/kernel version supports HOST_VM_INFO64; on ancient versions fall back to HOST_VM_INFO"],"exampleFix":"// before\nret := C.host_statistics64(C.host_t(host), C.HOST_VM_INFO64, ...)\nif ret != C.KERN_SUCCESS {\n    return nil, fmt.Errorf(\"couldn't get memory statistics, host_statistics returned %d\", ret)\n}\n// after\nret := C.host_statistics64(C.host_t(host), C.HOST_VM_INFO64, ...)\nif ret != C.KERN_SUCCESS {\n    return nil, fmt.Errorf(\"couldn't get memory statistics, host_statistics returned %d (kern_return_t)\", ret)\n}\n// diagnose: if ret == KERN_INVALID_ARGUMENT, switch to C.HOST_VM_INFO on older kernels","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"collector, err := NewNodeCollector(...)\nif err != nil {\n    // NewMeminfoCollector is invoked during registry setup; log and continue\n    // without the memory collector rather than aborting the exporter\n    log.Printf(\"meminfo collector unavailable: %v\", err)\n}","preventionTips":["Run the exporter without a restrictive macOS sandbox/entitlement profile","Test the collector after each macOS major upgrade","Check kern_return_t codes in logs to catch kernel-level failures early"],"tags":["darwin","memory","cgo","mach-kernel"],"backgroundTag":"api-request-failed","analyzedSha":"17ddd77c59ba27e1508e9f7894b1e55b44d6aed3","analyzedAt":"2026-09-07T17:54:06.211Z","contentChangedAt":"2026-09-07T17:54:06.211Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}