Skip to content

fix(ios): dispatch thermal EventChannel callbacks on main queue#14

Merged
kevmo314 merged 1 commit into
muxable:mainfrom
Couchsurfing:fix/ios-main-thread-event-sink
Jun 3, 2026
Merged

fix(ios): dispatch thermal EventChannel callbacks on main queue#14
kevmo314 merged 1 commit into
muxable:mainfrom
Couchsurfing:fix/ios-main-thread-event-sink

Conversation

@ThadOnTheCouch
Copy link
Copy Markdown
Contributor

@ThadOnTheCouch ThadOnTheCouch commented Jun 2, 2026

Summary

ProcessInfo.thermalStateDidChangeNotification can be delivered on a background dispatch queue. The current implementation invokes FlutterEventSink directly from onThermalStateChanged, which must run on the main thread. This causes NSInternalInconsistencyException in the Flutter engine when the device thermal state changes (e.g. under memory pressure while backgrounding).

This change:

  • Dispatches EventChannel callbacks with DispatchQueue.main.async
  • Guards against a nil sink after async hop with [weak self]
  • Removes the NotificationCenter observer in onCancel when the stream is cancelled

Reproduction

Observed in production application on Model: iPhone 13 Mini iOS Version:26.5.0 using thermal 1.2.1 when memory pressure + app background coincided with a thermal state notification.

Stack (abbreviated):

Fatal Exception: NSInternalInconsistencyException
SwiftThermalPlugin.onThermalStateChanged
NSProcessInfoNotifyThermalState

Test plan

  • Subscribe to onThermalStatusChanged on a physical iOS device
  • Trigger thermal/memory pressure (or wait for natural OS notification)
  • Confirm app does not crash and stream still receives updates
  • Cancel stream subscription and confirm no further observer callbacks

ProcessInfo.thermalStateDidChangeNotification can fire on a background
queue. Calling FlutterEventSink off the main thread triggers
NSInternalInconsistencyException in the Flutter engine.

Also remove the notification observer in onCancel.
@kevmo314
Copy link
Copy Markdown
Contributor

kevmo314 commented Jun 3, 2026

Thanks!

@kevmo314 kevmo314 merged commit 2f237e7 into muxable:main Jun 3, 2026
3 checks passed
@mrgnhnt96
Copy link
Copy Markdown

Hey @kevmo314! 👋

When will this change be available on pub.dev?

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants