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: infinite loop and code optimization - WPB-16115 #2563

Open
wants to merge 11 commits into
base: release/cycle-3.118
Choose a base branch
from
24 changes: 16 additions & 8 deletions wire-ios-data-model/Source/MLS/MLSService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1727,15 +1727,22 @@ public final class MLSService: MLSServiceInterface {
}

private var task: Task<Void, Never>?
private var lastExecutionTime = Date.distantPast
private let throttleInterval: TimeInterval = 2.0 // 2 seconds throttle

public func commitPendingProposalsIfNeeded() {
task?.cancel()
let now = Date()

guard now.timeIntervalSince(lastExecutionTime) > throttleInterval else {
return // Ignore call if within the throttle period
}

task = Task { [self] in
guard !Task.isCancelled else {
return
}
lastExecutionTime = now

task?.cancel()

task = Task { [self] in
guard !Task.isCancelled else { return }
await commitPendingProposals()
}
}
Expand All @@ -1753,6 +1760,7 @@ public final class MLSService: MLSServiceInterface {

// Committing proposals for each group is independent and should not wait for
// each other.
logger.info("TEST10: Entering task group")
await withTaskGroup(of: Void.self) { taskGroup in
for (groupID, timestamp) in groupsWithPendingCommits {
taskGroup.addTask { [self] in
Expand All @@ -1761,18 +1769,18 @@ public final class MLSService: MLSServiceInterface {
logger.info("commit scheduled in the past, committing...")
try await commitPendingProposals(in: groupID)
} else {
logger.info("commit scheduled in the future, waiting...")
logger.info("TEST10: commit scheduled in the future, waiting...")

let timeIntervalSinceNow = timestamp.timeIntervalSinceNow
if timeIntervalSinceNow > 0 {
try await Task.sleep(nanoseconds: timeIntervalSinceNow.nanoseconds)
}
logger.info("scheduled commit is ready, committing...")
logger.info("TEST10: scheduled commit is ready, committing...")
try await commitPendingProposals(in: groupID)
}

} catch {
logger.error("failed to commit pending proposals: \(String(describing: error))")
logger.error("TEST10: failed to commit pending proposals: \(String(describing: error))")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ extension EventDecoder {
StoredUpdateEvent.highestIndex(self.eventMOC)
}

guard proteusProvider.canPerform else {
guard proteusProvider.canPerform, !events.isEmpty else {
WireLogger.proteus.warn("ignore decrypting events because it is not safe")
return []
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ actor EventProcessor: UpdateEventProcessor {

guard !DeveloperFlag.ignoreIncomingEvents.isOn else { return }

let publicKeys = try? self.earService.fetchPublicKeys()
let publicKeys = try? await self.earService.fetchPublicKeys()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: something changed here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we were not using await: simple compilation warning but this will be an error in Swift 6

let decryptedEvents = try await self.eventDecoder.decryptAndStoreEvents(events, publicKeys: publicKeys)
await self.processBackgroundEvents(decryptedEvents)

Expand Down Expand Up @@ -178,7 +178,7 @@ actor EventProcessor: UpdateEventProcessor {
attributes: .safePublic
)

guard let self else { return }
guard let self, !decryptedUpdateEvents.isEmpty else { return }

let date = Date()
let fetchRequest = await prefetchRequest(updateEvents: decryptedUpdateEvents)
Expand Down
Loading