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

fix: stop a late play() rejection from re-claiming the Security audio session (#4131)

[._.]/ Adam Eivy257516e4e7e1
diff --git a/client/src/components/meatspace/post/MorseTrainer.jsx b/client/src/components/meatspace/post/MorseTrainer.jsx
index 31c8c12b32..c07e546adc 100644
--- a/client/src/components/meatspace/post/MorseTrainer.jsx
+++ b/client/src/components/meatspace/post/MorseTrainer.jsx
@@ -288,6 +288,10 @@ function useKeyingDecoder({ unitMs, hz, ensureCtx, claimSession, releaseSession,
// reaches stopTone, so it hands the claim back on its way out.
claimSession();
const ctx = await ensureCtx().catch((err) => { releaseSession(); throw err; });
+ // Unmounting close()s and nulls the per-mount context, so a press parked in
+ // the unlock window resumes with nothing to sound into (same bail as
+ // CopyDrill.playPrompt). Release rather than throwing into createOscillator.
+ if (!ctx) { releaseSession(); return; }
// A fast tap can release (endPress → stopTone), or a newer press can start,
// before the first-press-only resume await settles. Bail if the key is no
// longer held (`!pressingRef.current`) OR a newer press superseded this one
diff --git a/client/src/pages/Security.jsx b/client/src/pages/Security.jsx
index fb1698ac3d..779b7e25f1 100644
--- a/client/src/pages/Security.jsx
+++ b/client/src/pages/Security.jsx
@@ -169,7 +169,14 @@ export default function Security() {
// Set up media sources when streaming starts
useEffect(() => {
- if (!streaming) return;
+ if (!streaming) return undefined;
+ // Pausing the element (stopMedia, a restart, unmount) rejects an in-flight
+ // play() with AbortError, and the rejection handler below sets up the
+ // analyser anyway — which would CLAIM the audio session again just after
+ // stopMedia released it, pinning the page output-only with nothing running
+ // to hand it back. `active` drops both continuations once this effect run is
+ // superseded.
+ let active = true;
const timestamp = Date.now();
@@ -183,13 +190,16 @@ export default function Security() {
audioRef.current.src = `/api/media/audio?t=${timestamp}`;
audioRef.current.play().then(() => {
- setupAudioAnalyser(audioRef.current);
+ if (active) setupAudioAnalyser(audioRef.current);
}).catch(() => {
+ if (!active) return;
// On mobile browsers, autoplay is often blocked - show interaction prompt
setAudioNeedsInteraction(true);
setupAudioAnalyser(audioRef.current); // Set up analyser anyway for when user enables
});
}
+
+ return () => { active = false; };
}, [streaming, videoEnabled, audioEnabled, setupAudioAnalyser]);
// Stop media stream
diff --git a/client/src/pages/Security.test.jsx b/client/src/pages/Security.test.jsx
index 3540d658dc..3aa42e06f5 100644
--- a/client/src/pages/Security.test.jsx
+++ b/client/src/pages/Security.test.jsx
@@ -25,8 +25,13 @@ class FakeAnalyser {
connect() { return this; }
}
+// Counted so a test can assert the analyser graph was NOT rebuilt — the session
+// claim lives inside setupAudioAnalyser, so "no new context" is the tell that no
+// stray continuation re-claimed.
+let contextsBuilt = 0;
+
class FakeAudioContext {
- constructor() { this.state = 'running'; this.destination = {}; this.closed = false; }
+ constructor() { contextsBuilt += 1; this.state = 'running'; this.destination = {}; this.closed = false; }
createAnalyser() { return new FakeAnalyser(); }
createMediaElementSource() { return { connect() { return this; } }; }
resume() { return Promise.resolve(); }
@@ -47,6 +52,7 @@ const startMonitor = async () => {
describe('Security monitor iOS audio session', () => {
beforeEach(() => {
+ contextsBuilt = 0;
window.AudioContext = FakeAudioContext;
navigator.audioSession = { type: 'auto' };
// The level meter's rAF loop would otherwise run forever under jsdom.
@@ -80,4 +86,28 @@ describe('Security monitor iOS audio session', () => {
unmount();
expect(sessionType()).toBe('auto');
});
+
+ // Pausing the element rejects an in-flight play() with AbortError, and the
+ // rejection handler sets the analyser up anyway (the autoplay-blocked path).
+ // Landing after Stop Media, that would re-claim a session nothing is left to
+ // release, pinning the page output-only for the rest of the SPA session.
+ it('does not re-claim when a play() rejection lands after the stream was stopped', async () => {
+ let rejectPlay;
+ window.HTMLMediaElement.prototype.play = vi.fn(() => new Promise((_, reject) => { rejectPlay = reject; }));
+
+ render(<Security />);
+ await act(async () => {});
+ fireEvent.click(screen.getByRole('button', { name: /Start Media/i }));
+ await screen.findByRole('button', { name: /Stop Media/i });
+ // play() is still pending, so the analyser graph does not exist yet.
+ expect(contextsBuilt).toBe(0);
+ expect(sessionType()).toBe('auto');
+
+ await act(async () => { fireEvent.click(screen.getByRole('button', { name: /Stop Media/i })); });
+ await act(async () => { rejectPlay(new DOMException('aborted', 'AbortError')); });
+ // The rejection must NOT build the graph now that the stream is stopped —
+ // building it is what re-claims the session.
+ expect(contextsBuilt).toBe(0);
+ expect(sessionType()).toBe('auto');
+ });
});