Skip to content
5 changes: 5 additions & 0 deletions packages/perps-controller/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Bump `uuid` from `^14.0.0` to `^14.0.1` ([#11179](https://github.com/MetaMask/core/pull/11179))

### Fixed

- Prevent HyperLiquid `userFills` from going silent after a WebSocket reconnect when an in-flight subscribe races with `restoreSubscriptions` (same pending + generation pattern TWAP already uses) ([#11160](https://github.com/MetaMask/core/pull/11160))
- Without this, live fills can keep attaching to a dead pre-reconnect listener (MetaMask/metamask-extension#47287).

## [22.1.0]

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,27 @@ export class HyperLiquidSubscriptionService {
// Order fill subscriptions keyed by accountId (normalized: undefined -> 'default')
readonly #orderFillSubscriptions = new Map<string, ISubscription>();

// In-flight userFills subscribe calls, so concurrent subscribers / reconnect
// share one socket open instead of racing two opens onto the same map key.
readonly #pendingOrderFillSubscriptions = new Map<string, Promise<void>>();

// Bumped per account by teardown and reconnect so an in-flight open for
// that account discards its subscription instead of rehydrating a cleared
// map (same race TWAP already guards; without it live fills can stick to a
// dead pre-reconnect listener — MetaMask/metamask-extension#47287).
readonly #orderFillSubscriptionGenerations = new Map<string, number>();

#orderFillGeneration(normalizedAccountId: string): number {
return this.#orderFillSubscriptionGenerations.get(normalizedAccountId) ?? 0;
}

#bumpOrderFillGeneration(normalizedAccountId: string): void {
this.#orderFillSubscriptionGenerations.set(
normalizedAccountId,
this.#orderFillGeneration(normalizedAccountId) + 1,
);
}

// TWAP subscriptions keyed by accountId (normalized: undefined -> 'default')
readonly #twapOrderSubscriptions = new Map<string, ISubscription>();

Expand Down Expand Up @@ -2912,6 +2933,10 @@ export class HyperLiquidSubscriptionService {
});
this.#orderFillSubscriptions.delete(normalizedAccountId);
}
// Invalidate any open still in flight for this account: without this
// its continuation installs a live socket nobody is listening to.
this.#bumpOrderFillGeneration(normalizedAccountId);
this.#pendingOrderFillSubscriptions.delete(normalizedAccountId);
}
};
}
Expand All @@ -2934,17 +2959,79 @@ export class HyperLiquidSubscriptionService {
return;
}

const subscriptionClient = this.#clientService.getSubscriptionClient();
// Another caller is already opening this account's socket. Await it so
// subscribe + reconnect do not race two userFills opens onto one key.
// The owner of the pending open logs its failure, so swallow it here and
// fall through to open our own. Re-read after each wait: several waiters
// resume together and only the first should start the replacement open.
let pending = this.#pendingOrderFillSubscriptions.get(normalizedAccountId);
while (pending) {
await pending.catch(() => undefined);
if (this.#orderFillSubscriptions.has(normalizedAccountId)) {
return;
}
pending = this.#pendingOrderFillSubscriptions.get(normalizedAccountId);
}

// A discarded open resolves without registering; if the last subscriber
// left meanwhile, opening now would cache frames nobody is listening to.
const subscribers = this.#orderFillSubscribers.get(normalizedAccountId);
if (!subscribers || subscribers.size === 0) {
return;
}

// Captured once here, spanning the whole open including the client
// handshake. Capturing inside the open would let a bump that lands during
// the handshake be adopted as the baseline and silently forgotten.
const startGeneration = this.#orderFillGeneration(normalizedAccountId);
const subscriptionPromise = this.#createOrderFillSubscription(
normalizedAccountId,
accountId,
startGeneration,
);
this.#pendingOrderFillSubscriptions.set(
normalizedAccountId,
subscriptionPromise,
);
try {
await subscriptionPromise;
} finally {
// Delete by identity: a reconnect may have cleared the map and installed
// a newer open behind this one, which must not be evicted here.
if (
this.#pendingOrderFillSubscriptions.get(normalizedAccountId) ===
subscriptionPromise
) {
this.#pendingOrderFillSubscriptions.delete(normalizedAccountId);
}
}
}

