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

Fix race condition when setting up session verification controller subscriptions #3486

Merged
merged 1 commit into from
Nov 5, 2024
Merged
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
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
Loading