Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run all Crypto tasks with default priority #1639

Merged
merged 1 commit into from
Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1 change: 1 addition & 0 deletions changelog.d/pr-1639.change
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CryptoV2: Run all tasks with default priority