From 00b99a9fa0e292b042914ac509c29ce8ba29d274 Mon Sep 17 00:00:00 2001 From: cyan Date: Thu, 30 Oct 2025 02:16:18 +0100 Subject: [PATCH] fix: report isClosed from ProxySocket and use it to reconnect (#2617) --- cw_bitcoin/lib/electrum.dart | 11 ++++------- cw_core/lib/utils/proxy_socket/abstract.dart | 1 + cw_core/lib/utils/proxy_socket/insecure.dart | 12 ++++++------ cw_core/lib/utils/proxy_socket/secure.dart | 12 ++++++------ cw_core/lib/utils/proxy_socket/socks.dart | 8 ++++---- 5 files changed, 21 insertions(+), 23 deletions(-) diff --git a/cw_bitcoin/lib/electrum.dart b/cw_bitcoin/lib/electrum.dart index bd4a36c72..5dfdd6195 100644 --- a/cw_bitcoin/lib/electrum.dart +++ b/cw_bitcoin/lib/electrum.dart @@ -35,7 +35,6 @@ class SocketTask { class ElectrumClient { ElectrumClient() : _id = 0, - _isConnected = false, _tasks = {}, _errors = {}, unterminatedString = ''; @@ -43,7 +42,7 @@ class ElectrumClient { static const connectionTimeout = Duration(seconds: 5); static const aliveTimerDuration = Duration(seconds: 4); - bool get isConnected => _isConnected && socket != null; + bool get isConnected => socket != null && socket?.isClosed == false; ProxySocket? socket; void Function(ConnectionStatus)? onConnectionStatusChange; int _id; @@ -51,7 +50,6 @@ class ElectrumClient { Map get tasks => _tasks; final Map _errors; ConnectionStatus _connectionStatus = ConnectionStatus.disconnected; - bool _isConnected; Timer? _aliveTimer; String unterminatedString; @@ -451,7 +449,7 @@ class ElectrumClient { Future call( {required String method, List params = const [], Function(int)? idCallback}) async { - if (!_isConnected || socket == null) return null; + if (!isConnected) return null; final completer = Completer(); _id += 1; @@ -466,7 +464,7 @@ class ElectrumClient { Future callWithTimeout( {required String method, List params = const [], int timeout = 5000}) async { try { - if (!_isConnected || socket == null) return null; + if (!isConnected) return null; final completer = Completer(); _id += 1; @@ -564,8 +562,7 @@ class ElectrumClient { void _setConnectionStatus(ConnectionStatus status) { onConnectionStatusChange?.call(status); _connectionStatus = status; - _isConnected = status == ConnectionStatus.connected; - if (!_isConnected) { + if (!isConnected) { try { socket?.destroy(); } catch (_) {} diff --git a/cw_core/lib/utils/proxy_socket/abstract.dart b/cw_core/lib/utils/proxy_socket/abstract.dart index c22bce3bc..9e704c272 100644 --- a/cw_core/lib/utils/proxy_socket/abstract.dart +++ b/cw_core/lib/utils/proxy_socket/abstract.dart @@ -42,6 +42,7 @@ abstract class ProxySocket { Future close(); void destroy(); void write(String data); + bool get isClosed; StreamSubscription> listen(Function(Uint8List event) onData, {Function (Object error)? onError, Function ()? onDone, bool cancelOnError = true}); ProxyAddress get address; } \ No newline at end of file diff --git a/cw_core/lib/utils/proxy_socket/insecure.dart b/cw_core/lib/utils/proxy_socket/insecure.dart index 2bcbec8c2..1b1b189fe 100644 --- a/cw_core/lib/utils/proxy_socket/insecure.dart +++ b/cw_core/lib/utils/proxy_socket/insecure.dart @@ -10,15 +10,15 @@ class ProxySocketInsecure implements ProxySocket { ProxySocketInsecure(this.socket); - bool _isClosed = false; + bool isClosed = false; ProxyAddress get address => ProxyAddress(host: socket.remoteAddress.host, port: socket.remotePort); @override Future close() async { try { - if (_isClosed) return; - _isClosed = true; + if (isClosed) return; + isClosed = true; return socket.close(); } catch (e) { printV("ProxySocketInsecure: close: $e"); @@ -29,8 +29,8 @@ class ProxySocketInsecure implements ProxySocket { @override void destroy() async { try { - if (_isClosed) return; - _isClosed = true; + if (isClosed) return; + isClosed = true; socket.destroy(); } catch (e) { printV("ProxySocketInsecure: destroy: $e"); @@ -41,7 +41,7 @@ class ProxySocketInsecure implements ProxySocket { @override void write(String data) { try { - if (_isClosed) { + if (isClosed) { printV("ProxySocketInsecure: write: socket is closed"); return; } diff --git a/cw_core/lib/utils/proxy_socket/secure.dart b/cw_core/lib/utils/proxy_socket/secure.dart index bf499d8ab..b281392cd 100644 --- a/cw_core/lib/utils/proxy_socket/secure.dart +++ b/cw_core/lib/utils/proxy_socket/secure.dart @@ -8,7 +8,7 @@ import 'package:cw_core/utils/proxy_socket/abstract.dart'; class ProxySocketSecure implements ProxySocket { final SecureSocket socket; - bool _isClosed = false; + bool isClosed = false; ProxySocketSecure(this.socket); @@ -17,8 +17,8 @@ class ProxySocketSecure implements ProxySocket { @override Future close() async { try { - if (_isClosed) return; - _isClosed = true; + if (isClosed) return; + isClosed = true; return socket.close(); } catch (e) { printV("ProxySocketSecure: close: $e"); @@ -29,8 +29,8 @@ class ProxySocketSecure implements ProxySocket { @override void destroy() async { try { - if (_isClosed) return; - _isClosed = true; + if (isClosed) return; + isClosed = true; socket.destroy(); } catch (e) { printV("ProxySocketSecure: destroy: $e"); @@ -41,7 +41,7 @@ class ProxySocketSecure implements ProxySocket { @override void write(String data) { try { - if (_isClosed) { + if (isClosed) { printV("ProxySocketSecure: write: socket is closed"); return; } diff --git a/cw_core/lib/utils/proxy_socket/socks.dart b/cw_core/lib/utils/proxy_socket/socks.dart index d004f94f8..e62b84bf3 100644 --- a/cw_core/lib/utils/proxy_socket/socks.dart +++ b/cw_core/lib/utils/proxy_socket/socks.dart @@ -7,7 +7,7 @@ import 'package:socks_socket/socks_socket.dart'; class ProxySocketSocks implements ProxySocket { final SOCKSSocket socket; - bool _isClosed = false; + bool isClosed = false; ProxySocketSocks(this.socket); @override @@ -16,8 +16,8 @@ class ProxySocketSocks implements ProxySocket { @override Future close() async { try { - if (_isClosed) return; - _isClosed = true; + if (isClosed) return; + isClosed = true; await socket.close(); } catch (e) { printV("ProxySocketSocks: close: $e"); @@ -31,7 +31,7 @@ class ProxySocketSocks implements ProxySocket { @override void write(String data) { try { - if (_isClosed) { + if (isClosed) { printV("ProxySocketSocks: write: socket is closed"); return; }