Skip to content

[Bug] Crash in FanControlEngine.updateAllSensorsInPlace due to overlapping sensor polls #1

Closed

Description

@hetiankong

MacFansControl version:

1.1.3 (build 1)

macOS version:

macOS 26.4.1 (25E253)

Chip (e.g., M2 Pro, M4 Max):

Apple Silicon, MacBookPro18,4

Describe the bug:

MacFansControl reliably crashes after running in the background for a while. The crash appears to happen during sensor polling, specifically while FanControlEngine.updateAllSensorsInPlace(from:) is updating sensor state.

The crash report shows at least two concurrent tasks inside the same FanControlEngine.updateAllSensorsInPlace(from:) method:

Thread 2 Crashed:: Dispatch queue: com.apple.root.user-initiated-qos.cooperative
0   MacFansControl  FanControlEngine.updateAllSensorsInPlace(from:) + 1000
1   MacFansControl  FanControlEngine.poll() + 68
2   MacFansControl  closure #1 in closure #3 in FanControlEngine.start() + 1
3   MacFansControl  <deduplicated_symbol> + 1
4   MacFansControl  specialized thunk for @escaping @isolated(any) @callee_guaranteed @async () -> (@out A) + 1
5   MacFansControl  <deduplicated_symbol> + 1
6   libswift_Concurrency.dylib completeTaskWithClosure(...) + 1

Thread 9:: Dispatch queue: com.apple.root.user-initiated-qos.cooperative
0   MacFansControl  specialized _NativeDictionary.copy() + 304
1   MacFansControl  FanControlEngine.updateAllSensorsInPlace(from:) + 676
2   MacFansControl  FanControlEngine.poll() + 68
3   MacFansControl  closure #1 in closure #3 in FanControlEngine.start() + 1
4   MacFansControl  <deduplicated_symbol> + 1
5   MacFansControl  specialized thunk for @escaping @isolated(any) @callee_guaranteed @async () -> (@out A) + 1
6   MacFansControl  <deduplicated_symbol> + 1
7   libswift_Concurrency.dylib completeTaskWithClosure(...) + 1

Thread 10:: Dispatch queue: com.apple.root.utility-qos.cooperative
0   libsystem_kernel.dylib mach_msg2_trap + 8
...
7   MacFansControl  IOHIDSensorReader.readAllSensors() + 1828
8   MacFansControl  protocol witness for SensorReaderProtocol.readAllSensors() in conformance IOHIDSensorReader + 20
9   MacFansControl  closure #1 in FanControlEngine.poll() + 60

Exception:

Exception Type:    EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_INVALID_ADDRESS at 0x8000000000000010 -> 0x0000000000000010
Triggered by Thread: 2

After symbolication, the relevant app symbols are:

FanControlEngine.updateAllSensorsInPlace(from:) + 676
FanControlEngine.updateAllSensorsInPlace(from:) + 1000
FanControlEngine.poll() + 68
closure #1 in closure #3 in FanControlEngine.start() + 1
IOHIDSensorReader.readAllSensors() + 1828
closure #1 in FanControlEngine.poll() + 60

This looks like overlapping polling tasks are mutating FanControlEngine's Swift collections concurrently. The +676 frame is inside _NativeDictionary.copy(), and the fault address 0x8000000000000010 is consistent with one task observing a Swift exclusive-access / inout sentinel value as if it were a Dictionary storage pointer. In other words, one poll() appears to be updating a Dictionary such as the sample cache while another poll() is reading/copying/updating it.

From the binary symbols, FanControlEngine appears to have mutable observed state including:

_allSensors
_sensorGroups
_sensorIDOrder
_sampleByID
_favoriteReadings
_lastKnownFavoriteMetadata
_pollTimer

The likely fix is to make polling non-overlapping and isolate UI-observed state updates to a single actor, for example:

@MainActor
private var isPolling = false

@MainActor
func pollIfIdle() async {
    guard !isPolling else { return }
    isPolling = true
    defer { isPolling = false }

    let readings = await Task.detached(priority: .utility) {
        sensorReader.readAllSensors()
    }.value

    updateAllSensorsInPlace(from: readings)
}

Alternatively, replace the repeating NSTimer + new Task approach with a single structured polling loop that waits for one poll to complete before sleeping and starting the next. Marking FanControlEngine as @MainActor or moving mutable sensor state into an actor/serial queue should also prevent concurrent Dictionary mutation.

Steps to reproduce:

  1. Launch MacFansControl 1.1.3.
  2. Leave it running in the menu bar/background for several hours.
  3. The app eventually crashes during background sensor polling.

In the observed crash, the app launched at 00:01:27 and crashed at 07:08:07 on the same day.

Expected behavior:

Sensor polling should not create overlapping state updates, and the app should keep running without crashing.

Screenshots (if applicable):

Not applicable. This is a crash report / concurrency issue.

I can provide the full crash report if needed, but I omitted Crash Reporter Key / Incident Identifier and other non-essential identifiers from this public issue.

Activity

  1. added
    bug
    on Jun 7, 2026
  2. beyondthecode-bc commented on Jun 7, 2026

    @beyondthecode-bc
    Owner

    Fixed in v1.1.4 — thank you for the exceptionally detailed and well-symbolicated crash report. Your analysis was spot on.

    Root cause

    FanControlEngine.poll() was a nonisolated async method launched every 2 seconds from a repeating Timer via Task { @MainActor in await self.poll() }. Per Swift SE-0338, a nonisolated async function does not inherit the caller's actor — so poll() actually ran on the global cooperative executor, not the main thread (which is exactly why every crashing thread showed Dispatch queue: com.apple.root.user-initiated-qos.cooperative).

    The first await inside poll() (the detached sensor read) is a suspension point. With no reentrancy guard, if a sensor read ever outran the 2-second timer interval, the next tick spawned a second concurrent poll() body. Both bodies mutated the same non-Sendable collections (sampleByID, allSensors, sensorGroups, favoriteReadings) at once, racing on Swift Dictionary copy-on-write inside updateAllSensorsInPlace → _NativeDictionary.copy() → EXC_BAD_ACCESS at the exclusivity sentinel address 0x80000000…10. This exactly matches the two concurrent updateAllSensorsInPlace frames in your report.

    Fix

    • FanControlEngine is now isolated to @MainActor, so poll() and all sensor-state mutation are serialized on the main actor — overlapping ticks can no longer run in parallel. The expensive IOHIDEventSystem enumeration still runs off the main thread via Task.detached, so the UI never blocks.
    • Added a pollInFlight reentrancy guard so a tick that fires while a previous poll is still in flight is dropped rather than queued — in-flight polls are capped at one even under sustained sensor-read latency.
    • Propagated the isolation to AppDelegate and the app entry point, and cleaned up the resulting main-actor isolation warnings.

    If you have a chance to run v1.1.4 through the same multi-hour background soak that triggered the original crash, a confirmation would be very welcome. Closing as resolved — please reopen if you see anything similar. Thanks again! 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions