Skip to content

fix(perps): sentry issue improvements#25029

Merged
abretonc7s merged 15 commits intomainfrom
fix/perps/sentry-issues
Jan 23, 2026
Merged

fix(perps): sentry issue improvements#25029
abretonc7s merged 15 commits intomainfrom
fix/perps/sentry-issues

Conversation

@abretonc7s
Copy link
Copy Markdown
Contributor

@abretonc7s abretonc7s commented Jan 22, 2026

Description

This PR cleans up verbose Sentry error logging in the Perps feature. Many of these errors were logged for debugging connection issues but don't represent actual user-impacting problems. The changes focus on graceful handling during transient states (reconnection, initialization) and fixing a WebSocket race condition.

Key Issues Fixed:

  1. WebSocket Race Condition - Added transport ready gate to ensure WebSocket is in OPEN state before subscriptions
    • Prevents "WebSocket subscribe error: undefined" during initialization
    • Prevents subscription failures during reconnection window
  2. Subscription Client Race Condition - Added async gates (await ensureSubscriptionClient()) to guarantee client availability before use. Throws are preserved as safety assertions - they should never fire with the async gates in place. Exception: restoreSubscriptions() uses graceful return since it's invoked during reconnection when transport readiness is still being established.
  3. CLIENT_NOT_INITIALIZED - Added null-safe provider access method for UI components
  4. Market Pattern Validation - Added input normalization for HIP-3 DEX patterns

Technical Changes:

  • Added ensureTransportReady() method to HyperLiquidClientService that awaits SDK's transport.ready() before marking connection as CONNECTED
  • Made initialize() async with 10s timeout for WebSocket handshake
  • Updated handleConnectionDrop() to await transport ready before restoring subscriptions
  • Added ready check in restoreSubscriptions() before re-establishing subscriptions
  • Updated all 17 call sites in HyperLiquidProvider to await async initialization

Approach:

  • Uses SDK's built-in transport.ready() method - no SDK modification required
  • Async guarantees with safety assertions (throws preserved as bug indicators)
  • Graceful handling only in restoreSubscriptions() (legitimate transient state during reconnection)
  • Existing restoreSubscriptions() handles reconnection automatically

Changelog

CHANGELOG entry: Fixed Perps WebSocket race conditions and error handling during reconnection/initialization states

Related issues

Reduces verbose Sentry logging for:

Note: Event counts in Sentry are inflated due to verbose debug logging during development.

Manual testing steps

Feature: Perps error handling during reconnection

  Scenario: User views Perps while offline then connects
    Given user has the app open on Perps tab
    And device is offline

    When device reconnects to network
    Then Perps data should load without errors
    And no error toasts should appear

  Scenario: User switches accounts rapidly
    Given user is viewing Perps positions

    When user switches accounts multiple times quickly
    Then app should not crash
    And positions should update for final selected account

  Scenario: App backgrounded then foregrounded
    Given user is viewing Perps positions
    And app is backgrounded for a few minutes

    When user brings app to foreground
    Then positions should refresh correctly
    And no error toasts should appear

Screenshots/Recordings

Before

Verbose Sentry errors logged during WebSocket reconnection and initialization states (these were debug-level issues, not user-impacting)

After

  • WebSocket subscriptions only occur after transport is in OPEN state
  • Graceful degradation during transient states (debug logging instead of Sentry errors)
  • Subscriptions automatically restored via restoreSubscriptions() mechanism

Pre-merge author checklist

Pre-merge reviewer checklist

  • I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed).
  • I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.

Note

Strengthens Perps connection lifecycle and reduces transient Sentry noise.

  • Introduces ensureTransportReady() and makes HyperLiquidClientService.initialize() async; awaits transport.ready() during init and reconnection; improves health checks and reconnection cleanup
  • Refactors HyperLiquidProvider.ensureClientsInitialized() to async with a promise lock; awaits initialization at many read-only call sites; safely awaits pending init in toggleTestnet() and disconnect()
  • Adds PerpsController.getActiveProviderOrNull() and uses it in PerpsStreamManager to skip fetches during init/reinit
  • HyperLiquidSubscriptionService now awaits ensureSubscriptionClient() and verifies transport readiness in restoreSubscriptions(); subscriptions re-established only when ready
  • Normalizes HIP-3 market patterns and feature-flag parsing: trims input, strips quotes via new stripQuotes, and validates patterns in marketUtils
  • Extensive test updates to cover async flows, timeouts, and reconnection paths

Written by Cursor Bugbot for commit 56c79d8. This will update automatically on new commits. Configure here.

