Skip to content

Commit 4115382

Browse files
authored
Remove memory leak in BackupAttachmentDownloadProgress that was breaking tests
1 parent e006f4e commit 4115382

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

SignalServiceKit/Backups/Attachments/BackupAttachmentDownloadProgress.swift

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ public actor BackupAttachmentDownloadProgress {
3434

3535
/// Begin observing progress of all backup attachment downloads.
3636
/// The observer will immediately be provided the current progress if any, and then updated with future progress state.
37-
public func addObserver(_ block: @escaping (OWSProgress) -> Void) -> Observer {
37+
public func addObserver(_ block: @escaping (OWSProgress) -> Void) async -> Observer {
3838
let observer = Observer(block: block)
3939
if let latestProgress {
4040
block(latestProgress)
4141
} else {
42-
initializeProgress()
42+
await initializeProgress()
4343
latestProgress.map(block)
4444
}
4545
observers.append(observer)
@@ -54,7 +54,7 @@ public actor BackupAttachmentDownloadProgress {
5454

5555
/// Compute total pending bytes to download, and set up observation for attachments to be downloaded.
5656
internal func beginObserving() async throws {
57-
await initializationTask.value
57+
await initializeProgress()
5858

5959
let pendingByteCount: UInt64 = try computeRemainingUndownloadedByteCount()
6060

@@ -140,6 +140,7 @@ public actor BackupAttachmentDownloadProgress {
140140

141141
// MARK: - Private
142142

143+
private nonisolated let appContext: AppContext
143144
private nonisolated let backupAttachmentDownloadStore: BackupAttachmentDownloadStore
144145
private nonisolated let dateProvider: DateProvider
145146
private nonisolated let db: DB
@@ -153,33 +154,33 @@ public actor BackupAttachmentDownloadProgress {
153154
db: DB,
154155
remoteConfigProvider: RemoteConfigProvider
155156
) {
157+
self.appContext = appContext
156158
self.backupAttachmentDownloadStore = backupAttachmentDownloadStore
157159
self.dateProvider = dateProvider
158160
self.db = db
159161
self.remoteConfigProvider = remoteConfigProvider
160162

161-
var selfRef: BackupAttachmentDownloadProgress?
162-
if appContext.isMainApp {
163-
self.initializationTask = Task {
164-
await withCheckedContinuation { continuation in
165-
appReadiness.runNowOrWhenMainAppDidBecomeReadyAsync {
166-
Task {
167-
await selfRef?.initializeProgress()
168-
continuation.resume()
169-
}
170-
}
171-
}
163+
appReadiness.runNowOrWhenMainAppDidBecomeReadyAsync { [weak self] in
164+
Task {
165+
await self?.initializeProgress()
172166
}
173-
} else {
174-
// No need to do anything outside the main app.
175-
initializationTask = Task {}
176167
}
177-
selfRef = self
178168
}
179169

180-
private let initializationTask: Task<Void, Never>
170+
private var initializationTask: Task<Void, Never>?
171+
172+
private func initializeProgress() async {
173+
guard appContext.isMainApp else { return }
174+
if let initializationTask {
175+
await initializationTask.value
176+
return
177+
}
178+
initializationTask = Task { [weak self] in
179+
await self?._initializeProgress()
180+
}
181+
}
181182

182-
private func initializeProgress() {
183+
private func _initializeProgress() {
183184
if latestProgress != nil { return }
184185
// Initialize the `latestProgress` value using the on-disk cached values.
185186
// Later we will (expensively) recompute the remaining byte count.

0 commit comments

Comments
 (0)