Skip to content
Merged
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
7 changes: 4 additions & 3 deletions NormalTests/Shared/ShieldReconcilerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ struct ShieldReconcilerTests {
#expect(store.manualBlockState?.applications.blockedTokens == [TokenFixtures.app(4)])
}

@Test(.enabled(if: FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: SharedConstants.appGroupID) != nil))
func concurrentReconcilesNeverOverlap() async {
@Test func concurrentReconcilesNeverOverlap() async {
let lockURL = FileManager.default.temporaryDirectory.appending(path: "\(UUID().uuidString).lock")
defer { try? FileManager.default.removeItem(at: lockURL) }
let store = FakeSharedStore()
store.manualBlockState = ManualBlockState()
let shields = RecordingShields()
Expand All @@ -107,7 +108,7 @@ struct ShieldReconcilerTests {
await withTaskGroup(of: Void.self) { group in
for id in UInt8(1) ... 8 {
group.addTask {
ShieldReconciler.reconcile(sharedStore: store, shields: shields, monitoredActivities: []) {
ShieldReconciler.reconcile(sharedStore: store, shields: shields, monitoredActivities: [], lockURL: lockURL) {
$0.block(TokenFixtures.selection(apps: [id]), customDomains: [], at: .now)
}
}
Expand Down
9 changes: 5 additions & 4 deletions Shared/ShieldReconciler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ nonisolated protocol ShieldApplying: AnyObject {
}

nonisolated enum SharedFileLock {
private static var url: URL? {
private static var defaultURL: URL? {
FileManager.default
.containerURL(forSecurityApplicationGroupIdentifier: SharedConstants.appGroupID)?
.appending(path: "shields.lock")
}

static func withLock<T>(_ body: () throws -> T) rethrows -> T? {
guard let path = url?.path(percentEncoded: false) else { return try body() }
static func withLock<T>(at url: URL? = nil, _ body: () throws -> T) rethrows -> T? {
guard let path = (url ?? defaultURL)?.path(percentEncoded: false) else { return try body() }
let descriptor = open(path, O_CREAT | O_RDWR, 0o644)
guard descriptor >= 0 else {
return errno == EPERM || errno == EACCES ? nil : try body()
Expand All @@ -35,9 +35,10 @@ nonisolated enum ShieldReconciler {
shields: some ShieldApplying,
monitoredActivities: Set<String>,
at date: Date = .now,
lockURL: URL? = nil,
update: (inout ManualBlockState) -> Void = { _ in }
) -> ShieldPlan? {
SharedFileLock.withLock {
SharedFileLock.withLock(at: lockURL) {
let schedules = sharedStore.loadSchedules()
.filter { $0.isEffectivelyEnabled(monitoredActivities: monitoredActivities) }
let timedUnblocks = sharedStore.loadTimedUnblocks()
Expand Down
Loading