HomePhabricator

[chronik-client] Add handling for WebSocket closure

Description

[chronik-client] Add handling for WebSocket closure

Summary:
In _websocketUrlConnects, timeoutFailure did not close the WS, added a closure.

Test Plan:
I wrote a test script by overwriting the WebSocket's close method to count the number of times it was called. Then directly tested _websocketUrlConnects, simulating normal and timeout scenarios. Before modification, in non-timeout mode (5000ms), close should be called once (due to testWs.onopen), in timeout mode (1ms), close was called 0 times, indicating that nodes couldn't be properly closed during timeout. After modification, in non-timeout mode (5000ms), close is called once (same as before modification), in timeout mode (1ms), close is called twice because testWs.close(); immediately triggers testWs.onerror, which demonstrates that after the modification, WebSocket closure is guaranteed even after timeout.

```ts

// alitatest2.ts

import { FailoverProxy } from './src/failoverProxy';

import WebSocket from 'isomorphic-ws';

async function simpleWsCloseTest() {

console.log("Starting WebSocket connection and close test...");

// Record the number of WebSocket closes

let closeCount = 0;

// Save the original close method

const originalClose = WebSocket.prototype.close;

// Replace close method for monitoring

WebSocket.prototype.close = function(code?: number, data?: string | Buffer) {

closeCount++;

console.log(`WebSocket close call #${closeCount}`);

return originalClose.call(this, code, data);

};

try {

// Create proxy

const proxy = new FailoverProxy('https://chronik.e.cash');

// Directly call private method (for test purposes only)

// @ts-ignore - Ignore TypeScript warning about accessing private property

const result = await proxy._websocketUrlConnects('wss://chronik.e.cash/ws');

console.log(`Connection result: ${result ? 'success' : 'failure'}`);

console.log(`Total WebSocket.close() calls: ${closeCount}`);

} catch (error) {

console.error('Test error:', error);

} finally {

WebSocket.prototype.close = originalClose;

console.log("Test completed, closing ws");

}

}

simpleWsCloseTest();

Reviewers: Fabien, bytesofman, emack, O1 Bitcoin ABC, #bitcoin_abc

Reviewed By: bytesofman, emack, O1 Bitcoin ABC, #bitcoin_abc

Differential Revision: https://reviews.bitcoinabc.org/D17949

Details

Provenance
alitayinAuthored on Apr 18 2025, 03:21
alitayinPushed on Apr 22 2025, 20:26
Reviewer
Restricted Owners Package
Differential Revision
D17949: [chronik-client] Add handling for WebSocket closure
Parents
rABC56115c774db2: init: avoid unsetting service bits from `nLocalServices`
Branches
Unknown
Tags
Unknown