Skip to content

Commit cc69f2c

Browse files
LukasDeco0xbigz
authored andcommitted
fix: ws v2 subscriber hangs on async iterable loop (#1793)
1 parent 5ae39ae commit cc69f2c

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

sdk/src/accounts/webSocketAccountSubscriberV2.ts

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,19 @@ export class WebSocketAccountSubscriberV2<T> implements AccountSubscriber<T> {
9090
this.rpcSubscriptions = rpcSubscriptions;
9191
}
9292

93+
private async handleNotificationLoop(subscription: AsyncIterable<any>) {
94+
for await (const notification of subscription) {
95+
if (this.resubOpts?.resubTimeoutMs) {
96+
this.receivingData = true;
97+
clearTimeout(this.timeoutId);
98+
this.handleRpcResponse(notification.context, notification.value);
99+
this.setTimeout();
100+
} else {
101+
this.handleRpcResponse(notification.context, notification.value);
102+
}
103+
}
104+
}
105+
93106
async subscribe(onChange: (data: T) => void): Promise<void> {
94107
if (this.listenerId != null || this.isUnsubscribing) {
95108
if (this.resubOpts?.logResubMessages) {
@@ -109,6 +122,13 @@ export class WebSocketAccountSubscriberV2<T> implements AccountSubscriber<T> {
109122
const abortController = new AbortController();
110123
this.abortController = abortController;
111124

125+
this.listenerId = Math.random(); // Unique ID for logging purposes
126+
127+
if (this.resubOpts?.resubTimeoutMs) {
128+
this.receivingData = true;
129+
this.setTimeout();
130+
}
131+
112132
// Subscribe to account changes using gill's rpcSubscriptions
113133
const pubkey = this.accountPublicKey.toBase58();
114134
if (isAddress(pubkey)) {
@@ -121,23 +141,8 @@ export class WebSocketAccountSubscriberV2<T> implements AccountSubscriber<T> {
121141
abortSignal: abortController.signal,
122142
});
123143

124-
for await (const notification of subscription) {
125-
if (this.resubOpts?.resubTimeoutMs) {
126-
this.receivingData = true;
127-
clearTimeout(this.timeoutId);
128-
this.handleRpcResponse(notification.context, notification.value);
129-
this.setTimeout();
130-
} else {
131-
this.handleRpcResponse(notification.context, notification.value);
132-
}
133-
}
134-
}
135-
136-
this.listenerId = Math.random(); // Unique ID for logging purposes
137-
138-
if (this.resubOpts?.resubTimeoutMs) {
139-
this.receivingData = true;
140-
this.setTimeout();
144+
// Start notification loop without awaiting
145+
this.handleNotificationLoop(subscription);
141146
}
142147
}
143148

0 commit comments

Comments
 (0)