fix(perps): connection-aware ensureReady() to fix stale cache on slow connections cp-7.67.0#26324
fix(perps): connection-aware ensureReady() to fix stale cache on slow connections cp-7.67.0#26324abretonc7s merged 7 commits intomainfrom
Conversation
… connections TAT-2597 / TAT-2598: After preload PR merged, slow connections caused StreamChannels to exhaust 150 polling retries (30s) in ensureReady() and silently give up, leaving users with stale REST cache and no live WebSocket data — positions not appearing after trade, missing prices. - Add PerpsConnectionManager.waitForConnection() exposing init/reconnect promises so channels can await instead of blind-polling - StreamChannel.ensureReady() now detects isConnecting state and awaits the connection promise via awaitConnectionThenConnect() - Add [PERPS_DEBUG] instrumentation across StreamManager, ConnectionManager, and usePerpsLivePositions for diagnosing data flow issues - PriceStreamChannel: 33 retries → 0 on device after this fix
|
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. |
Strip ~150 lines of debug instrumentation added during TAT-2597/2598 investigation. Reverts all modified DevLogger calls to their original wording and removes logs that duplicate existing error handling or trace normal execution paths. The functional fix (connection-aware ensureReady, awaitConnectionThenConnect, waitForConnection) is unchanged.
…guard The deferConnect callback called connect() without clearing deferConnectTimer first. If ensureReady() then entered the isConnecting branch, awaitConnectionThenConnect() saw the stale truthy timer ID and returned immediately — silently stopping retries.
…layMs constant Capture sentinel reference in awaitConnectionThenConnect so promise handlers only null deferConnectTimer if it still matches, preventing orphaned timer references on disconnect/resubscribe races. Replace magic number 200 with PERPS_CONSTANTS.ConnectRetryDelayMs.
…ion, and isCurrentlyConnecting guard Cover previously untested code paths to bump new-code test coverage: - awaitConnectionThenConnect: early-exit, promise resolution, and rejection fallback - MarketDataChannel: isCurrentlyConnecting deferred connect guard - waitForConnection: initPromise and pendingReconnectPromise resolve/reject
🔍 Smart E2E Test Selection
click to see 🤖 AI reasoning detailsE2E Test Selection:
These changes affect the Perps WebSocket connection handling and could impact how the Perps feature initializes and reconnects. The PerpsStreamManager is imported by multiple Perps hooks (usePerpsLiveAccount, usePerpsLiveCandles, usePerpsLiveOrders, etc.) and views (PerpsTabView, TrendingView sections). Per tag descriptions:
Performance Test Selection: |
|



Description
Fixes TAT-2597 and TAT-2598: After the preload PR merged, slow connections caused StreamChannels to exhaust 150 polling retries (30s) in
ensureReady()and silently give up, leaving users with stale REST cache and no live WebSocket data — positions not appearing after trade, missing prices.Root Cause:
StreamChannel.ensureReady()used blind polling (isReadyevery 200ms × 150 retries) with no awareness of WebSocket connection state. On slow connections, the connection had not even established yet, so polling burned through all retries before data could arrive.Fix:
PerpsConnectionManager.waitForConnection()— exposes init/reconnect promises so channels canawaitinstead of blind-pollingStreamChannel.ensureReady()— detectsisConnectingstate and awaits the connection promise viaawaitConnectionThenConnect()Result: PriceStreamChannel retries dropped from 33 → 0 on device after this fix.
Changelog
CHANGELOG entry: Fixed stale cache on slow connections where positions and prices were not updating after a trade
Related issues
Fixes: TAT-2597, TAT-2598
Manual testing steps
Screenshots/Recordings
Before
After
Pre-merge author checklist
Pre-merge reviewer checklist
Note
Medium Risk
Touches Perps connection/stream reconnection timing and retry logic; mistakes could cause missed subscriptions or delayed real-time updates, though changes are localized and well-covered by tests.
Overview
Prevents Perps stream channels from burning through retry polling on slow connections by making
StreamChannel.ensureReady()detectisConnectingand wait on the connection manager before retryingconnect().Adds
PerpsConnectionManager.waitForConnection()(awaits init/reconnect promises, swallowing rejections) and introducesawaitConnectionThenConnect()with a sentinel timer to avoid duplicate awaits, plus a smalldeferConnect()timer cleanup fix and a sharedPERPS_CONSTANTS.ConnectRetryDelayMsconstant.Expands unit tests to cover the new await/guard behavior (duplicate-await prevention, resolve/reject fallbacks) and updates market-data channel tests to use the new retry delay constant.
Written by Cursor Bugbot for commit 7e0d599. This will update automatically on new commits. Configure here.