Skip to content

fix: queue drain continues in background tabs and across chat switches#55

Open
samueljklee wants to merge 1 commit intomainfrom
fix/queue-drain-background-tab
Open

fix: queue drain continues in background tabs and across chat switches#55
samueljklee wants to merge 1 commit intomainfrom
fix/queue-drain-background-tab

Conversation

@samueljklee
Copy link
Contributor

Problem

When the browser tab is not in focus, queued chat messages don't get sent. Two scenarios:

  1. Tab not visible (switched to another app): The queue countdown uses recursive setTimeout(tick, 1000). Browsers aggressively throttle setTimeout in background tabs — after ~5 minutes, Chrome throttles to 1 per minute, and with Tab Freezing, JS can be completely paused. A 5-second countdown could take minutes or never fire.

  2. Switched to another chat (same browser tab): When creating a new chat, the previous session's queue was actively cleared (clearSessionQueue(prevKey)), deleting all queued messages.

Changes

  • Wall-clock countdownstartCountdown now uses Date.now() vs a fixed deadline instead of tick-decrement. Even if the browser throttles setTimeout from 1s to 60s, the next tick sees real wall time has elapsed and fires the callback immediately.

  • Resume drain on tab focusvisibilitychange handler now calls tryDrainQueue() when the tab regains focus, catching any timers that were frozen while backgrounded.

  • Preserve queue across sessions — Removed clearSessionQueue(prevKey) when creating a new chat. Queue items are preserved and drain when the user switches back.

  • Clearer drain guards — Split the combined guard into two explicit checks: session-switched-away (preserves queue for later) vs execution-started (backs off).

Testing

  1. Open a chat, send a message, while AI is executing queue another message
  2. Switch to a different app/tab — queue should still drain after countdown
  3. Switch to a different chat — queue should be preserved and drain when you switch back

@samueljklee samueljklee force-pushed the fix/queue-drain-background-tab branch 2 times, most recently from c41f142 to e26ecbf Compare March 24, 2026 20:20
(includes debug logging for background drain - remove before merge)

🤖 Generated with [Amplifier](https://github.com/microsoft/amplifier)

Co-Authored-By: Amplifier <240397093+microsoft-amplifier@users.noreply.github.com>
@samueljklee samueljklee force-pushed the fix/queue-drain-background-tab branch from e26ecbf to 58870aa Compare March 25, 2026 16:00
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.

1 participant