fix(monitors): 限制外部报告输出并可靠终止超时子进程
This commit is contained in:
@@ -33,24 +33,8 @@ actor SystemBatteryHealthMonitor {
|
||||
}
|
||||
|
||||
nonisolated private static func readReport() async -> Double? {
|
||||
await withCheckedContinuation { continuation in
|
||||
// Process/pipe waits use a utility queue rather than the cooperative executor.
|
||||
DispatchQueue.global(qos: .utility).async {
|
||||
let process = Process()
|
||||
process.executableURL = URL(fileURLWithPath: "/usr/sbin/system_profiler")
|
||||
process.arguments = ["SPPowerDataType", "-json", "-detailLevel", "mini", "-timeout", "5"]
|
||||
let pipe = Pipe()
|
||||
process.standardOutput = pipe
|
||||
process.standardError = FileHandle.nullDevice
|
||||
do { try process.run() }
|
||||
catch { continuation.resume(returning: nil); return }
|
||||
let deadline = DispatchWorkItem { if process.isRunning { process.terminate() } }
|
||||
DispatchQueue.global(qos: .utility).asyncAfter(deadline: .now() + 10, execute: deadline)
|
||||
let data = pipe.fileHandleForReading.readDataToEndOfFile()
|
||||
process.waitUntilExit()
|
||||
deadline.cancel()
|
||||
continuation.resume(returning: process.terminationStatus == 0 && data.count <= 262_144 ? parse(data) : nil)
|
||||
}
|
||||
}
|
||||
guard let data = await BoundedProcess.read(executable: URL(fileURLWithPath: "/usr/sbin/system_profiler"),
|
||||
arguments: ["SPPowerDataType", "-json", "-detailLevel", "mini", "-timeout", "5"]) else { return nil }
|
||||
return parse(data)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user