Skip to content

Commit 1ac950c

Browse files
fix: deadlock on incrementalSync - WPB-21010 🍒 (#3747)
Co-authored-by: François Benaiteau <[email protected]> Co-authored-by: François Benaiteau <[email protected]>
1 parent e008873 commit 1ac950c

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

WireDomain/Sources/WireDomain/Synchronization/IncrementalSync.swift

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import Combine
2020
import Foundation
2121
import WireLogging
2222
import WireNetwork
23+
import WireSystem
2324

2425
public struct IncrementalSync: IncrementalSyncProtocol {
2526

@@ -123,10 +124,23 @@ public struct IncrementalSync: IncrementalSyncProtocol {
123124
logger.debug("handling live event stream", attributes: .incrementalSyncV2)
124125
syncStateSubject.send(.liveSyncing(.ongoing))
125126

126-
await processLiveEvents(
127-
liveEventStream: liveEventStream,
128-
processedEnvelopeIDs: processedEnvelopeIDs
129-
)
127+
do {
128+
// because we might be interrupted when in background, we wrap the sync in an expiringActivity that
129+
// will cancel the task - not keeping any db operation (sqlite file opened) in suspend mode
130+
try await withExpiringActivity(reason: "processLiveStream IncrementalSync") {
131+
await processLiveEvents(
132+
liveEventStream: liveEventStream,
133+
processedEnvelopeIDs: processedEnvelopeIDs
134+
)
135+
}
136+
} catch {
137+
// if we expire, close everything
138+
WireLogger.sync.debug(
139+
"Error while processing live stream, close push channel",
140+
attributes: .incrementalSyncV2
141+
)
142+
await pushChannel.close()
143+
}
130144

131145
logger.debug("live event stream did finish", attributes: .incrementalSyncV2)
132146
syncStateSubject.send(.liveSyncing(.finished))

WireDomain/Sources/WireDomain/Synchronization/IncrementalSyncV2.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ public struct IncrementalSyncV2: LiveSyncProtocol {
146146
await mlsGroupRepairAgent.repairConversations()
147147

148148
let task = Task { @Sendable [self] in
149+
logger.debug("handling live event stream", attributes: logAttributes)
150+
syncStateSubject.send(.liveSyncing(.ongoing))
151+
149152
do {
150153
// because we might be interrupted when in background, we wrap the sync in an expiringActivity that will
151154
// cancel the task (not keeping any file lock in suspend mode)
@@ -156,13 +159,16 @@ public struct IncrementalSyncV2: LiveSyncProtocol {
156159
syncMarker: syncMarker
157160
)
158161

159-
WireLogger.sync.debug("Live stream ended, close push channel")
162+
WireLogger.sync.debug("Live stream ended, close push channel", attributes: logAttributes)
160163
await pushChannel.close()
161164
await pushChannelState.markAsClosed()
162165
}
163166
} catch {
164167
// if we expire, close everything
165-
WireLogger.sync.debug("Error while processing live stream, close push channel")
168+
WireLogger.sync.debug(
169+
"Error while processing live stream, close push channel",
170+
attributes: logAttributes
171+
)
166172
await pushChannel.close()
167173
await pushChannelState.markAsClosed()
168174
}

0 commit comments

Comments
 (0)