@github-actions
Copy link
Copy Markdown
Contributor

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.

@metamaskbot metamaskbot added the team-perps Perps team label Jan 22, 2026
@abretonc7s abretonc7s marked this pull request as ready for review January 22, 2026 10:08
@abretonc7s abretonc7s requested a review from a team as a code owner January 22, 2026 10:08
cursor[bot]

This comment was marked as outdated.

cursor[bot]

This comment was marked as outdated.

cursor[bot]

This comment was marked as outdated.

cursor[bot]

This comment was marked as outdated.

cursor[bot]

This comment was marked as outdated.

cursor[bot]

This comment was marked as outdated.

@github-actions github-actions bot added size-L and removed size-M labels Jan 23, 2026
@abretonc7s abretonc7s added the skip-sonar-cloud Only used for bypassing sonar cloud when failures are not relevant to the changes. label Jan 23, 2026
@abretonc7s abretonc7s enabled auto-merge January 23, 2026 04:51
@github-actions github-actions bot added size-XL and removed size-L labels Jan 23, 2026
@codecov-commenter
Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 84.61538% with 18 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.89%. Comparing base (129cc7c) to head (e111cff).
⚠️ Report is 40 commits behind head on main.

Files with missing lines Patch % Lines
...Perps/controllers/providers/HyperLiquidProvider.ts 81.63% 7 Missing and 2 partials ⚠️
...mponents/UI/Perps/providers/PerpsStreamManager.tsx 25.00% 2 Missing and 1 partial ⚠️
...I/Perps/services/HyperLiquidSubscriptionService.ts 62.50% 3 Missing ⚠️
...ents/UI/Perps/services/HyperLiquidClientService.ts 94.73% 0 Missing and 2 partials ⚠️
...components/UI/Perps/controllers/PerpsController.ts 83.33% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #25029      +/-   ##
==========================================
+ Coverage   79.88%   79.89%   +0.01%     
==========================================
  Files        4231     4245      +14     
  Lines      108283   109173     +890     
  Branches    22674    22832     +158     
==========================================
+ Hits        86503    87229     +726     
- Misses      15722    15886     +164     
  Partials     6058     6058              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@abretonc7s abretonc7s removed the skip-sonar-cloud Only used for bypassing sonar cloud when failures are not relevant to the changes. label Jan 23, 2026
cursor[bot]

This comment was marked as outdated.

Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 3 potential issues.

@github-actions
Copy link
Copy Markdown
Contributor

🔍 Smart E2E Test Selection

  • Selected E2E tags: SmokePerps
  • Risk Level: medium
  • AI Confidence: 92%
click to see 🤖 AI reasoning details

This PR makes significant changes to the Perps (Perpetuals) trading functionality, specifically:

  1. PerpsController.ts - Added getActiveProviderOrNull() method for graceful provider handling during initialization
  2. HyperLiquidProvider.ts - Major refactoring to make ensureClientsInitialized() async with promise-based locking to prevent race conditions
  3. HyperLiquidClientService.ts - Made initialize() async and added ensureTransportReady() to ensure WebSocket is in OPEN state before subscriptions
  4. HyperLiquidSubscriptionService.ts - Updated to await async methods and added transport readiness checks
  5. PerpsStreamManager.tsx - Uses new getActiveProviderOrNull() for graceful handling
  6. Utility changes - Added stripQuotes() function and whitespace normalization in market pattern validation

All changes are contained within the app/components/UI/Perps/ directory and are specifically related to the Perps trading feature. The changes fix race conditions in WebSocket initialization and improve connection handling reliability.

The SmokePerps tag is the appropriate and only necessary tag because:

  • All modified files are in the Perps module
  • No changes affect other features (accounts, confirmations, swaps, networks, etc.)
  • The E2E tests in e2e/specs/perps/ use the SmokePerps tag
  • These tests will validate the Perps add funds flow, positions, and trading functionality which rely on the modified WebSocket and provider initialization code

View GitHub Actions results

@sonarqubecloud
Copy link
Copy Markdown

@abretonc7s abretonc7s added this pull request to the merge queue Jan 23, 2026
Merged via the queue into main with commit e3951e9 Jan 23, 2026
67 of 69 checks passed
@abretonc7s abretonc7s deleted the fix/perps/sentry-issues branch January 23, 2026 12:01
@github-actions github-actions bot locked and limited conversation to collaborators Jan 23, 2026
@metamaskbot metamaskbot added the release-7.64.0 Issue or pull request that will be included in release 7.64.0 label Jan 23, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

release-7.64.0 Issue or pull request that will be included in release 7.64.0 size-XL team-perps Perps team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants