Skip to content

Commit 8b49cf6

Browse files
committed
Attempt to prevent crashes after expiring background refreshes.
1 parent 98a9534 commit 8b49cf6

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

ElementX/Sources/Application/AppCoordinator.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -893,8 +893,11 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
893893
// Attempt to stop the background task sync loop cleanly, only if the app not already running
894894
return
895895
}
896-
userSession?.clientProxy.stopSync(completion: completion)
897-
clientProxyObserver = nil
896+
897+
MainActor.assumeIsolated {
898+
userSession?.clientProxy.stopSync(completion: completion)
899+
clientProxyObserver = nil
900+
}
898901
}
899902

900903
private func startSync() {
@@ -1036,7 +1039,13 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
10361039
// This is important for the app to keep refreshing in the background
10371040
scheduleBackgroundAppRefresh()
10381041

1039-
task.expirationHandler = { [weak self] in
1042+
/// We have a lot of crashes stemming here which we previously believed are caused by stopSync not being async
1043+
/// on the client proxy side (see the comment on that method). We have now realised that will likely not fix anything but
1044+
/// we also noticed this does not crash on the main thread, even though the whole AppCoordinator is on the Main actor.
1045+
/// As such, we introduced a MainActor conformance on the expirationHandler but we are also assuming main actor
1046+
/// isolated in the `stopSync` method above.
1047+
/// https://sentry.tools.element.io/organizations/element/issues/4477794/
1048+
task.expirationHandler = { @MainActor [weak self] in
10401049
MXLog.info("Background app refresh task is about to expire.")
10411050

10421051
self?.stopSync(isBackgroundTask: true) {

0 commit comments

Comments
 (0)