Prevent the sync service from looping so tightly on failure (#2540)

Add a delay when restarting the sync service and prevent the service from restarting when stopped.
This commit is contained in:
Doug
2024-03-07 16:27:33 +00:00
committed by GitHub
parent ad2de36f5a
commit 1a605807ec

View File

@@ -211,6 +211,27 @@ class ClientProxy: ClientProxyProtocol {
}
}
/// A stored task for restarting the sync after a failure. This is stored so that we can cancel
/// it when `stopSync` is called (e.g. when signing out) to prevent an otherwise infinite
/// loop that was triggered by trying to sync a signed out session.
@CancellableTask private var restartTask: Task<Void, Never>?
func restartSync() {
guard restartTask == nil else { return }
restartTask = Task { [weak self] in
do {
// Until the SDK can tell us the failure, we add a small
// delay to avoid generating multi-gigabyte log files.
try await Task.sleep(for: .milliseconds(250))
self?.startSync()
} catch {
MXLog.error("Restart cancelled.")
}
self?.restartTask = nil
}
}
func stopSync() {
stopSync(completion: nil)
}
@@ -218,6 +239,11 @@ class ClientProxy: ClientProxyProtocol {
private func stopSync(completion: (() -> Void)?) {
MXLog.info("Stopping sync")
if restartTask != nil {
MXLog.warning("Removing the sync service restart task.")
restartTask = nil
}
Task {
do {
defer {
@@ -592,7 +618,7 @@ class ClientProxy: ClientProxyProtocol {
case .running, .terminated, .idle:
break
case .error:
startSync()
restartSync()
}
})
}