network shutdown safety belts
This commit is contained in:
@@ -302,7 +302,6 @@ class Daemon(DaemonThread):
|
|||||||
if self.network:
|
if self.network:
|
||||||
self.print_error("shutting down network")
|
self.print_error("shutting down network")
|
||||||
self.network.stop()
|
self.network.stop()
|
||||||
self.network.join()
|
|
||||||
self.on_stop()
|
self.on_stop()
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
|
|||||||
@@ -836,29 +836,32 @@ class Network(PrintError):
|
|||||||
self._jobs.append(job)
|
self._jobs.append(job)
|
||||||
await self.main_taskgroup.spawn(job)
|
await self.main_taskgroup.spawn(job)
|
||||||
|
|
||||||
|
@log_exceptions
|
||||||
async def _stop(self, full_shutdown=False):
|
async def _stop(self, full_shutdown=False):
|
||||||
self.print_error("stopping network")
|
self.print_error("stopping network")
|
||||||
try:
|
try:
|
||||||
await asyncio.wait_for(self.main_taskgroup.cancel_remaining(), timeout=2)
|
await asyncio.wait_for(self.main_taskgroup.cancel_remaining(), timeout=2)
|
||||||
except asyncio.TimeoutError: pass
|
except (asyncio.TimeoutError, asyncio.CancelledError) as e:
|
||||||
self.main_taskgroup = None
|
self.print_error(f"exc during main_taskgroup cancellation: {repr(e)}")
|
||||||
|
try:
|
||||||
assert self.interface is None
|
self.main_taskgroup = None
|
||||||
assert not self.interfaces
|
self.interface = None # type: Interface
|
||||||
self.connecting.clear()
|
self.interfaces = {} # type: Dict[str, Interface]
|
||||||
self.server_queue = None
|
self.connecting.clear()
|
||||||
self.trigger_callback('network_updated')
|
self.server_queue = None
|
||||||
|
if not full_shutdown:
|
||||||
if full_shutdown:
|
self.trigger_callback('network_updated')
|
||||||
self._run_forever.set_result(1)
|
finally:
|
||||||
|
if full_shutdown:
|
||||||
|
self._run_forever.set_result(1)
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
assert self._thread != threading.current_thread(), 'must not be called from network thread'
|
assert self._thread != threading.current_thread(), 'must not be called from network thread'
|
||||||
fut = asyncio.run_coroutine_threadsafe(self._stop(full_shutdown=True), self.asyncio_loop)
|
fut = asyncio.run_coroutine_threadsafe(self._stop(full_shutdown=True), self.asyncio_loop)
|
||||||
fut.result()
|
try:
|
||||||
|
fut.result(timeout=2)
|
||||||
def join(self):
|
except (asyncio.TimeoutError, asyncio.CancelledError): pass
|
||||||
self._thread.join(1)
|
self._thread.join(timeout=1)
|
||||||
|
|
||||||
async def _ensure_there_is_a_main_interface(self):
|
async def _ensure_there_is_a_main_interface(self):
|
||||||
if self.is_connected():
|
if self.is_connected():
|
||||||
|
|||||||
@@ -882,7 +882,7 @@ def log_exceptions(func):
|
|||||||
raise
|
raise
|
||||||
except BaseException as e:
|
except BaseException as e:
|
||||||
print_ = self.print_error if hasattr(self, 'print_error') else print_error
|
print_ = self.print_error if hasattr(self, 'print_error') else print_error
|
||||||
print_("Exception in", func.__name__, ":", e.__class__.__name__, repr(e))
|
print_("Exception in", func.__name__, ":", repr(e))
|
||||||
try:
|
try:
|
||||||
traceback.print_exc(file=sys.stderr)
|
traceback.print_exc(file=sys.stderr)
|
||||||
except BaseException as e2:
|
except BaseException as e2:
|
||||||
|
|||||||
Reference in New Issue
Block a user