diff --git a/MatrixSDK/Crypto/KeyBackup/Engine/MXCryptoKeyBackupEngine.swift b/MatrixSDK/Crypto/KeyBackup/Engine/MXCryptoKeyBackupEngine.swift index 09441ed361..a82312204f 100644 --- a/MatrixSDK/Crypto/KeyBackup/Engine/MXCryptoKeyBackupEngine.swift +++ b/MatrixSDK/Crypto/KeyBackup/Engine/MXCryptoKeyBackupEngine.swift @@ -284,7 +284,7 @@ class MXCryptoKeyBackupEngine: NSObject, MXKeyBackupEngine { return } - Task(priority: .medium) { + Task { let encryptedSessions = keysBackupData.rooms.flatMap { roomId, room in room.sessions.map { sessionId, keyBackup in EncryptedSession(roomId: roomId, sessionId: sessionId, keyBackup: keyBackup) diff --git a/MatrixSDK/Crypto/MXCryptoV2.swift b/MatrixSDK/Crypto/MXCryptoV2.swift index 34ba84aa2c..c58d0cb9c6 100644 --- a/MatrixSDK/Crypto/MXCryptoV2.swift +++ b/MatrixSDK/Crypto/MXCryptoV2.swift @@ -74,6 +74,7 @@ private class MXCryptoV2: NSObject, MXCrypto { private let trustLevelSource: MXTrustLevelSource private let backupEngine: MXCryptoKeyBackupEngine? private let keyVerification: MXKeyVerificationManagerV2 + private var startTask: Task<(), Never>? private var roomEventObserver: Any? private let log = MXNamedLog(name: "MXCryptoV2") @@ -192,8 +193,14 @@ private class MXCryptoV2: NSObject, MXCrypto { _ onComplete: (() -> Void)?, failure: ((Swift.Error) -> Void)? ) { + guard startTask == nil else { + log.error("Crypto module has already been started") + onComplete?() + return + } + log.debug("->") - Task { + startTask = Task { do { try await machine.start() crossSigning.refreshState(success: nil) @@ -215,6 +222,10 @@ private class MXCryptoV2: NSObject, MXCrypto { public func close(_ deleteStore: Bool) { log.debug("->") + + startTask?.cancel() + startTask = nil + session?.removeListener(roomEventObserver) Task { await roomEventDecryptor.resetUndecryptedEvents() @@ -392,7 +403,7 @@ private class MXCryptoV2: NSObject, MXCrypto { MXLog.debug("[MXCryptoV2] --------------------------------") log.debug("Handling new sync response with \(toDeviceCount) to-device event(s), \(devicesChanged) device(s) changed, \(devicesLeft) device(s) left") - Task(priority: .medium) { + Task { do { let toDevice = try machine.handleSyncResponse( toDevice: syncResponse.toDevice, @@ -586,18 +597,16 @@ private class MXCryptoV2: NSObject, MXCrypto { return } - Task(priority: .medium) { [weak self] in - guard let self = self else { return } - + Task { do { let data = try engine.exportRoomKeys(passphrase: password) await MainActor.run { - self.log.debug("Exported room keys") + log.debug("Exported room keys") success?(data) } } catch { await MainActor.run { - self.log.error("Failed exporting room keys", context: error) + log.error("Failed exporting room keys", context: error) failure?(error) } } @@ -618,7 +627,7 @@ private class MXCryptoV2: NSObject, MXCrypto { return } - Task(priority: .medium) { + Task { do { let result = try await engine.importRoomKeys(keyFile, passphrase: password) @@ -680,13 +689,12 @@ private class MXCryptoV2: NSObject, MXCrypto { guard let self = self else { return } if direction == .forwards && event.sender != session.myUserId { - Task(priority: .medium) { + Task { if let userId = await self.keyVerification.handleRoomEvent(event), !self.machine.isUserTracked(userId: userId) { // If we recieved a verification event from a new user we do not yet track // we need to download their keys to be able to proceed with the verification flow try await self.machine.downloadKeys(users: [userId]) } - try await self.machine.processOutgoingRequests() } } }