Fix effect cleanup race condition and unmanaged timers in synthutils#7457
Open
sahu-virendra-1908 wants to merge 3 commits into
Open
Fix effect cleanup race condition and unmanaged timers in synthutils#7457sahu-virendra-1908 wants to merge 3 commits into
sahu-virendra-1908 wants to merge 3 commits into
Conversation
Contributor
Author
|
@sum2it @omsuneri @walterbender @ssz2605 kindly review this PR. Happy to make any improvements or changes if required. |
Contributor
|
🧪 Jest Test Results ✅ All Jest tests passed! This PR is ready to merge. Coverage: Statements: 48.03% | Branches: 39.54% | Functions: 52.75% | Lines: 48.43% |
ssz2605
reviewed
Jun 13, 2026
ssz2605
left a comment
Collaborator
There was a problem hiding this comment.
I noticed this PR includes blocks.js changes (cycle detection and iterative _calculateDragGroup() traversal) in addition to the synth cleanup work. These seem unrelated to the issue described in the PR. Would it make sense to split those changes into a separate PR so the synth cleanup fix can be reviewed independently?
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.
What this fixes
Prevents unmanaged effect-cleanup callbacks from executing against disposed synth instances after playback is stopped.
The current implementation of
_performNotes()creates per-note Tone.js effect nodes such as:and schedules their cleanup using a raw:
callback.
Because these timers are not managed by Music Blocks' timer infrastructure, they remain active after playback is stopped and instruments are disposed.
When the delayed cleanup eventually executes, it may attempt to:
or perform other operations on synth instances that have already been disposed.
This creates stale cleanup activity, can interfere with subsequent playback sessions, and leaves dynamically created effect nodes outside centralized cleanup handling.
Related Issue: [https://github.com//issues/7400]
Root cause
Current cleanup logic:
The cleanup callback captures references to:
and executes long after note playback has completed.
If playback is stopped before the timeout fires:
disposes the synths, but the pending cleanup callback still executes later.
This can result in cleanup operations running against already-disposed audio objects and creates a gap between timer management and instrument lifecycle management.
What I changed
Replaced the raw cleanup timer with a managed guarded timer:
and added instrument epoch validation:
The cleanup callback now verifies that the instrument generation is still valid before attempting synth reconnection logic.
If instruments have already been disposed:
the callback safely disposes temporary effect/filter nodes and exits without interacting with the disposed synth.
Added pending effect tracking
Registered dynamically created effect nodes for centralized cleanup:
This allows temporary effect nodes to participate in global cleanup and prevents them from being left outside the disposal lifecycle.
Result
Testing
Verified with:
Results:
PR Category
Checklist
npm run lintandnpx prettier --check .with no errors.