Skip to content

Commit fd999f1

Browse files
paullinatorsamholmes
authored andcommitted
Fix change server reconnection
Move reconnect logic from the error handler to the close handler so reconnection happens reliably after any disconnect. Add a closing flag to prevent reconnection when intentionally closed. Handle 'reconnecting' status in subscription filters so wallets are re-subscribed after the connection is re-established.
1 parent da4c174 commit fd999f1

3 files changed

Lines changed: 17 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- fixed: Change-server websocket reconnects reliably after disconnection
6+
57
## 2.40.0 (2026-01-26)
68

79
- added: Added change-server subscription timeout fallback.

src/core/currency/change-server-connection.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ export function connectChangeServer(
2626
callbacks: ChangeServerCallbacks
2727
): ChangeServerConnection {
2828
let ws: WebSocket
29+
let closing = false
30+
let reconnectTimeoutId: ReturnType<typeof setTimeout> | undefined
2931
function makeWs(): void {
3032
ws = new WebSocket(url)
3133
ws.binaryType = 'arraybuffer'
@@ -38,15 +40,17 @@ export function connectChangeServer(
3840
out.connected = false
3941
codec.handleClose()
4042
callbacks.handleDisconnect()
43+
// Reconnect after 5 seconds, unless intentionally closed:
44+
reconnectTimeoutId = setTimeout(() => {
45+
if (!closing) {
46+
makeWs()
47+
}
48+
}, 5000)
4149
})
4250

4351
ws.addEventListener('error', errEvent => {
4452
console.error('changeServer websocket error:', errEvent)
4553
ws.close()
46-
// Reconnect after 5 seconds:
47-
setTimeout(() => {
48-
makeWs()
49-
}, 5000)
5054
})
5155

5256
ws.addEventListener('open', () => {
@@ -87,6 +91,11 @@ export function connectChangeServer(
8791
},
8892

8993
close() {
94+
closing = true
95+
if (reconnectTimeoutId != null) {
96+
clearTimeout(reconnectTimeoutId)
97+
reconnectTimeoutId = undefined
98+
}
9099
ws.close()
91100
},
92101

src/core/currency/currency-pixie.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,8 @@ export const currency: TamePixie<RootProps> = combinePixies({
292292
.subscribe(subscribeParams)
293293
.catch(err => {
294294
input.props.log(`Failed to subscribe: ${String(err)}`)
295-
return [0] as SubscribeResult[]
295+
// Return failure result for each subscription in the batch:
296+
return subscribeParams.map(() => 0 as SubscribeResult)
296297
})
297298
results.push(...r)
298299
}

0 commit comments

Comments
 (0)