Repository: Chia-Network/chia-blockchain

Issue:
[Bug] dictionary changed size during iteration 
### What happened?

when switching between wallets 
![wallet_change_error_GUI](https://user-images.githubusercontent.com/116583441/234676534-8ff77ef9-f1ac-4301-b134-2b52bcc1a437.png)
occured.
after clicking [OK] and clicking on the desired wallet, it will load without issue

### Version

1.8.0rc3

### What platform are you using?

Windows

### What ui mode are you using?

GUI

### Relevant log output or stacktrace

_No response_


Discussion:
Interestingly, this is actually a python error message. @bramv-chia any chance you can find that error message in your debug log?
@paninaro 
2023-04-26T07:06:48.114 wallet chia.wallet.wallet_node    : INFO     Sync (trusted: True) duration was: 51.454047441482544
2023-04-26T07:06:48.148 wallet chia.rpc.rpc_server        : WARNING  Error while handling message: Traceback (most recent call last):
  File "chia\rpc\rpc_server.py", line 340, in safe_handle
  File "chia\rpc\rpc_server.py", line 331, in ws_api
  File "chia\rpc\wallet_rpc_api.py", line 317, in log_in
  File "chia\wallet\wallet_node.py", line 388, in _start_with_fingerprint
RuntimeError: dictionary changed size during iteration
Thanks @bramv-chia I'll transfer this to the blockchain repo and assign it.
@trepca @xdustinface the error message shown in the GUI is originating from the backend when logging into a wallet key. Bram provided the following debug.log output:

```
2023-04-26T07:06:48.114 wallet chia.wallet.wallet_node : INFO Sync (trusted: True) duration was: 51.454047441482544
2023-04-26T07:06:48.148 wallet chia.rpc.rpc_server : WARNING Error while handling message: Traceback (most recent call last):
File "chia\rpc\rpc_server.py", line 340, in safe_handle
File "chia\rpc\rpc_server.py", line 331, in ws_api
File "chia\rpc\wallet_rpc_api.py", line 317, in log_in
File "chia\wallet\wallet_node.py", line 388, in _start_with_fingerprint
RuntimeError: dictionary changed size during iteration
```

Looks to me like it the collection of wallets is updating while iterating over it:

```python
        # Populate the balance caches for all wallets
        for wallet_id in self.wallet_state_manager.wallets:
            await self.get_balance(wallet_id)
```

Resolving patch:
diff --git a/chia/wallet/wallet_node.py b/chia/wallet/wallet_node.py
--- a/chia/wallet/wallet_node.py
+++ b/chia/wallet/wallet_node.py
@@ -377,9 +377,6 @@ async def _start_with_fingerprint(
             self,
         )
 
-        if self.wallet_peers is None:
-            self.initialize_wallet_peers()
-
         if self.state_changed_callback is not None:
             self.wallet_state_manager.set_callback(self.state_changed_callback)
 
@@ -403,6 +400,10 @@ async def _start_with_fingerprint(
             index = await self.wallet_state_manager.puzzle_store.get_last_derivation_path()
             if index is None or index < self.wallet_state_manager.initial_num_public_keys - 1:
                 await self.wallet_state_manager.create_more_puzzle_hashes(from_zero=True)
+
+        if self.wallet_peers is None:
+            self.initialize_wallet_peers()
+
         return True
 
     def _close(self) -> None:


Test patch: