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 046f1dd533
commit da0fcfe052
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

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() { func stopSync() {
stopSync(completion: nil) stopSync(completion: nil)
} }
@ -218,6 +239,11 @@ class ClientProxy: ClientProxyProtocol {
private func stopSync(completion: (() -> Void)?) { private func stopSync(completion: (() -> Void)?) {
MXLog.info("Stopping sync") MXLog.info("Stopping sync")
if restartTask != nil {
MXLog.warning("Removing the sync service restart task.")
restartTask = nil
}
Task { Task {
do { do {
defer { defer {
@ -592,7 +618,7 @@ class ClientProxy: ClientProxyProtocol {
case .running, .terminated, .idle: case .running, .terminated, .idle:
break break
case .error: case .error:
startSync() restartSync()
} }
}) })
} }