fix(perps): recover connection after app state changes cp-7.67.1#26780
fix(perps): recover connection after app state changes cp-7.67.1#26780
Conversation
|
CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes. |
🔍 Smart E2E Test Selection
click to see 🤖 AI reasoning detailsE2E Test Selection:
Tag selection rationale:
The changes are isolated to the Perps feature and don't affect other wallet functionality like accounts, networks, swaps, or identity features. Performance Test Selection: |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.
| } | ||
|
|
||
| await this.validateAndReconnect('appResume'); | ||
| } |
There was a problem hiding this comment.
Stale retry timer forces unnecessary reconnection on healthy connection
Medium Severity
handleAppStateChange does not call cancelNetworkRestoreRetry(), so pending retry timers from the network-restore path survive an app-resume reconnection. When such a timer fires after the connection was already restored, attemptNetworkRestoreReconnect calls validateAndReconnect with skipPing = true, which bypasses the health check that would detect the healthy connection. This forces an unnecessary disconnect-and-reconnect cycle, briefly disrupting live data streams that were just recovered.
Additional Locations (1)
There was a problem hiding this comment.
I wonder if this is going to cause unecessary reconnections
There was a problem hiding this comment.
I think the fix for this is calling this.cancelNetworkRestoreRetry() before await this.validateAndReconnect('appResume');
Draft PR here: #26803
I'm keeping it approved as is. The PR functionally fixes the reported bug for the incident. This could be seen as a follow up if you feel it's necessary


Description
Fixes Perps WebSocket connectivity issues when:
PerpsConnectionManagerstill reportsisConnected = true. No code path detected the stale connection or triggered reconnection.activestate, the existingAppState-based recovery (if any) never fires.Root Cause
PerpsConnectionManagerhad no lifecycle awareness of:AppStatetransitions (background → foreground)@react-native-community/netinfo)Arthur's prior fix (#26334) made
StreamChannel.ensureReady()connection-aware to avoid blind polling on slow connections, but it only helps when a reconnection is already in progress (isConnecting = true). After background resume or WiFi restore, nobody triggers the reconnection in the first place.Fix
AppStatelistener — onactive, cancels any pending grace period and runsvalidateAndReconnect()NetInfolistener — trackswasOfflinestate; on offline → online transition, runsvalidateAndReconnect()validateAndReconnect(context)— shared method that sends a lightweightping()health check to the active provider. If the ping fails (stale WebSocket), marks the connection as lost and triggersreconnectWithNewContext({ force: true })which reinitializes the controller, validates with a fresh health check, and preloads all stream subscriptions.cleanupStateMonitoring()Changelog
CHANGELOG entry: Fixed Perps WebSocket not reconnecting after app resume from background or WiFi/network toggle
Related issues
Fixes: connectivity loss after backgrounding app, WiFi off/on not recovering Perps data
Manual testing steps
Screenshots/Recordings
Before
After backgrounding or WiFi toggle, Perps shows stale data with no automatic recovery. User must navigate away and back to restore the connection.
After
Connection automatically recovers via health-check ping and force reconnection. Live data resumes within seconds.
Pre-merge author checklist
Pre-merge reviewer checklist
Note
Medium Risk
Touches Perps connection lifecycle and reconnection paths; regressions could cause reconnect loops or delayed/stuck loading during flaky connectivity.
Overview
Improves Perps WebSocket resilience by adding AppState and NetInfo listeners in
PerpsConnectionManagerto detect background→foreground and offline→online transitions, validate the connection via providerping(), and force a reconnect when stale.Adds network-restore retry/backoff knobs (
NetworkRestoreMaxRetries,NetworkRestoreRetryBaseMs) and ensures cleanup of new subscriptions/timers on teardown; reconnection now explicitly callsPerpsController.disconnect()beforeinit()to avoid skipping re-init on a dead socket.Updates
usePerpsHomeDatato treat WebSocket-backed sections (positions/orders/activity) as loading whileisConnecting, preventing brief empty-state flashes during reconnection.Written by Cursor Bugbot for commit b3aab14. This will update automatically on new commits. Configure here.