Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash when accessing setValue concurrently #2

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

shota-nishizawa
Copy link

No description provided.

@@ -46,3 +46,68 @@ public final class CacheContainer {
}
}
}

final class ThreadSafeDictionary<V: Hashable,T>: Collection {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this type a class and not a struct?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I use ThreadSafeDictionary with struct, I get the error Escaping closure captures mutating 'self' parameter in subscript and removeValue.
It seems difficult to update self in a struct closure, so I use class.

struct ThreadSafeDictionary<V: Hashable,T>: Collection {
    private var dictionary: [V: T]
...
    subscript(key: V) -> T? {
        set(newValue) {
            self.concurrentQueue.async(flags: .barrier) {
                dictionary[key] = newValue   //   Error occurred: Escaping closure captures mutating 'self' parameter
            }
        }
        get {
            self.concurrentQueue.sync {
                return self.dictionary[key]
            }
        }
    }

...
    mutating func removeValue(forKey key: V) {
        self.concurrentQueue.async(flags: .barrier) {
            dictionary.removeValue(forKey: key)   //   Error occurred: Escaping closure captures mutating 'self' parameter
        }
    }
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your explanation. Understood.

@@ -46,3 +46,68 @@ public final class CacheContainer {
}
}
}

final class ThreadSafeDictionary<V: Hashable,T>: Collection {
private var dictionary: [V: T]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please unify the format of dictionary type declaration.

Suggested change
private var dictionary: [V: T]
private var dictionary: [V : T]

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed by 7c1ad17.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like it hasn't been fixed.

@shota-nishizawa shota-nishizawa changed the base branch from main to update-ci-macos-version April 1, 2024 09:11
@shota-nishizawa shota-nishizawa changed the base branch from update-ci-macos-version to main April 1, 2024 09:19
@shota-nishizawa
Copy link
Author

CI tests have passed.
https://github.com/cybozu/CachePropertyKit/actions/runs/8506365229

@ichiho-ojima
Copy link
Contributor

Some of your code does not seem to be concurrency-safe.
To check, make the following changes to Package.swift, and then build it using Xcode 15.3 and Swift 5.10.

.target(
    name: "CachePropertyKit",
-    path: "Sources"
+    path: "Sources",
+    swiftSettings: [
+        .unsafeFlags([
+            "-Xfrontend", "-warn-concurrency",
+            "-Xfrontend", "-enable-actor-data-race-checks"
+        ])
+    ]
),

Note

The unsafe flags does not exist in upstream, because the use of unsafe flags makes the products containing this target ineligible for use by other packages.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants