Repository navigation
fix: stop leaking a native pattern composer on every parse - #294
Merged
Merged
Conversation
`PatternComposer_parsePattern` and `parsePatternWithSound` each build a brand-new native composer and file it under a fresh id. `parse()` overwrote the hook's id and dropped the old one on the floor, so only the last composer was ever released, on unmount. Every orphan keeps its `registerAudioResource` registration alive on the shared haptic engine — `releaseAudio()` only runs on that composer's own next parse, which never comes. The engine's audio capacity is finite, so after a handful of plays registration starts failing, `hasSound` falls back to false, and the preset plays the AudioSimulator rendering of its haptics instead of its own audio. Replaying or seeking a single preset is enough to get there, because both re-parse. Android leaks the same way, one SoundPool/MediaPlayer per parse.
`parse()` overwrote `continuousPlayerId`/`discretePlayerId` without handing the old players back, so every re-parse left two dead players in the engine's 20-slot registry. Once the registry filled, each new player evicted the oldest — which could be one still playing. Players are released before the audio resource: Core Haptics refuses to unregister a resource a live player still holds, and a registration that fails to unregister stays loaded in the engine.
piaskowyk
force-pushed
the
claude/pattern-composer-leak
branch
from
September 16, 2026 12:08
d860713 to
11ac8ff
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The bug
Playing a handful of audio-backed presets in a row — or replaying a single one — makes the app stop playing the clip and play the synthesized "haptics as sound" rendering instead. Restarting the app buys a few more plays, then it happens again.
Why
usePatternComposer().parse()leaks a native composer on every call.PatternComposer_parsePattern/parsePatternWithSoundeach build a brand-newPatternComposerand file it under a fresh id (Haptics.mm,PulsarModule.kt). The hook overwrotepatternIdand never released the previous one; only the unmount cleanup released an id, the last one.Each orphan keeps holding, on the single shared
CHHapticEngine:registerAudioResource(url:)registration —releaseAudio()only runs on that composer's own next parse or dispose, which never comes;CHHapticPatternPlayers in the engine's 20-slot registry.The engine's audio-resource capacity is finite, so after a few plays
registerAudioResourcethrows,HapticEngineWrapperlogsError registering audio resource: …and returnsnil,makeAudioEventreturnsnil,hasSoundgoes false — andplay()falls through to theAudioSimulatorbuffer, which is the audible rendering of the haptic curve. A fresh engine after a restart starts with empty capacity, hence "works for a few, then stops".Replaying or seeking a single preset is enough to get there: both re-parse.
Android leaks the same way, one
SoundPool/MediaPlayerper parse; it ends in silence rather than the simulator fallback.The fix
usePatternComposer.parsereleases the composer it held before building the next one — the idiomcreateBundle.tsalready uses. Fixes iOS and Android.PatternComposer.swiftdrops its own previous players before re-parsing. This was a second leak of the same family, reachable from the native SDK API, and it let the registry evict a player that was still playing. Order matters: players first, because Core Haptics refuses to unregister an audio resource a live player still holds.Verification
main, all pass here.playerStops → 0onmain, passes here.xcodebuild test -scheme Pulsar-Package: 63 tests in 11 suites, plus the 8 isolated CoreHaptics mock tests — all green.tsc --noEmitclean.