Set keychain_proxy to None in await_closed() to support reinitialization (#11075)

* Set keychain_proxy to None in await_closed() to support reinitialization.

* Added `shutting_down` param to _await_closed() to control whether the keychain_proxy is closed.
This commit is contained in:
Jeff 2022-04-06 20:14:49 -07:00 committed by GitHub
parent c238ce17ba
commit d35c414c09
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 9 deletions

View File

@ -140,7 +140,7 @@ class Farmer:
self.harvester_cache: Dict[str, Dict[str, HarvesterCacheEntry]] = {}
async def ensure_keychain_proxy(self) -> KeychainProxy:
if not self.keychain_proxy:
if self.keychain_proxy is None:
if self.local_keychain:
self.keychain_proxy = wrap_local_keychain(self.local_keychain, log=self.log)
else:
@ -212,13 +212,15 @@ class Farmer:
def _close(self):
self._shut_down = True
async def _await_closed(self):
async def _await_closed(self, shutting_down: bool = True):
if self.cache_clear_task is not None:
await self.cache_clear_task
if self.update_pool_state_task is not None:
await self.update_pool_state_task
if self.keychain_proxy is not None:
await self.keychain_proxy.close()
if shutting_down and self.keychain_proxy is not None:
proxy = self.keychain_proxy
self.keychain_proxy = None
await proxy.close()
await asyncio.sleep(0.5) # https://docs.aiohttp.org/en/stable/client_advanced.html#graceful-shutdown
self.started = False

View File

@ -157,7 +157,7 @@ class WalletRpcApi:
"""
if self.service is not None:
self.service._close()
peers_close_task: Optional[asyncio.Task] = await self.service._await_closed()
peers_close_task: Optional[asyncio.Task] = await self.service._await_closed(shutting_down=False)
if peers_close_task is not None:
await peers_close_task

View File

@ -143,7 +143,7 @@ class WalletNode:
self.LONG_SYNC_THRESHOLD = 200
async def ensure_keychain_proxy(self) -> KeychainProxy:
if not self.keychain_proxy:
if self.keychain_proxy is None:
if self.local_keychain:
self.keychain_proxy = wrap_local_keychain(self.local_keychain, log=self.log)
else:
@ -259,7 +259,7 @@ class WalletNode:
if self._secondary_peer_sync_task is not None:
self._secondary_peer_sync_task.cancel()
async def _await_closed(self):
async def _await_closed(self, shutting_down: bool = True):
self.log.info("self._await_closed")
if self.server is not None:
@ -269,8 +269,10 @@ class WalletNode:
if self.wallet_state_manager is not None:
await self.wallet_state_manager._await_closed()
self.wallet_state_manager = None
if self.keychain_proxy is not None:
await self.keychain_proxy.close()
if shutting_down and self.keychain_proxy is not None:
proxy = self.keychain_proxy
self.keychain_proxy = None
await proxy.close()
await asyncio.sleep(0.5) # https://docs.aiohttp.org/en/stable/client_advanced.html#graceful-shutdown
self.logged_in = False
self.wallet_peers = None