Repository navigation
fix(ios): prevent thread overload by reusing CHHapticEngine - #24
GorkaMinus wants to merge 2 commits into
Conversation
|
We independently hit this exact crash in production — calling Just adding a +1 with an independent reproduction in the hope of getting this reviewed/merged — it's a real crash and the fix here looks correct. Happy to help test on additional devices if that unblocks it. |
| private func resetIdleTimer(after duration: TimeInterval) { | ||
| idleTimer?.invalidate() | ||
| idleTimer = Timer.scheduledTimer( | ||
| withTimeInterval: duration + idleInterval, repeats: false | ||
| ) { [weak self] _ in | ||
| self?.shutdownEngine() | ||
| } | ||
| } |
There was a problem hiding this comment.
| private func resetIdleTimer(after duration: TimeInterval) { | |
| idleTimer?.invalidate() | |
| idleTimer = Timer.scheduledTimer( | |
| withTimeInterval: duration + idleInterval, repeats: false | |
| ) { [weak self] _ in | |
| self?.shutdownEngine() | |
| } | |
| } | |
| private func resetIdleTimer(after duration: TimeInterval) { | |
| idleTimer?.invalidate() | |
| DispatchQueue.main.async { [weak self] in | |
| guard let self else { return } | |
| self.idleTimer = Timer.scheduledTimer( | |
| withTimeInterval: duration + self.idleInterval, repeats: false | |
| ) { [weak self] _ in | |
| self?.shutdownEngine() | |
| } | |
| } | |
| } |
Timer.scheduledTimer uses the current thread's run loop. Fine on main thread, but if vibrate() is called from a background queue with no run loop, the timer never fires and the engine stays alive. Safer to force main.
| private func initializeEngine() { | ||
| do { | ||
| let engine = try CHHapticEngine() | ||
| engine.resetHandler = { [weak self] in | ||
| do { | ||
| try self?.hapticEngine?.start() | ||
| } catch { | ||
| self?.hapticEngine = nil | ||
| } | ||
| } | ||
| engine.stoppedHandler = { [weak self] _ in | ||
| self?.hapticEngine = nil | ||
| } | ||
| try engine.start() | ||
| hapticEngine = engine | ||
| } catch { | ||
| hapticEngine = nil | ||
| } | ||
| } |
There was a problem hiding this comment.
| private func initializeEngine() { | |
| do { | |
| let engine = try CHHapticEngine() | |
| engine.resetHandler = { [weak self] in | |
| do { | |
| try self?.hapticEngine?.start() | |
| } catch { | |
| self?.hapticEngine = nil | |
| } | |
| } | |
| engine.stoppedHandler = { [weak self] _ in | |
| self?.hapticEngine = nil | |
| } | |
| try engine.start() | |
| hapticEngine = engine | |
| } catch { | |
| hapticEngine = nil | |
| } | |
| } | |
| private func initializeEngine() { | |
| do { | |
| let engine = try CHHapticEngine() | |
| engine.resetHandler = { [weak self] in | |
| guard self?.hapticEngine === engine else { return } | |
| do { | |
| try engine.start() | |
| } catch { | |
| self?.hapticEngine = nil | |
| } | |
| } | |
| engine.stoppedHandler = { [weak self] _ in | |
| if self?.hapticEngine === engine { | |
| self?.hapticEngine = nil | |
| } | |
| } | |
| hapticEngine = engine | |
| try engine.start() | |
| } catch { | |
| hapticEngine = nil | |
| } | |
| } |
Inside resetHandler you use self?.hapticEngine?.start(), but the handler was registered on this specific engine. If another initializeEngine() runs meanwhile, self?.hapticEngine may point to a different one. Same issue in stoppedHandler.
Also, hapticEngine = engine runs after try engine.start(), so if a reset fires during startup, self?.hapticEngine is still nil and nothing happens.
My suggestion is to use the captured engine in the handlers, add an identity check, and assign the property before starting.
There was a problem hiding this comment.
Hi @GorkaMinus, tested it and reproduced the original issue on main with a burst of 200 vibrate calls.
Left two small suggestions on edge cases (handler identity + timer run loop). Other than that LGTM
Description
This PR addresses an issue where calling
Haptics.vibrate()rapidly (e.g., from a fast-scrolling picker in my case) causes the app to hang or crash on iOS. The problem stems from creating a newCHHapticEngineinstance on each call, which leads to excessive thread creation and eventual system overload.Solution:
CHHapticEngineinstance across multiple vibration callsduration + 5sof inactivity, so the idle countdown starts after the vibration finishesresetHandler(restarts engine after server recovery) andstoppedHandler(clears the reference on external stops like audio interruptions or app suspension)Change Type
Rationale / Problems Fixed
Continuation of ionic-team/capacitor-plugins#2340, moved here per maintainer guidance since the haptics plugin now lives in its own repository.
Related: #7
Creating a new
CHHapticEnginepervibrate()call spawns threads that accumulate faster than the system can reclaim them. Under rapid-fire usage (pickers, scroll-driven haptics), this leads to thread exhaustion, UI freezes, and eventual crashes.Tests or Reproductions
Haptics.vibrate()from a picker/scroll, confirm no freeze or crashvibrate(duration)with a long duration (e.g. 5s), confirm vibration completes fully before engine shuts downAudioServicesPlayAlertSoundon devices without haptic supportPlatforms Affected
Notes / Comments
This also addresses review feedback from the original PR (ionic-team/capacitor-plugins#2340):
supportsHapticscheck insideinitializeEngine()since it's already guarded in the callerduration + 5s) instead of a flat 5s countdownresetHandlerandstoppedHandlerfor proper engine lifecycle management following Apple's recommended patterns