Skip to content
This repository was archived by the owner on Mar 11, 2026. It is now read-only.

fix: handle orphan subscription messages during WebSocket reconnection#549

Open
demora33 wants to merge 2 commits into
alchemyplatform:masterfrom
demora33:fix/handle-orphan-subscription-messages
Open

fix: handle orphan subscription messages during WebSocket reconnection#549
demora33 wants to merge 2 commits into
alchemyplatform:masterfrom
demora33:fix/handle-orphan-subscription-messages

Conversation

@demora33
Copy link
Copy Markdown

Fixes #547

Problem

During WebSocket reconnection, handleMessage() can crash due to timing-related race conditions when in-flight messages arrive before subscriptions are fully restored.

Crash 1: Malformed messages (line 500)

TypeError: Cannot read properties of undefined (reading 'subscription')
at AlchemyWebSocketProvider.handleMessage (alchemy-websocket-provider.ts:500:39)

Some messages pass isSubscriptionEvent() but do not contain a params object.
Since isSubscriptionEvent() only checks that the message is not a response (id is undefined), accessing message.params.subscription can throw.

Crash 2: Orphan subscription messages (line ~505)

TypeError: Cannot read properties of undefined (reading 'method')

This occurs when a virtualId still exists in virtualIdsByPhysicalId but the corresponding entry has already been removed from virtualSubscriptionsById

The existing non-null assertion (!) causes a crash when accessing subscription.method.

Both issues are race conditions that happen during the reconnection window while SturdyWebSocket is re-establishing the connection and re-subscribing.

Root Cause Analysis Timeline:

  1. WebSocket is connected with active subscriptions
  2. Network interruption causes disconnection
  3. Alchemy server still has in-flight messages
  4. SturdyWebSocket reconnects automatically
  5. handleReopen() clears and rebuilds subscription mappings
  6. An in-flight message arrives during this window
  7. handleMessage() processes the message while internal state is inconsistent
  8. Accessing undefined properties throws a TypeError

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error with websocket reconnection

1 participant