Skip to content

Commit

Permalink
Fix race condition when setting up session verification controller su…
Browse files Browse the repository at this point in the history
…bscriptions.
  • Loading branch information
stefanceriu committed Nov 5, 2024
1 parent f3a15f7 commit c54e4bf
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions ElementX/Sources/Services/Client/ClientProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,19 +156,10 @@ class ClientProxy: ClientProxyProtocol {
self?.ignoredUsersSubject.send(ignoredUsers)
})

updateVerificationState(client.encryption().verificationState())
await updateVerificationState(client.encryption().verificationState())

verificationStateListenerTaskHandle = client.encryption().verificationStateListener(listener: VerificationStateListenerProxy { [weak self] verificationState in
guard let self else { return }

updateVerificationState(verificationState)

// The session verification controller requires the user's identity which
// isn't available before a keys query response. Use the verification
// state updates as an aproximation for when that happens.
Task {
await self.buildSessionVerificationControllerProxyIfPossible(verificationState: verificationState)
}
Task { await self?.updateVerificationState(verificationState) }
})

sendQueueListenerTaskHandle = client.subscribeToSendQueueStatus(listener: SendQueueRoomErrorListenerProxy { [weak self] roomID, error in
Expand Down Expand Up @@ -727,7 +718,7 @@ class ClientProxy: ClientProxyProtocol {

// MARK: - Private

private func updateVerificationState(_ verificationState: VerificationState) {
private func updateVerificationState(_ verificationState: VerificationState) async {
let verificationState: SessionVerificationState = switch verificationState {
case .unknown:
.unknown
Expand All @@ -737,10 +728,17 @@ class ClientProxy: ClientProxyProtocol {
.verified
}

// The session verification controller requires the user's identity which
// isn't available before a keys query response. Use the verification
// state updates as an aproximation for when that happens.
await buildSessionVerificationControllerProxyIfPossible(verificationState: verificationState)

// Only update the session verification state after creating a session
// verification proxy to avoid race conditions
verificationStateSubject.send(verificationState)
}

private func buildSessionVerificationControllerProxyIfPossible(verificationState: VerificationState) async {
private func buildSessionVerificationControllerProxyIfPossible(verificationState: SessionVerificationState) async {
guard sessionVerificationController == nil, verificationState != .unknown else {
return
}
Expand Down

0 comments on commit c54e4bf

Please sign in to comment.