/**
* Open one userFills subscription and register it for teardown.
*
* @param normalizedAccountId - Map key for this account ('default' when unset).
* @param accountId - Optional CAIP account ID to resolve the address from.
* @param startGeneration - This account's generation when the open began.
* @returns A promise that resolves once the subscription is registered.
*/
async #createOrderFillSubscription(
normalizedAccountId: string,
accountId: CaipAccountId | undefined,
startGeneration: number,
): Promise<void> {
// Bootstrap the client inline rather than recursing: a recursive call
// would re-read the generation after the handshake and adopt a bump that
// landed during it as its own baseline.
let subscriptionClient = this.#clientService.getSubscriptionClient();
if (!subscriptionClient) {
await this.#clientService.ensureSubscriptionClient(
this.#walletService.createWalletAdapter(),
);
const client = this.#clientService.getSubscriptionClient();
if (!client) {
subscriptionClient = this.#clientService.getSubscriptionClient();
if (!subscriptionClient) {
throw new Error('SubscriptionClient not available');
}
await this.#ensureOrderFillISubscription(accountId);
return;
}

const userAddress =
Expand All @@ -2954,6 +3041,13 @@ export class HyperLiquidSubscriptionService {
const subscription = await subscriptionClient.userFills(
{ user: userAddress },
(data: UserFillsWsEvent) => {
// A frame queued before an unsubscribe or reconnect must not mutate
// the replacement's cache or reach its subscribers.
if (
startGeneration !== this.#orderFillGeneration(normalizedAccountId)
) {
return;
}
// Build a Map for O(1) lookup instead of O(n) find per fill
const orderMap = new Map<string, string>();
if (this.#cachedOrders) {
Expand Down Expand Up @@ -3019,6 +3113,19 @@ export class HyperLiquidSubscriptionService {
},
);

// Teardown, clearAll, or a reconnect may have run while this open was in
// flight. Rehydrating the map here would leave a socket with no
// subscribers, or clobber the live replacement — so drop ours instead.
const subscribers = this.#orderFillSubscribers.get(normalizedAccountId);
if (
startGeneration !== this.#orderFillGeneration(normalizedAccountId) ||
this.#orderFillSubscriptions.has(normalizedAccountId) ||
!subscribers ||
subscribers.size === 0
) {
subscription.unsubscribe().catch(() => undefined);
return;
}
this.#orderFillSubscriptions.set(normalizedAccountId, subscription);
}

Expand Down Expand Up @@ -4956,8 +5063,19 @@ export class HyperLiquidSubscriptionService {

// Re-establish order fill subscriptions if there are fill subscribers
if (this.#orderFillSubscribers.size > 0) {
// Clear existing subscription references (they're dead after reconnection)
// Existing subscription references are dead after reconnection. Bump
// each account's generation first so a pre-reconnect open still in
// flight discards itself instead of occupying the map ahead of its
// replacement (MetaMask/metamask-extension#47287).
for (const normalizedAccountId of new Set([
...this.#orderFillSubscribers.keys(),
...this.#orderFillSubscriptions.keys(),
...this.#pendingOrderFillSubscriptions.keys(),
])) {
this.#bumpOrderFillGeneration(normalizedAccountId);
}
this.#orderFillSubscriptions.clear();
this.#pendingOrderFillSubscriptions.clear();

// Re-establish subscriptions for all accountIds with subscribers
// Note: normalizedAccountId is 'default' for undefined, need to convert back
Expand Down Expand Up @@ -5192,6 +5310,15 @@ export class HyperLiquidSubscriptionService {
])) {
this.#bumpTwapOrderGeneration(normalizedAccountId);
}
// Same for order fills: bump before subscriber keys are cleared so an
// in-flight userFills open cannot rehydrate after clearAll.
for (const normalizedAccountId of new Set([
...this.#orderFillSubscribers.keys(),
...this.#orderFillSubscriptions.keys(),
...this.#pendingOrderFillSubscriptions.keys(),
])) {
this.#bumpOrderFillGeneration(normalizedAccountId);
}

// Clear all local subscriber collections
this.#priceSubscribers.clear();
Expand All @@ -5204,13 +5331,14 @@ export class HyperLiquidSubscriptionService {
this.#marketDataSubscribers.clear();
this.#orderBookSubscribers.clear();

// Clear order fill subscriptions
// Clear order fill subscriptions; generations were bumped above.
this.#orderFillSubscriptions.forEach((subscription) => {
subscription.unsubscribe().catch(() => {
// Ignore errors during cleanup
});
});
this.#orderFillSubscriptions.clear();
this.#pendingOrderFillSubscriptions.clear();

// Clear TWAP subscriptions; generations were bumped above, before the
// subscriber keys naming those accounts were cleared.
Expand Down
Loading
Loading