Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

songs: fix stale partner data and mid-decode playback race in round stack

[._.]/ Adam Eivy64e8dd9937da
diff --git a/client/src/components/songs/RoundStack.jsx b/client/src/components/songs/RoundStack.jsx
index 736951d0ef..882c269652 100644
--- a/client/src/components/songs/RoundStack.jsx
+++ b/client/src/components/songs/RoundStack.jsx
@@ -27,6 +27,11 @@ const hasText = (v) => typeof v === 'string' && v.trim().length > 0;
export default function RoundStack({ songs = [] }) {
const [playing, setPlaying] = useState(false);
const playerRef = useRef(null);
+ // Bumped on every stop / takes-change / unmount. createLayeredPlayer only
+ // marks itself "playing" AFTER decoding, so a stop() issued mid-decode no-ops
+ // and the awaited play() would start the mix anyway. We capture this token
+ // before awaiting and silence the player if it advanced while we were decoding.
+ const playGenRef = useRef(0);
// Every saved take across all stacked songs, keyed by a song-namespaced id so
// two songs' recordings can't collide in the mixer's id map.
@@ -41,25 +46,34 @@ export default function RoundStack({ songs = [] }) {
// — otherwise navigating to another partner (or any songs-prop change) while
// ?stack=1 is open would leave the previous mix audibly playing under the new
// stack. Resetting `playing` keeps the button in sync with the silenced mix.
- useEffect(() => () => { playerRef.current?.stop(); setPlaying(false); }, [takes]);
+ useEffect(() => () => { playGenRef.current += 1; playerRef.current?.stop(); setPlaying(false); }, [takes]);
const stop = useCallback(() => {
+ playGenRef.current += 1; // invalidate any in-flight (still-decoding) play
playerRef.current?.stop();
setPlaying(false);
}, []);
const playAll = useCallback(async () => {
playerRef.current?.stop();
+ const gen = (playGenRef.current += 1);
const player = createLayeredPlayer(takes);
player.onEnded(() => setPlaying(false));
playerRef.current = player;
setPlaying(true);
- // A take URL that 404s or fails to decode rejects play() — without this the
- // button would stay stuck on "Stop" with no mix running (mirrors SongRecordings).
- await player.play().catch((err) => {
- toast.error(err?.message || 'Playback failed');
- setPlaying(false);
- });
+ try {
+ await player.play();
+ // Stopped or navigated away while decoding? The player only just marked
+ // itself playable, so stop it now before the scheduled sources sound.
+ if (playGenRef.current !== gen) player.stop();
+ } catch (err) {
+ // A take URL that 404s or fails to decode rejects play() — reset the button
+ // (mirrors SongRecordings) unless a newer action already superseded us.
+ if (playGenRef.current === gen) {
+ toast.error(err?.message || 'Playback failed');
+ setPlaying(false);
+ }
+ }
}, [takes]);
if (songs.length === 0) return null;
diff --git a/client/src/components/songs/RoundStack.test.jsx b/client/src/components/songs/RoundStack.test.jsx
index 1198bd68cc..94b3020efe 100644
--- a/client/src/components/songs/RoundStack.test.jsx
+++ b/client/src/components/songs/RoundStack.test.jsx
@@ -5,12 +5,17 @@ import toast from '../ui/Toast';
// Mock the audio mixer — jsdom has no Web Audio. Track every created player so
// tests can assert which takes were passed and that old players get stopped.
-const playerCalls = vi.hoisted(() => ({ takes: null, players: [], rejectPlay: false }));
+const playerCalls = vi.hoisted(() => ({ takes: null, players: [], rejectPlay: false, deferPlay: false, resolvePlay: null }));
vi.mock('../../lib/songPlayback', () => ({
createLayeredPlayer: (takes) => {
playerCalls.takes = takes;
const player = {
- play: vi.fn(() => (playerCalls.rejectPlay ? Promise.reject(new Error('boom')) : Promise.resolve())),
+ play: vi.fn(() => {
+ if (playerCalls.rejectPlay) return Promise.reject(new Error('boom'));
+ // deferPlay simulates a still-decoding mix the test resolves on demand.
+ if (playerCalls.deferPlay) return new Promise((res) => { playerCalls.resolvePlay = res; });
+ return Promise.resolve();
+ }),
stop: vi.fn(),
onEnded: vi.fn(),
};
@@ -49,6 +54,8 @@ describe('RoundStack', () => {
playerCalls.takes = null;
playerCalls.players = [];
playerCalls.rejectPlay = false;
+ playerCalls.deferPlay = false;
+ playerCalls.resolvePlay = null;
toast.error.mockClear();
});
@@ -103,4 +110,16 @@ describe('RoundStack', () => {
rerender(<MemoryRouter><RoundStack songs={[SONGS[0]]} /></MemoryRouter>);
expect(playerCalls.players[0].stop).toHaveBeenCalled();
});
+
+ it('does not start the mix if stopped while still decoding', async () => {
+ playerCalls.deferPlay = true; // play() stays pending until the test resolves it
+ renderStack(SONGS);
+ fireEvent.click(screen.getByRole('button', { name: /Play all parts/i }));
+ // User stops before decode finishes — the mixer's own stop() can't cancel a
+ // mid-decode play, so the generation guard must.
+ fireEvent.click(screen.getByRole('button', { name: /^Stop$/i }));
+ playerCalls.resolvePlay(); // decode "completes" after the stop
+ await waitFor(() => expect(screen.getByRole('button', { name: /Play all parts/i })).toBeTruthy());
+ expect(playerCalls.players[0].stop).toHaveBeenCalled();
+ });
});
diff --git a/client/src/pages/SongEditor.jsx b/client/src/pages/SongEditor.jsx
index 874a9271be..07b223279f 100644
--- a/client/src/pages/SongEditor.jsx
+++ b/client/src/pages/SongEditor.jsx
@@ -133,16 +133,18 @@ export default function SongEditor() {
return () => { cancelled = true; };
}, [id]);
- // Fetch once on mount — the all-songs list resolves partner titles + the
- // editor pick list and doesn't depend on which song is open, so it must not
- // re-fetch on every song navigation.
+ // Refresh the all-songs list whenever the open song changes — this component
+ // stays mounted across /songs/:id navigation, so a once-on-mount fetch would
+ // leave partner records (titles, saved takes) stale after editing a partner
+ // and navigating back. The list is small and single-user, so re-fetching on
+ // navigation is cheap and keeps the round stack honest.
useEffect(() => {
let cancelled = false;
listSongs({ silent: true })
.then((data) => { if (!cancelled) setAllSongs(data?.songs || []); })
.catch(() => { /* the page degrades to no partner resolution */ });
return () => { cancelled = true; };
- }, []);
+ }, [id]);
// Resolve this song's partner ids to records (skip any that no longer exist).
const partnerSongs = useMemo(() => {