Skip to content

Commit

Permalink
Run all Crypto tasks with default priority
Browse files Browse the repository at this point in the history
  • Loading branch information
Anderas committed Nov 21, 2022
1 parent 756c355 commit 8825c38
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
28 changes: 18 additions & 10 deletions MatrixSDK/Crypto/MXCryptoV2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -618,7 +627,7 @@ private class MXCryptoV2: NSObject, MXCrypto {
return
}

Task(priority: .medium) {
Task {
do {
let result = try await engine.importRoomKeys(keyFile, passphrase: password)

Expand Down Expand Up @@ -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()
}
}
}
Expand Down

0 comments on commit 8825c38

Please sign in to comment.