Skip to content

Fix mobile sleep disconnects, visibility reconnect bug, and reload race conditions - #524

Open
BemreSTR wants to merge 2 commits into
schlagmichdoch:masterfrom
BemreSTR:fix/reconnect-stability-mobile-sleep
Open

BemreSTR wants to merge 2 commits into
schlagmichdoch:masterfrom
BemreSTR:fix/reconnect-stability-mobile-sleep

Conversation

@BemreSTR

Copy link
Copy Markdown

Summary

This pull request fixes a collection of critical stability and reconnection bugs, most notably resolving the 2-year-old issue #247 (where mobile devices are dropped offline while selecting files or taking photos in the camera) and #498 (stuck offline on tab focus/visibility change).

It strictly follows the repository's "Stability always comes first" guideline with zero protocol or UI changes.


Root Causes & Fixes

1. Mobile Suspension Timeout (Fixes #247)

  • Problem: When a user on Android or iOS opens the system gallery or camera, the mobile OS suspends JavaScript execution in the browser tab. The server previously enforced an aggressive 5-second keepalive timeout (Date.now() - lastBeat > 5000). If a user took even 6 seconds to browse photos or snap a picture, the server dropped the connection, broadcasted peer-left, and the other device saw them go offline.
  • Fix: Increased the server keepalive timeout threshold to 30 seconds. This gives mobile users ample time to pick files or take pictures without being evicted.
  • Fix: Added explicit peer.socket.on('close') listener so real tab closures on desktop are cleaned up immediately without waiting for the timeout.

2. Auto-Reconnect on Visibility Change Bug (Fixes #498, #260)

  • Problem: In public/scripts/network.js:296, _onVisibilityChange checked:
    if (window.hiddenProperty) return;
    window.hiddenProperty is defined as the string 'hidden' (or vendor prefixed). Because a non-empty string is always truthy in JavaScript, if (window.hiddenProperty) always evaluated to true and returned immediately on every single visibility change event! PairDrop never actually attempted to auto-reconnect when switching back to the tab.
  • Fix: Changed the check to evaluate the document property document[window.hiddenProperty], and proactively trigger reconnect if disconnected.

3. Ghost peer-left Race Condition on Tab Reload / Reconnect

  • Problem: When a tab reloads, sessionStorage preserves the peer_id. The client establishes a new WebSocket with the same peer_id. When the new connection joins the room, the dying old socket was subsequently cleaned up by disconnect/timeout, which deleted the room entry and broadcasted a phantom peer-left (disconnect: true) to all other devices, prematurely destroying their WebRTC connection to the newly reconnected tab.
  • Fix: In _leaveRoom(), added guard if (this._rooms[roomId][peer.id] !== peer) return;. If a newer connection has already taken over the room entry for that peer.id, the dying old socket will never delete the room entry nor broadcast peer-left. Additionally, reconnecting with the same peer.id terminates the superseded socket cleanly without emitting peer-left.

4. WebRTC Disconnect Debounce & BFCache Preservation

  • WebRTC Debounce: Per WebRTC standards, connectionState === 'disconnected' is often a transient state during ICE candidate rechecks (e.g. Wi-Fi handover). Firing peer-disconnected immediately killed connections on brief glitches. It is now debounced for 5 seconds to allow recovery before tearing down the peer.
  • BFCache: In pagehide, ignore events where e.persisted === true so tabs placed in browser page cache do not eagerly sever their active WebRTC connections.
  • Array Cleanup: Fixed addRoomSecret / removeRoomSecret in server/peer.js to use includes and filter instead of in and delete on Arrays.

Verification

  • Tested with Node.js WebSocket client simulation: verified that when a peer reconnects with the same peer_id, active message routing switches to the new socket, and closing the old socket does NOT evict the new socket from the room.
  • Verified node --check syntax on all files.

Fixes #247
Fixes #498
Relates to #260, #478

…nd timeout

- Server: increase keepalive timeout threshold from 5s to 30s so mobile
  browsers suspended while choosing files or taking photos are not
  prematurely dropped (fixes schlagmichdoch#247).
- Server: listen to socket 'close' for prompt tab-close cleanup.
- Server: prevent ghost peer-left broadcasts when a reloaded tab reconnects
  with the same peerId; ensure dying old sockets never evict newly
  reconnected sockets from rooms.
- Server: fix array membership and deletion on roomSecrets in Peer.
- Client: fix _onVisibilityChange to check document[window.hiddenProperty]
  instead of truthy string window.hiddenProperty, which previously
  prevented any auto-reconnection on tab focus (fixes schlagmichdoch#498).
- Client: ignore pagehide when e.persisted (bfcache).
- Client: debounce WebRTC transient 'disconnected' state before tearing
  down peer to allow ICE candidate recovery.
Copilot AI balanced review requested due to automatic review settings October 16, 2026 09:02

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@SaturnusDJ

SaturnusDJ commented Nov 10, 2026 •

Copy link
Copy Markdown

Hello @BemreSTR , I tried your patch on a few affected phones with specific ways of selecting files. On one phone the selection window can be kept open 15 instead of 10 seconds now, on another phone the connection drops after just 5 seconds. Increasing the number "30000" further does not make any difference. It thus seems the patch is not working here.

Edit: This setup uses Coturn, it might matter.

dayowe pushed a commit to dayowe/PairDrop that referenced this pull request Nov 25, 2026
Partial port of 0a7b466 from schlagmichdoch#524. This takes the visibility-state check.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants