Improve cleanup lifecycle readability

Signed-off-by: Loren Eteval <loren.eteval@proton.me>
This commit is contained in:
Loren Eteval
2026-10-10 21:09:04 +08:00
parent bd02e4b4c7
commit 1e4c8b6683
9 changed files with 121 additions and 0 deletions
@@ -224,9 +224,12 @@ class ConnectionController(QtCore.QObject):
self._pendingHttpProxy = ''
self.progressFinished.emit(True)
if not isValid(self):
return
self._setActiveProfile(None)
if not isValid(self):
return
@@ -589,6 +592,7 @@ class ConnectionController(QtCore.QObject):
if self.isDisconnecting():
self._reconnectAfterStop = False
return False
if (
self.state is ConnectionState.Disconnected
and not self._startAdmissionPending
@@ -610,8 +614,10 @@ class ConnectionController(QtCore.QObject):
self._startOperation = None
self._setState(ConnectionState.Disconnecting)
if not isValid(self) or generation != self._connectionGeneration:
return True
self._actionTimer.stop()
if operation is not None and isValid(operation):
@@ -637,9 +643,11 @@ class ConnectionController(QtCore.QObject):
self._disconnectionNotification = notification
asyncStop = getattr(self._coreManager, 'stopAsync', None)
if callable(asyncStop):
try:
self._stopOperation = stopOperation = asyncStop()
connectWeakly(
stopOperation.finished,
self,
@@ -652,13 +660,19 @@ class ConnectionController(QtCore.QObject):
'_connectionStopDestroyed',
sender=stopOperation,
)
stopOperation.start()
return True
except Exception as ex:
# Any non-exit exceptions
self._runtimeCleanupFailed = True
logger.error(f'failed to stop connection runtime: {ex}')
self._finishDisconnection(generation, notification)
return True
try:
@@ -678,10 +692,14 @@ class ConnectionController(QtCore.QObject):
def _connectionStopFinished(self, operation, error):
if operation is not self._stopOperation:
return
self._stopOperation = None
if error:
self._runtimeCleanupFailed = True
logger.error(f'failed to stop connection runtime: {error}')
self._finishDisconnection(
self._disconnectionGeneration, self._disconnectionNotification
)
@@ -690,15 +708,19 @@ class ConnectionController(QtCore.QObject):
if self._stopOperation is not None and not isValid(self._stopOperation):
self._stopOperation = None
self._runtimeCleanupFailed = True
self._finishDisconnection(
self._disconnectionGeneration, self._disconnectionNotification
)
def _finishDisconnection(self, generation, notification):
reconnect, self._reconnectAfterStop = self._reconnectAfterStop, False
self._emitRuntimesChanged()
if not isValid(self):
return
self._reset()
if not isValid(self) or generation != self._connectionGeneration:
@@ -713,22 +735,28 @@ class ConnectionController(QtCore.QObject):
pass
Mixins.ConnectionAware.callDisconnectedCallback()
if not isValid(self) or generation != self._connectionGeneration:
return
if notification:
self.notificationRequested.emit(notification)
if not isValid(self):
return
if self._shuttingDown:
if self._shutdownConnectPreference:
AppSettings.turnON_('Connect')
success = not self._runtimeCleanupFailed
self._shutdownComplete = success
if not success:
self._shuttingDown = False
self._shutdownConnectPreference = None
self.shutdownFinished.emit(success)
elif (
reconnect
@@ -761,10 +789,14 @@ class ConnectionController(QtCore.QObject):
"""Stop runtime resources without changing the next-start preference."""
if self._shutdownComplete:
self.shutdownFinished.emit(True)
return True
reconnectOnStartup = AppSettings.isStateON_('Connect')
if self._shutdownConnectPreference is None:
self._shutdownConnectPreference = reconnectOnStartup
reconnectOnStartup = self._shutdownConnectPreference
self._shuttingDown = True
@@ -789,12 +821,15 @@ class ConnectionController(QtCore.QObject):
self._runtimeCleanupFailed = False
self._shutdownComplete = True
self.shutdownFinished.emit(True)
return True
except Exception:
# Any non-exit exceptions
self._shuttingDown = False
self._shutdownConnectPreference = None
self.shutdownFinished.emit(False)
raise
finally:
if reconnectOnStartup: