From 616959486433fb0ce89ea0d78cb0b4ceffb30d00 Mon Sep 17 00:00:00 2001 From: Claudio Cambra Date: Mon, 20 Mar 2023 13:16:24 +0100 Subject: [PATCH] Prevent multi-thread modification of the same array with serial array modification dispatch queue when enumerating items in FileProviderEnumerator Signed-off-by: Claudio Cambra --- .../FileProviderExt/FileProviderEnumerator.swift | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/FileProviderEnumerator.swift b/shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/FileProviderEnumerator.swift index e7bc514ed..56e2b6b73 100644 --- a/shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/FileProviderEnumerator.swift +++ b/shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/FileProviderEnumerator.swift @@ -259,27 +259,27 @@ class FileProviderEnumerator: NSObject, NSFileProviderEnumerator { private static func metadatasToFileProviderItems(_ itemMetadatas: [NextcloudItemMetadataTable], ncKit: NextcloudKit, completionHandler: @escaping(_ items: [NSFileProviderItem]) -> Void) { var items: [NSFileProviderItem] = [] + let conversionQueue = DispatchQueue(label: "metadataToItemConversionQueue", qos: .userInitiated, attributes: .concurrent) + let appendQueue = DispatchQueue(label: "enumeratorItemAppendQueue", qos: .userInitiated) // Serial queue let dispatchGroup = DispatchGroup() for itemMetadata in itemMetadatas { - dispatchGroup.enter() - - DispatchQueue.global(qos: .userInitiated).async { + conversionQueue.async(group: dispatchGroup) { if itemMetadata.e2eEncrypted { Logger.enumeration.info("Skipping encrypted metadata in enumeration: \(itemMetadata.ocId, privacy: .public) \(itemMetadata.fileName, privacy: OSLogPrivacy.auto(mask: .hash))") - dispatchGroup.leave() return } if let parentItemIdentifier = NextcloudFilesDatabaseManager.shared.parentItemIdentifierFromMetadata(itemMetadata) { let item = FileProviderItem(metadata: itemMetadata, parentItemIdentifier: parentItemIdentifier, ncKit: ncKit) Logger.enumeration.debug("Will enumerate item with ocId: \(itemMetadata.ocId, privacy: .public) and name: \(itemMetadata.fileName, privacy: OSLogPrivacy.auto(mask: .hash))") - items.append(item) + + appendQueue.async(group: dispatchGroup) { + items.append(item) + } } else { Logger.enumeration.error("Could not get valid parentItemIdentifier for item with ocId: \(itemMetadata.ocId, privacy: .public) and name: \(itemMetadata.fileName, privacy: OSLogPrivacy.auto(mask: .hash)), skipping enumeration") } - - dispatchGroup.leave() } }