Lock and unlock when accessing sync actions in FileProviderExt to protect against...
authorClaudio Cambra <claudio.cambra@nextcloud.com>
Tue, 16 Jul 2024 11:35:09 +0000 (19:35 +0800)
committerClaudio Cambra <claudio.cambra@nextcloud.com>
Tue, 23 Jul 2024 06:06:29 +0000 (14:06 +0800)
Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>
shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/FileProviderExtension+ClientInterface.swift
shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/FileProviderExtension.swift

index a4b60a407ee0a9de2817192db9a7e6820c465181..61f0a06052fb5bdee0ea92d01bbaff0cceaa8a5e 100644 (file)
@@ -134,7 +134,12 @@ extension FileProviderExtension: NSFileProviderServicing, ChangeNotificationInte
     }
 
     func updatedSyncStateReporting(oldActions: Set<UUID>) {
-        guard oldActions.isEmpty != syncActions.isEmpty else { return }
+        actionsLock.lock()
+
+        guard oldActions.isEmpty != syncActions.isEmpty else {
+            actionsLock.unlock()
+            return
+        }
 
         let command = "FILE_PROVIDER_DOMAIN_SYNC_STATE_CHANGE"
         var argument: String?
@@ -144,6 +149,8 @@ extension FileProviderExtension: NSFileProviderServicing, ChangeNotificationInte
             argument = errorActions.isEmpty ? "SYNC_FINISHED" : "SYNC_FAILED"
             errorActions = []
         }
+        
+        actionsLock.unlock()
 
         guard let argument else { return }
         Logger.fileProviderExtension.debug("Reporting sync \(argument)")
index 1e648a244f1d437f51aa4692a1d1657b8246799f..533d20d569e47837a977b52ca6268ecacb078760 100644 (file)
@@ -39,6 +39,7 @@ import OSLog
 
     var syncActions = Set<UUID>()
     var errorActions = Set<UUID>()
+    var actionsLock = NSLock()
 
     // Whether or not we are going to recursively scan new folders when they are discovered.
     // Apple's recommendation is that we should always scan the file hierarchy fully.
@@ -71,22 +72,28 @@ import OSLog
     }
 
     func insertSyncAction(_ actionId: UUID) {
+        actionsLock.lock()
         let oldActions = syncActions
         syncActions.insert(actionId)
+        actionsLock.unlock()
         updatedSyncStateReporting(oldActions: oldActions)
     }
 
     func insertErrorAction(_ actionId: UUID) {
+        actionsLock.lock()
         let oldActions = syncActions
         syncActions.remove(actionId)
         errorActions.insert(actionId)
+        actionsLock.unlock()
         updatedSyncStateReporting(oldActions: oldActions)
     }
 
     func removeSyncAction(_ actionId: UUID) {
+        actionsLock.lock()
         let oldActions = syncActions
         syncActions.remove(actionId)
         errorActions.remove(actionId)
+        actionsLock.unlock()
         updatedSyncStateReporting(oldActions: oldActions)
     }