HotFix: Remove Unnecessary Mach service for Stats

This commit is contained in:
Hariharan Mudaliar 2026-03-20 20:24:08 +05:30
commit 28320e4cb8
No known key found for this signature in database
GPG key ID: 025583EB0BE5640E
11 changed files with 58 additions and 161 deletions

View file

@ -39,19 +39,10 @@ class IdleAnimationManager {
// MARK: - Initialization
/// Load bundled animations from the LottieAnimations folder and built-in face
/// Load bundled animations from the LottieAnimations folder
func initializeDefaultAnimations() {
var animations: [CustomIdleAnimation] = []
// Add built-in face as first option
let builtInFace = CustomIdleAnimation(
name: "Classic Face",
source: .builtInFace,
speed: 1.0,
isBuiltIn: true
)
animations.append(builtInFace)
// Load bundled Lottie files
if let bundledAnimations = loadBundledAnimations() {
animations.append(contentsOf: bundledAnimations)
@ -60,14 +51,21 @@ class IdleAnimationManager {
// Get existing animations
var existing = Defaults[.customIdleAnimations]
// Remove any legacy builtInFace entries that may be stored from older versions
existing.removeAll { animation in
if case .lottieFile(let url) = animation.source, url.absoluteString == "builtin://face" {
return true
}
return false
}
if existing.isEmpty {
// First launch - set everything
Defaults[.customIdleAnimations] = animations
Defaults[.selectedIdleAnimation] = builtInFace
Defaults[.selectedIdleAnimation] = animations.first
print("✅ [IdleAnimationManager] First launch: Initialized with \(animations.count) animations")
} else {
// Subsequent launch - ensure all bundled animations are present
let existingIDs = Set(existing.map { $0.id })
let existingNames = Set(existing.filter { $0.isBuiltIn }.map { $0.name })
// Add any missing bundled animations
@ -79,7 +77,23 @@ class IdleAnimationManager {
}
Defaults[.customIdleAnimations] = existing
print("✅ [IdleAnimationManager] Subsequent launch: \(existing.count) total animations (\(animations.count - 1) bundled + built-in face)")
// If current selection was the old built-in face (no longer valid), select first available
if let selected = Defaults[.selectedIdleAnimation] {
let isValid: Bool
switch selected.source {
case .lottieFile(let url):
isValid = url.absoluteString != "builtin://face"
case .lottieURL:
isValid = true
}
if !isValid {
Defaults[.selectedIdleAnimation] = existing.first
print("🔄 [IdleAnimationManager] Migrated selection from legacy face to: \(existing.first?.name ?? "nil")")
}
}
print("✅ [IdleAnimationManager] Subsequent launch: \(existing.count) total animations")
}
}

View file

@ -414,14 +414,20 @@ class StatsManager: ObservableObject {
private var delayedStopTimer: Timer?
private var delayedStartTimer: Timer?
private let maxHistoryPoints = 30
/// Cached host port to avoid leaking Mach send rights.
/// Every call to `mach_host_self()` acquires a new send right that must be
/// explicitly deallocated; caching it once prevents port exhaustion over time.
private let hostPort: mach_port_t = mach_host_self()
private let totalPhysicalMemory: UInt64 = {
var stats = host_basic_info()
var count = mach_msg_type_number_t(MemoryLayout<host_basic_info_data_t>.size / MemoryLayout<integer_t>.size)
let initHostPort = mach_host_self()
let result = withUnsafeMutablePointer(to: &stats) {
$0.withMemoryRebound(to: integer_t.self, capacity: Int(count)) {
host_info(mach_host_self(), HOST_BASIC_INFO, $0, &count)
host_info(initHostPort, HOST_BASIC_INFO, $0, &count)
}
}
mach_port_deallocate(mach_task_self_, initHostPort)
if result == KERN_SUCCESS {
return UInt64(stats.max_mem)
}
@ -786,7 +792,7 @@ class StatsManager: ObservableObject {
let result = withUnsafeMutablePointer(to: &info) {
$0.withMemoryRebound(to: integer_t.self, capacity: Int(count)) {
host_statistics(mach_host_self(), HOST_CPU_LOAD_INFO, $0, &count)
host_statistics(hostPort, HOST_CPU_LOAD_INFO, $0, &count)
}
}
@ -858,7 +864,7 @@ class StatsManager: ObservableObject {
let vmResult = withUnsafeMutablePointer(to: &vmStatistics) {
$0.withMemoryRebound(to: integer_t.self, capacity: Int(size)) {
host_statistics64(mach_host_self(), HOST_VM_INFO64, $0, &size)
host_statistics64(hostPort, HOST_VM_INFO64, $0, &size)
}
}
@ -978,7 +984,7 @@ class StatsManager: ObservableObject {
var cpuInfo: processor_info_array_t?
var numCpuInfo: mach_msg_type_number_t = 0
var numCPUsU: natural_t = 0
let result = host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &numCPUsU, &cpuInfo, &numCpuInfo)
let result = host_processor_info(hostPort, PROCESSOR_CPU_LOAD_INFO, &numCPUsU, &cpuInfo, &numCpuInfo)
guard result == KERN_SUCCESS, let cpuInfo else {
return cpuCoreUsage
}