fix(react): recover from StoreError: DESTROYED on React <Activity> hide/reveal#1587
Merged
mihar-22 merged 2 commits intoJun 30, 2026
Merged
Conversation
|
| Name | Link |
|---|---|
| 🔨 Latest commit | a746227 |
|
@ronald-urbina is attempting to deploy a commit to the Mux Team on Vercel. A member of the Team first needs to authorize it. |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
mihar-22
approved these changes
Jun 23, 2026
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.
resolves #1583
Problem
When Player.Provider is used inside a React subtree (e.g. Next.js cacheComponents), navigating away and back throws StoreError: DESTROYED.
Why it happens:
useDestroy defers store.destroy() to a setTimeout(0) so React StrictMode's synchronous cleanup+remount can cancel it. That works because StrictMode's cycle is synchronous. With , the gap between cleanup and reveal is asynchronous — the timeout fires before React gets a chance to re-run effects, leaving the store destroyed. When Activity reveals the subtree, useState returns the same (now-destroyed) store reference and store.attach() throws.
Fix
Added a destroyed-store guard in the attach effect in create-player.tsx. If store.destroyed is true when the effect runs, a fresh store is created via setStore and the effect returns early. The resulting re-render re-runs the effect with a live store and attaches normally.
if (store.destroyed) {
setStore(createStore()(combine(...config.features)));
return;
}
StrictMode is unaffected — the timeout is cancelled before it fires, so store.destroyed is always false during StrictMode's re-mount.
Tests
Two tests added to the Provider describe block:
Activity recovery — destroys the store mid-flight, triggers the attach effect via setMedia, asserts no throw and a fresh store instance
StrictMode invariant — flushes fake timers after a StrictMode render, asserts the same store instance and that the deferred destroy was cancelled
Note
Medium Risk
Touches core player lifecycle and store recreation on attach; behavior is narrow (destroyed-store guard) but wrong timing could leak stores or double-attach in edge cases.
Overview
Fixes
StoreError: DESTROYEDwhenPlayer.Provideris hidden and shown again via React<Activity>(e.g. Next.js cache components). DeferreduseDestroycleanup can run in the async gap before effects re-run, so the sameuseStatestore is still destroyed whenstore.attach()runs.The attach
useEffectnow checksstore.destroyedwhenmediais present: it **setStore**s a new combined store and bails once so the next run attaches with a live instance. StrictMode behavior is unchanged because the pending destroy is still cancelled on the synchronous remount.Adds Provider tests for Activity-style destroy +
setMediarecovery (new store, no throw) and for StrictMode (single store after flushing timers).Reviewed by Cursor Bugbot for commit 4351b36. Bugbot is set up for automated code reviews on this repo. Configure here.