-
Notifications
You must be signed in to change notification settings - Fork 2
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
base: main
Are you sure you want to change the base?
Conversation
Sources/CacheContainer.swift
Outdated
@@ -46,3 +46,68 @@ public final class CacheContainer { | |||
} | |||
} | |||
} | |||
|
|||
final class ThreadSafeDictionary<V: Hashable,T>: Collection { |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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
}
}
}
There was a problem hiding this comment.
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] |
There was a problem hiding this comment.
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.
private var dictionary: [V: T] | |
private var dictionary: [V : T] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed by 7c1ad17.
There was a problem hiding this comment.
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.
CI tests have passed. |
Some of your code does not seem to be concurrency-safe. .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. |
No description provided.