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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 56 additions & 6 deletions Sources/CacheContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ public final class CacheContainer {
static let shared = CacheContainer()

public static func clearAll() {
shared.storage.forEach { (key, _) in
shared.storage.removeValue(forKey: key)
shared.storage.keys.forEach {
shared.storage.removeValue(forKey: $0)
}
}

public static func clearAll(where shouldBeCleared: (CacheKey) -> Bool) {
shared.storage.forEach { (key, _) in
if shouldBeCleared(key) {
shared.storage.removeValue(forKey: key)
shared.storage.keys.forEach {
if shouldBeCleared($0) {
shared.storage.removeValue(forKey: $0)
}
}
}

var storage: [CacheKey : Data] = [:]
var storage: ThreadSafeDictionary<CacheKey, Data> = .init()

private let decoder = JSONDecoder()
private let encoder = JSONEncoder()
Expand Down Expand Up @@ -46,3 +46,53 @@ 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.

private let concurrentQueue = DispatchQueue(label: "Dictionary Barrier Queue", attributes: .concurrent)

var keys: Dictionary<V, T>.Keys {
concurrentQueue.sync { dictionary.keys }
}

var values: Dictionary<V, T>.Values {
concurrentQueue.sync { dictionary.values }
}

var startIndex: Dictionary<V, T>.Index {
concurrentQueue.sync { dictionary.startIndex }
}

var endIndex: Dictionary<V, T>.Index {
concurrentQueue.sync { dictionary.endIndex }
}

init(dictionary: [V : T] = [:]) {
self.dictionary = dictionary
}

func index(after i: Dictionary<V, T>.Index) -> Dictionary<V, T>.Index {
concurrentQueue.sync { dictionary.index(after: i) }
}

subscript(key: V) -> T? {
set(newValue) {
concurrentQueue.async(flags: .barrier) { [weak self] in
self?.dictionary[key] = newValue
}
}
get {
concurrentQueue.sync { dictionary[key] }
}
}

subscript(index: Dictionary<V, T>.Index) -> Dictionary<V, T>.Element {
concurrentQueue.sync { dictionary[index] }
}

func removeValue(forKey key: V) {
concurrentQueue.async(flags: .barrier) { [weak self] in
self?.dictionary.removeValue(forKey: key)
}
}
}
18 changes: 18 additions & 0 deletions Tests/CacheContainerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ final class CacheContainerTests: XCTestCase {
let data: CacheContainer.CacheData<String>? = sut.value(forKey: "key.value")
XCTAssertEqual(data, .init(value: "value", cacheDate: cacheDate))
}

func test_setValue_concurrently() async throws {
let cacheDate = try Date("2023-01-01T00:00:00Z", strategy: .iso8601)
(0..<100).forEach { i in
Task.detached { [sut] in
sut.setValue("value\(i)", forKey: "key.value\(i)", cacheDate: cacheDate)
}
}
await Task.megaYield()
XCTAssertEqual(sut.storage.count, 100)
}
}

extension CacheContainer.CacheData<String>: Equatable {
Expand All @@ -46,3 +57,10 @@ extension CacheContainer.CacheData<String>: Equatable {
}
}

private extension Task where Success == Never, Failure == Never {
static func megaYield(count: Int = 10) async {
for _ in 0..<count {
await Task<Void, Never>.detached(priority: .background) { await Task.yield() }.value
}
}
}
Loading