From 3dbbffaf5ced37f3874f3c2cb121cc6c8167234f Mon Sep 17 00:00:00 2001
From: Matthew Fosse <matt@fosse.co>
Date: Thu, 25 Jul 2024 10:22:55 -0500
Subject: [PATCH] electrum stream potential fix

---
 cw_bitcoin/lib/electrum_wallet.dart | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/cw_bitcoin/lib/electrum_wallet.dart b/cw_bitcoin/lib/electrum_wallet.dart
index 4e35c8e1e..84be047bc 100644
--- a/cw_bitcoin/lib/electrum_wallet.dart
+++ b/cw_bitcoin/lib/electrum_wallet.dart
@@ -261,6 +261,7 @@ abstract class ElectrumWalletBase
   void Function(FlutterErrorDetails)? _onError;
   Timer? _autoSaveTimer;
   static const int _autoSaveInterval = 30;
+  StreamSubscription<dynamic>? _receiveStream;
 
   Future<void> init() async {
     await walletAddresses.init();
@@ -312,7 +313,8 @@ abstract class ElectrumWalletBase
           isSingleScan: doSingleScan ?? false,
         ));
 
-    await for (var message in receivePort) {
+    _receiveStream?.cancel();
+    _receiveStream = receivePort.listen((var message) async {
       if (message is Map<String, ElectrumTransactionInfo>) {
         for (final map in message.entries) {
           final txid = map.key;
@@ -378,7 +380,7 @@ abstract class ElectrumWalletBase
         syncStatus = message.syncStatus;
         await walletInfo.updateRestoreHeight(message.height);
       }
-    }
+    });
   }
 
   void _updateSilentAddressRecord(BitcoinSilentPaymentsUnspent unspent) {
@@ -455,6 +457,7 @@ abstract class ElectrumWalletBase
     try {
       syncStatus = ConnectingSyncStatus();
 
+      await _receiveStream?.cancel();
       await electrumClient.close();
 
       electrumClient.onConnectionStatusChange = _onConnectionStatusChange;
@@ -1141,6 +1144,7 @@ abstract class ElectrumWalletBase
   @override
   Future<void> close() async {
     try {
+      await _receiveStream?.cancel();
       await electrumClient.close();
     } catch (_) {}
     _autoSaveTimer?.cancel();