Skip to content

fix(notifications): bound the custom sound's dedupe window so a stalled element cannot silence every later notification - #15938

Closed
yzxcj797 wants to merge 2 commits into
stablyai:mainfrom
yzxcj797:fix/completion-notification-sound-15933
Closed

yzxcj797 wants to merge 2 commits into
stablyai:mainfrom
yzxcj797:fix/completion-notification-sound-15933

Conversation

@yzxcj797

Copy link
Copy Markdown
Contributor

What

The custom notification sound's dedupe window is now bounded in time. A small createNotificationSoundPlayback module (src/preload/notification-sound-playback.ts) owns the in-flight flag and releases it on ended, error, or a 10-second time bound, whichever comes first — replacing the inline pair of module-level variables in the preload whose flag cleared only on the two events.

Why (issue #15933)

playSound's dedupe gate (if (!options?.force && isNotificationSoundPlaying) return { played: false, reason: 'deduped' }) existed to drop replays while a sound was still ringing. But the flag was released only by the Audio element's ended/error listeners. An element that stalls — an OS audio route change (Bluetooth/HDMI disconnect), a device sleep mid-play, a decode stall — fires neither event, so the flag latched true forever. Every subsequent completion notification then short-circuited to 'deduped', a reason the renderer wrapper deliberately does not warn about (it means expected burst coalescing) — so the failure was both permanent-until-restart and completely silent. That matches the report's "intermittent, timing/state dependent" exactly: one stall event, and sounds stop from then on.

The time bound makes one stall cost at most one missed sound. Normal sounds end well under 10s; force (the settings test button) bypasses the gate entirely and is unaffected; intentionally muted/suppressed notifications never reach playSound at all.

How

  • begin(audio) marks playback, wires ended/error, and arms setTimeout(finish, MAX_NOTIFICATION_SOUND_PLAYBACK_MS); finish is idempotent and unhooks everything.
  • A later begin releases the previous window first, so a stuck element cannot leak into the next notification; forceRelease() covers the dispose/reload paths (sound file change).
  • The preload's playSound flow is otherwise unchanged — same results, same IPC, same cache.

Testing

New src/preload/notification-sound-playback.test.ts (fake timers + fake audio element):

  • releases on ended; releases on error;
  • the regression: a stalled element (no events ever) releases the window at exactly the bound — one tick before it is still playing, at the bound it is not;
  • a finished playback's late fallback timer cannot release the next window early;
  • begin while one is stuck releases the previous, and the stuck element's late ended cannot release the fresh window;
  • forceRelease drops the window, unhooks listeners, and the later release() from the audio.play() rejection path is a safe no-op.

Full typecheck clean; the preload/dispatch suites pass. (Two unrelated failures visible in a full src/renderer/src/lib run — a cmd.exe quoting case and the activation-surface census — are load flakes: both pass in isolation on a tree with these changes present.)

Fixes #15933

…ed element cannot silence every later notification

The dedupe flag cleared only on the Audio element's ended/error events;
an OS audio route change or device sleep mid-play fires neither, latching
the flag true forever — every subsequent completion notification then
returned 'deduped' silently, which the renderer treats as expected burst
coalescing. Playback state moves to a small module that releases the
window on ended, error, or a 10s time bound, whichever comes first.

Fixes stablyai#15933
@coderabbitai

coderabbitai Bot commented Aug 22, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 27ab3297-dc72-4a58-a061-5407cd5d5f95

📥 Commits

Reviewing files that changed from the base of the PR and between 8dc94a4 and 59ac803.

📒 Files selected for processing (1)
  • src/preload/notification-sound-playback.test.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 8 remain after this review.


📝 Walkthrough

Walkthrough

The change adds a notification sound playback manager with event-based cleanup and a 10-second timeout fallback. The preload script now uses this manager for deduplication, playback start, release handling, and state clearing. Tests cover normal completion, errors, stalled playback, replacement, stale timers and events, forced release, listener cleanup, and idempotent release.

Merge Risk: 🔵 Low · up to 59ac8

The change prevents a stalled notification sound from permanently suppressing later sounds by releasing the dedupe window after 10 seconds. It is mergeable with owner awareness that the timer-isolation regression test should clearly verify that an earlier playback cannot release a later one.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 5 functions across 3 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: bounding the notification sound deduplication window to prevent permanent suppression after stalled playback.
Description check ✅ Passed The description clearly covers the problem, implementation, testing, and Fixes #15933; it omits several template headings, but the essential review information is present.
Linked Issues check ✅ Passed The changes address issue #15933 by bounding stalled playback, adding regression coverage, and preserving silence for muted or suppressed notifications.
Out of Scope Changes check ✅ Passed The changes are limited to notification playback lifecycle management, preload integration, and focused regression tests.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
src/preload/notification-sound-playback.ts (1)

1-6: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Shorten the rationale comment.

Reduce this six-line comment to one concise line. Move the full incident detail to issue #15933 or a design document.

As per coding guidelines, comments must be concise, non-obvious, and brief; prefer one line.

Source: Coding guidelines


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 24345875-2aa2-45d9-bd0b-d93ceec95ba0

📥 Commits

Reviewing files that changed from the base of the PR and between 6b51ef4 and 8dc94a4.

📒 Files selected for processing (3)
  • src/preload/index.ts
  • src/preload/notification-sound-playback.test.ts
  • src/preload/notification-sound-playback.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.

Comment on lines +82 to +92
it('stops the fallback timer once ended fires, and a late timer fires nothing', () => {
const playback = createNotificationSoundPlayback()
const first = fakeAudio()
playback.begin(first.audio)
first.emit('ended')

// The timer for the finished playback must not release the NEXT one early.
const second = fakeAudio()
playback.begin(second.audio)
vi.advanceTimersByTime(MAX_NOTIFICATION_SOUND_PLAYBACK_MS + 5)
expect(playback.isPlaying()).toBe(false)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Test the old timer separately.

The two timers have the same deadline. This assertion passes when the second timer releases playback, even if the first timer was not cleared.

Advance time before ending the first playback. Then assert that playback remains active at the old timer deadline and releases only at the second timer deadline.

Proposed test change
     const playback = createNotificationSoundPlayback()
     const first = fakeAudio()
     playback.begin(first.audio)
+    vi.advanceTimersByTime(1)
     first.emit('ended')

-    // The timer for the finished playback must not release the NEXT one early.
     const second = fakeAudio()
     playback.begin(second.audio)
-    vi.advanceTimersByTime(MAX_NOTIFICATION_SOUND_PLAYBACK_MS + 5)
+    vi.advanceTimersByTime(MAX_NOTIFICATION_SOUND_PLAYBACK_MS - 1)
+    expect(playback.isPlaying()).toBe(true)
+
+    vi.advanceTimersByTime(1)
     expect(playback.isPlaying()).toBe(false)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
it('stops the fallback timer once ended fires, and a late timer fires nothing', () => {
const playback = createNotificationSoundPlayback()
const first = fakeAudio()
playback.begin(first.audio)
first.emit('ended')
// The timer for the finished playback must not release the NEXT one early.
const second = fakeAudio()
playback.begin(second.audio)
vi.advanceTimersByTime(MAX_NOTIFICATION_SOUND_PLAYBACK_MS + 5)
expect(playback.isPlaying()).toBe(false)
it('stops the fallback timer once ended fires, and a late timer fires nothing', () => {
const playback = createNotificationSoundPlayback()
const first = fakeAudio()
playback.begin(first.audio)
vi.advanceTimersByTime(1)
first.emit('ended')
const second = fakeAudio()
playback.begin(second.audio)
vi.advanceTimersByTime(MAX_NOTIFICATION_SOUND_PLAYBACK_MS - 1)
expect(playback.isPlaying()).toBe(true)
vi.advanceTimersByTime(1)
expect(playback.isPlaying()).toBe(false)

…nes differ

Coderabbit review on stablyai#15938: with both timers armed at the same tick the
test could not show the finished window's timer leaving the next one
alone.
@yzxcj797

Copy link
Copy Markdown
Contributor Author

Right — with both timers armed at the same tick the deadlines coincided, so the test proved nothing about separation. Pushed a fix: the second begin now happens half a bound after the first (deadlines t=10000 vs t=15000), and the assertions cross the first deadline while the second window is still open.

@nwparker

nwparker commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

Closing as superseded: the linked issue's requested behavior is already implemented on main by #15940.

The global already-playing/dedupe flag was removed, so stalled audio cannot latch later notifications silent. Later delivered completions restart one cached player; no timeout gate is needed.

Thanks for the contribution. This closes the redundant PR without deleting its branch.

@nwparker nwparker closed this Oct 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Completion notifications intermittently play no sound

2 participants