fix(monitors): 补齐联网权限并释放采样端口引用

This commit is contained in:
2026-11-03 21:59:49 +08:00
parent cecd646376
commit ea4d0e7f23
7 changed files with 89 additions and 4 deletions
+24
View File
@@ -0,0 +1,24 @@
import Foundation
import Darwin
@main struct MachPortChecks {
static func hostReferences() -> mach_port_urefs_t {
let host = mach_host_self()
defer { mach_port_deallocate(mach_task_self_, host) }
var references: mach_port_urefs_t = 0
precondition(mach_port_get_refs(mach_task_self_, host, mach_port_right_t(MACH_PORT_RIGHT_SEND), &references) == KERN_SUCCESS)
return references
}
static func main() {
var cpu = CPUMonitor()
_ = cpu.sample()
_ = MemoryMonitor.sample()
let initial = hostReferences()
for _ in 0..<1_000 { _ = cpu.sample() }
precondition(hostReferences() == initial, "CPU sampling leaked host send-right references")
for _ in 0..<1_000 { _ = MemoryMonitor.sample() }
precondition(hostReferences() == initial, "Memory sampling leaked host send-right references")
print("PASS: 1,000 CPU + 1,000 memory samples keep host send-right references at \(initial)")
}
}