Skip to content

fix: prevent WebSocket reconnect race condition on app resume#35

Merged
wizzomafizzo merged 2 commits intomainfrom
fix/websocket-reconnect-race-condition
Feb 4, 2026
Merged

fix: prevent WebSocket reconnect race condition on app resume#35
wizzomafizzo merged 2 commits intomainfrom
fix/websocket-reconnect-race-condition

Conversation

@wizzomafizzo
Copy link
Member

Summary

  • Track the 500ms setTimeout in immediateReconnect() so subsequent calls cancel any pending timer
  • Clear the timer in pauseHeartbeat() to prevent connections opening while backgrounded
  • Clear the timer in cleanup() for thorough resource cleanup

Fixes intermittent issue where the app gets stuck in "reconnecting" state after resuming from background.

Track the 500ms setTimeout in immediateReconnect() so subsequent calls
cancel any pending timer. This fixes a race condition when both app
resume and network status change trigger immediateReconnect() in quick
succession, which could leave the connection stuck in "reconnecting"
state.

Also clear the timer in pauseHeartbeat() to prevent connections from
opening while the app is in the background.
@codecov
Copy link

codecov bot commented Feb 4, 2026

Codecov Report

❌ Patch coverage is 81.81818% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/lib/transport/WebSocketTransport.ts 81.81% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

Verify that:
- Multiple rapid immediateReconnect() calls don't create multiple
  connection attempts (second call cancels first timer)
- pauseHeartbeat() cancels any pending immediate reconnect to prevent
  connections opening while the app is backgrounded
@wizzomafizzo wizzomafizzo merged commit 6cd9463 into main Feb 4, 2026
2 checks passed
@wizzomafizzo wizzomafizzo deleted the fix/websocket-reconnect-race-condition branch February 4, 2026 07:48
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