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_state_manager.py b/chia/wallet/wallet_state_manager.py
--- a/chia/wallet/wallet_state_manager.py
+++ b/chia/wallet/wallet_state_manager.py
@@ -515,9 +515,9 @@ async def set_sync_mode(self, target_height: uint32) -> AsyncIterator[uint32]:
         if self.log.level == logging.DEBUG:
             self.log.debug(f"set_sync_mode enter {await self.blockchain.get_finished_sync_up_to()}-{target_height}")
         async with self.lock:
+            self._sync_target = target_height
             start_time = time.time()
             start_height = await self.blockchain.get_finished_sync_up_to()
-            self._sync_target = target_height
             self.log.info(f"set_sync_mode syncing - range: {start_height}-{target_height}")
             self.state_changed("sync_changed")
             try:
@@ -527,7 +527,6 @@ async def set_sync_mode(self, target_height: uint32) -> AsyncIterator[uint32]:
                     f"set_sync_mode failed - range: {start_height}-{target_height}, seconds: {time.time() - start_time}"
                 )
             finally:
-                self._sync_target = None
                 self.state_changed("sync_changed")
                 if self.log.level == logging.DEBUG:
                     self.log.debug(
@@ -535,6 +534,7 @@ async def set_sync_mode(self, target_height: uint32) -> AsyncIterator[uint32]:
                         f"get_finished_sync_up_to: {await self.blockchain.get_finished_sync_up_to()}, "
                         f"seconds: {time.time() - start_time}"
                     )
+                self._sync_target = None
 
     async def get_confirmed_spendable_balance_for_wallet(self, wallet_id: int, unspent_records=None) -> uint128:
         """


Test patch: