Repository navigation
fix(perps): fix flaky TP/SL e2e — confirm mocked WS subscriptions and stop dropping positions during the TP/SL window - #47796
Conversation
…onResponse The perps WebSocket mock answered subscribe requests on a `subscribed` channel, but @nktkas/hyperliquid only matches `subscriptionResponse` echoes. Every subscription therefore timed out after 10 s and the SDK removed its listener, silently dropping later clearinghouseState, userFills, openOrders and price pushes. Co-authored-by: javiergarciavera <javiergarciavera@users.noreply.github.com>
…indow PerpsStreamManager dropped WebSocket position pushes for 3 s after an optimistic TP/SL update. The background only emits positions when they change, so a fill or close that arrived in that window was never resent and the closed position stayed on screen. Hold the latest push instead and apply it when the window ends, unless a newer REST snapshot or a reset supersedes it. Co-authored-by: javiergarciavera <javiergarciavera@users.noreply.github.com>
Simulate the fill as soon as the UI confirms the TP/SL. The stream now keeps delivering after 10 s and position pushes received during the optimistic window are applied when it ends, so no sleep is needed. Co-authored-by: javiergarciavera <javiergarciavera@users.noreply.github.com>
✨ Files requiring CODEOWNER review ✨👨🔧 @MetaMask/perps (4 files, +139 -21)
|
Builds ready [e5e5624]
⚡ Performance Benchmarks (Total: 🟢 8 pass · 🟡 2 warn · 🔴 3 fail)
Bundle Size Diffs [🚨 Warning! Bundle size has increased!]
|
|
|
Stress results for
Deterministic check, close pushed 15 s after the save: 0/2 pass with the old mock (every subscription logs |
|
No release label on PR. Adding release label release-13.58.0 on PR, as PR was added to branch 13.58.0 when release was cut. |



Description
Fixes the flaky
Perps Take Profit / Stop Losse2e tests (simulates take-profit fill …and its stop-loss sibling). The flake showed up as a timeout inwaitForTradeCtaButtons(): the position never cleared afterpushPositionClosed().Root cause: the perps WS mock never confirmed subscriptions
buildSubscribedConfirmation(test/e2e/tests/perps/mocks/websocketDefaultMocks.ts) replied tosubscribeon achannel: 'subscribed'frame.@nktkas/hyperliquid@0.33.3only resolves subscribe requests fromsubscriptionResponseechoes, as the real API sends and as the function's own JSDoc says. So in every perps e2e test:clearinghouseState,openOrders,userFills,webData3,allMids,assetCtxs,candleand others) timed out after the SDK's 10 s default.WebSocketSubscriptionManager.subscriberemoves the channel listener it had registered.The TP test pushes the close about 10–11 s after
clearinghouseStatesubscribes, so it passed or failed depending on runner speed. In the failing CI attempt the gap was 22.672 → 33.31 s, or 10.6 s. On the passing retry it was 30.441 → 39.675 s, or 9.2 s. The first test in a fresh browser starts slowest, which is why the TP test, not SL, fails on CI.How this was found and proven (with temporary probes in the background and SDK, not committed):
clearinghouseStatecallback in the background after the push. The reconcile POST response had arrived on the same socket 800 ms earlier.Request timed out after 10000 mswith the listener removed for every channel, exactly 10 s after subscribing.The same artifact explains #47287 ("userFills … only delivers its initial snapshot"). It also explains the
fastAssetCtxs/assetCtxsresubscriptions every ~10 s visible in every CI log.Why #47643 didn't fix it
#47643 forced a fresh REST read when Perps Activity refetches. That fixes the later Activity step (missing "Closed long" row), which was also a symptom of the dead
userFillslistener. The current failure happens before Activity:waitForTradeCtaButtons()times out because theclearinghouseStatelistener is gone. The earlier attempts (#47165: stateful REST mock and rebroadcasts; #45323) also work around missing frames without addressing why they're missing.The hypothesis in the ticket was checked and only partly holds:
Perps Take Profit / Stop Loss simulates...#47165 the mock's REST and WSclearinghouseStatealready return the pushed state. In CI the reconcile response was sent 800 ms before the close on the same FIFO socket.WEBSOCKET_BLOCK_MS": not the cause here, since the push came ≥3.36 s after the optimistic update. It is still a real product gap, fixed below.Changes
channel: 'subscriptionResponse'. The generic'subscribe'fallback also matchesunsubscribeframes, so those confirm too.PerpsStreamManager): a WSpositionsupdate received during the 3 s TP/SL window was dropped. The background (HyperLiquidSubscriptionService) only emits positions when their hash changes, so the four 0/200/600/1400 ms rebroadcasts reach the UI as one event, and a dropped update is never resent. A real TP fill or close in that window left the closed position on screen; the 2.5 s reconcile refetch only rescues fills that land before it runs. The manager now keeps the latest held update and applies it when the window ends. A REST snapshot pushed afterwards or a reset/account change discards it, which keeps arrival-order semantics.driver.delay(3200)from both tests. The fill is simulated as soon as the UI confirms the TP/SL; no sleeps remain. No skip of these tests exists onmain(#47645 was closed unmerged).Changelog
CHANGELOG entry: Fixed a bug where a Perps position could stay visible after it closed right after its TP/SL was edited
Related issues
Fixes:
Related: #47287, #47643, #47645, #45323, failing run https://github.com/MetaMask/metamask-extension/actions/runs/42770861905/job/129680144075
Manual testing steps
Testing evidence
ui/providers/perps/PerpsStreamManager.test.ts141/141, with 4 new tests; 2 of them fail without the product change. Related suitesui/providers/perps,update-tpslandui/hooks/perps/stream: 377/377.yarn lint:changedandyarn lint:tscpass.deep-link-route-perps(36) andmoney-account-withdraw(2).perps-tpsl.spec.tson Firefox under heavy CPU contention (14 busy loops on 8 cores), which reproduced the flake onmain(2 failures in ~27 runs). Results are in the comment below.Pre-merge author checklist
Pre-merge reviewer checklist
Note
Medium Risk
Changes Perps position stream timing after TP/SL edits and fixes all perps e2e WebSocket subscription behavior; behavior is covered by new unit tests but affects live trading UI state.
Reviewed by Cursor Bugbot for commit e5e5624. Bugbot is set up for automated code reviews on this repo. Configure here.