Free fork of NotProton: Windows Steam games in Mac Steam, on CodeWeavers' Wine and Apple's Game Porting Toolkit
Swift apple-silicond3dmetaldxmtdxvkgame-porting-toolkitgptkmac-gamingmacosmetalfxrosetta-2steamsteam-playwinewinecx
Fix unreliable file locks.
NotProtonNot1e267cea9798
diff --git a/app/Sources/NotProtonApp/Model/PrefixStore.swift b/app/Sources/NotProtonApp/Model/PrefixStore.swiftindex 911b04e..e93424f 100644--- a/app/Sources/NotProtonApp/Model/PrefixStore.swift+++ b/app/Sources/NotProtonApp/Model/PrefixStore.swift@@ -308,7 +308,7 @@ enum PrefixStore {) -> Bool {let lock = open(prefix.root.appending(path: ".notproton-prefix.lock").path(percentEncoded: false), O_RDWR | O_NOFOLLOW | O_CLOEXEC)if lock >= 0 {- defer { close(lock) }+ defer { FileLock.release(lock) }var info = stat()guard fstat(lock, &info) == 0, info.st_mode & S_IFMT == S_IFREG,flock(lock, LOCK_EX | LOCK_NB) == 0diff --git a/app/Sources/NotProtonApp/Model/RunnerInstaller.swift b/app/Sources/NotProtonApp/Model/RunnerInstaller.swiftindex 1064350..ca13e80 100644--- a/app/Sources/NotProtonApp/Model/RunnerInstaller.swift+++ b/app/Sources/NotProtonApp/Model/RunnerInstaller.swift@@ -131,7 +131,7 @@ enum RunnerInstaller {}let lock = openat(parent, ".notproton-template.lock", O_RDWR | O_CREAT | O_NOFOLLOW | O_CLOEXEC, 0o600)guard lock >= 0 else { throw POSIXError(POSIXErrorCode(rawValue: errno) ?? .EIO) }- defer { close(lock) }+ defer { FileLock.release(lock) }var lockInfo = stat()guard fstat(lock, &lockInfo) == 0 else { throw POSIXError(POSIXErrorCode(rawValue: errno) ?? .EIO) }guard lockInfo.st_mode & S_IFMT == S_IFREG else { throw POSIXError(.EINVAL) }diff --git a/app/Sources/NotProtonApp/Model/SteamInstaller.swift b/app/Sources/NotProtonApp/Model/SteamInstaller.swiftindex 88c2fc4..4034ab6 100644--- a/app/Sources/NotProtonApp/Model/SteamInstaller.swift+++ b/app/Sources/NotProtonApp/Model/SteamInstaller.swift@@ -87,7 +87,7 @@ enum SteamInstaller {try assertBundleIsPresent(app)try assertInsertIsDeployedOrAbsent(at: plist, dylib: dylib)let installationLock = holdingInstallationLock ? -1 : try DeploymentContent.acquireInstallationLock(for: app)- defer { if installationLock >= 0 { close(installationLock) } }+ defer { if installationLock >= 0 { FileLock.release(installationLock) } }guard let dylibHashes = try MachOBuild.hashesIgnoringSignature(of: payload.dylib) else {throw StepFailure(step: step, detail: "The bundled NotProton dylib is invalid.")}diff --git a/app/Sources/NotProtonApp/Support/FileLock.swift b/app/Sources/NotProtonApp/Support/FileLock.swiftnew file mode 100644index 0000000..fa767ff--- /dev/null+++ b/app/Sources/NotProtonApp/Support/FileLock.swift@@ -0,0 +1,10 @@+import Darwin++enum FileLock {+ // A child being spawned holds a copy of every descriptor, close-on-exec ones included, until it+ // execs. close() alone leaves the lock held by that copy, so a relock can find it busy.+ static func release(_ fd: Int32) {+ flock(fd, LOCK_UN)+ close(fd)+ }+}diff --git a/app/Sources/NotProtonApp/Support/RemoveEverythingCommand.swift b/app/Sources/NotProtonApp/Support/RemoveEverythingCommand.swiftindex cde9e16..3bb21cf 100644--- a/app/Sources/NotProtonApp/Support/RemoveEverythingCommand.swift+++ b/app/Sources/NotProtonApp/Support/RemoveEverythingCommand.swift@@ -11,7 +11,7 @@ enum RemoveEverythingCommand {private static func perform() async -> Int32 {do {let lock = try DeploymentContent.acquireInstallationLock(for: SupportPaths.Steam.app)- defer { close(lock) }+ defer { FileLock.release(lock) }let output = Output()let result = try await Uninstall.run { phase inif case .restoring(.finished) = phase { return }diff --git a/app/Sources/NotProtonApp/SystemStatus.swift b/app/Sources/NotProtonApp/SystemStatus.swiftindex d08c436..0a5d543 100644--- a/app/Sources/NotProtonApp/SystemStatus.swift+++ b/app/Sources/NotProtonApp/SystemStatus.swift@@ -242,7 +242,7 @@ final class SystemStatus {pendingRemoval = nilawait perform(from: RunnerInstaller.removeStep) { _ inlet lock = try DeploymentContent.acquireInstallationLock(for: SupportPaths.Steam.app)- defer { close(lock) }+ defer { FileLock.release(lock) }try await requireUnblockedContent()let changed = try await Task.detached(priority: .userInitiated) {try RunnerInstaller.removeClone(forBuild: build)@@ -349,7 +349,7 @@ final class SystemStatus {await perform(from: RunnerSetup.Phase.cloning.label) { progress inlet lock = try DeploymentContent.acquireInstallationLock(for: SupportPaths.Steam.app)- defer { close(lock) }+ defer { FileLock.release(lock) }try await requireInstallableContent()let result = try await Task.detached(priority: .userInitiated) {try RunnerSetup.run(from: install, replacingExisting: replacingExisting) {@@ -365,7 +365,7 @@ final class SystemStatus {guard canInstall else { return }await perform(from: ValveFetcher.Phase.verifying.label) { progress inlet lock = try DeploymentContent.acquireInstallationLock(for: SupportPaths.Steam.app)- defer { close(lock) }+ defer { FileLock.release(lock) }try await requireInstallableContent()let result = try await ValveFetcher.run { progress($0.label) }return result.wroteNothing ? nil : "Downloaded missing components."@@ -404,7 +404,7 @@ final class SystemStatus {func installIntoSteam() async {await perform(from: InstallPhase.checkingPayload.label) { progress inlet lock = try DeploymentContent.acquireInstallationLock(for: SupportPaths.Steam.app)- defer { close(lock) }+ defer { FileLock.release(lock) }let result = try await Task.detached(priority: .userInitiated) {try SteamInstaller.run(holdingInstallationLock: true, report: { progress($0.label) })}.value@@ -464,7 +464,7 @@ final class SystemStatus {func repairSteam() async {await perform(from: RepairPhase.checking.label) { progress inlet lock = try DeploymentContent.acquireInstallationLock(for: SupportPaths.Steam.app)- defer { close(lock) }+ defer { FileLock.release(lock) }let result = try await SteamRepair.run { progress($0.label) }var parts = ["Steam restored to its original state."]@@ -491,7 +491,7 @@ final class SystemStatus {func removeEverything() async {await perform(from: UninstallPhase.stoppingClient.label) { progress inlet lock = try DeploymentContent.acquireInstallationLock(for: SupportPaths.Steam.app)- defer { close(lock) }+ defer { FileLock.release(lock) }let result = try await Uninstall.run { progress($0.label) }return result.steamNeedsRedownload@@ -527,7 +527,7 @@ final class SystemStatus {let measured = await Task.detached(priority: .utility) {if cleanTemplates, let lock = try? DeploymentContent.acquireInstallationLock(for: SupportPaths.Steam.app) {RunnerInstaller.removeLeftoverRemovals(runners: runners)- close(lock)+ FileLock.release(lock)}let failures = cleanTemplates? RunnerInstaller.removeStalePrefixTemplates(runners: runners, libraries: libraries, reportBusy: false) : []diff --git a/app/Tests/NotProtonAppTests/DeploymentContentTests.swift b/app/Tests/NotProtonAppTests/DeploymentContentTests.swiftindex 427e9ea..eba51fc 100644--- a/app/Tests/NotProtonAppTests/DeploymentContentTests.swift+++ b/app/Tests/NotProtonAppTests/DeploymentContentTests.swift@@ -181,13 +181,13 @@ struct DeploymentContentTests {let app = root.appending(path: "Steam.app")do {let fd = try DeploymentContent.acquireInstallationLock(for: app)- defer { close(fd) }+ defer { FileLock.release(fd) }#expect(throws: StepFailure.self) { try DeploymentContent.acquireInstallationLock(for: app) }let status = try Shell.run("/bin/sh", ["-c", #"exec 9< "$1"; /usr/bin/lockf -s -t 0 9"#, "fixture", root.path]).status#expect(status == 75)}let next = try DeploymentContent.acquireInstallationLock(for: app)- close(next)+ FileLock.release(next)#expect(try FileManager.default.contentsOfDirectory(atPath: root.path).isEmpty)}diff --git a/app/Tests/NotProtonAppTests/FileLockTests.swift b/app/Tests/NotProtonAppTests/FileLockTests.swiftnew file mode 100644index 0000000..1cc720f--- /dev/null+++ b/app/Tests/NotProtonAppTests/FileLockTests.swift@@ -0,0 +1,39 @@+import Darwin+import Foundation+import Synchronization+import Testing++@testable import NotProtonApp++@Suite("File locks")+struct FileLockTests {+ @Test("A lock released while another thread spawns processes is free at once")+ func releasedDuringSpawn() throws {+ let dir = try scratchDirectory("file-lock")+ defer { try? FileManager.default.removeItem(at: dir) }+ let path = dir.appending(path: "lock").path+ let stop = Atomic(false)+ let spawned = Atomic(0)+ Thread.detachNewThread {+ while !stop.load(ordering: .relaxed) {+ let child = Process()+ child.executableURL = URL(filePath: "/usr/bin/true")+ guard (try? child.run()) != nil else { continue }+ child.waitUntilExit()+ spawned.add(1, ordering: .relaxed)+ }+ }+ defer { stop.store(true, ordering: .relaxed) }++ var busy = 0+ let deadline = ContinuousClock.now + .seconds(10)+ while spawned.load(ordering: .relaxed) < 200, ContinuousClock.now < deadline {+ let fd = open(path, O_RDWR | O_CREAT | O_CLOEXEC, 0o600)+ try #require(fd >= 0)+ if flock(fd, LOCK_EX | LOCK_NB) != 0 { busy += 1 }+ FileLock.release(fd)+ }+ #expect(spawned.load(ordering: .relaxed) >= 200)+ #expect(busy == 0)+ }+}diff --git a/app/Tests/NotProtonAppTests/PrefixStoreTests.swift b/app/Tests/NotProtonAppTests/PrefixStoreTests.swiftindex 8edbda4..1ff68d5 100644--- a/app/Tests/NotProtonAppTests/PrefixStoreTests.swift+++ b/app/Tests/NotProtonAppTests/PrefixStoreTests.swift@@ -217,7 +217,7 @@ struct PrefixStoreTests {let lock = prefix.root.appending(path: ".notproton-prefix.lock")#expect(!PrefixStore.isInUse(prefix, root: root))#expect(!FileManager.default.fileExists(atPath: lock.path))- let fd = open(lock.path, O_RDWR | O_CREAT | O_NOFOLLOW, 0o600)+ let fd = open(lock.path, O_RDWR | O_CREAT | O_NOFOLLOW | O_CLOEXEC, 0o600)try #require(fd >= 0)defer { close(fd) }try #require(flock(fd, LOCK_EX | LOCK_NB) == 0)diff --git a/app/Tests/NotProtonAppTests/TemplateCleanupTests.swift b/app/Tests/NotProtonAppTests/TemplateCleanupTests.swiftindex 2fb9111..932c17e 100644--- a/app/Tests/NotProtonAppTests/TemplateCleanupTests.swift+++ b/app/Tests/NotProtonAppTests/TemplateCleanupTests.swift@@ -89,7 +89,7 @@ struct TemplateCleanupTests {let save = layout.library.compatdata.appending(path: "700330/pfx/drive_c/save.dat")try FileManager.default.createDirectory(at: save.deletingLastPathComponent(), withIntermediateDirectories: true)try Data("save".utf8).write(to: save)- let opened = open(layout.lock.path, O_RDWR | O_CREAT, 0o600)+ let opened = open(layout.lock.path, O_RDWR | O_CREAT | O_CLOEXEC, 0o600)#expect(opened >= 0)guard opened >= 0 else { return }defer { close(opened) }@@ -100,7 +100,7 @@ struct TemplateCleanupTests {#expect(FileManager.default.fileExists(atPath: layout.lock.path))#expect(FileManager.default.fileExists(atPath: save.path))#expect(flock(opened, LOCK_EX | LOCK_NB) == 0)- let next = open(layout.lock.path, O_RDWR | O_CREAT, 0o600)+ let next = open(layout.lock.path, O_RDWR | O_CREAT | O_CLOEXEC, 0o600)#expect(next >= 0)guard next >= 0 else { return }defer { close(next) }@@ -163,7 +163,7 @@ struct TemplateCleanupTests {func reportsBusyLock() throws {let layout = try Layout()defer { try? FileManager.default.removeItem(at: layout.root) }- let fd = open(layout.lock.path, O_RDWR | O_CREAT | O_NOFOLLOW, 0o600)+ let fd = open(layout.lock.path, O_RDWR | O_CREAT | O_NOFOLLOW | O_CLOEXEC, 0o600)try #require(fd >= 0)defer { close(fd) }try #require(flock(fd, LOCK_EX | LOCK_NB) == 0)@@ -203,7 +203,7 @@ struct TemplateCleanupTests {func refreshRetriesWithoutRunners() async throws {let layout = try Layout()defer { try? FileManager.default.removeItem(at: layout.root) }- let fd = open(layout.lock.path, O_RDWR | O_CREAT | O_NOFOLLOW, 0o600)+ let fd = open(layout.lock.path, O_RDWR | O_CREAT | O_NOFOLLOW | O_CLOEXEC, 0o600)try #require(fd >= 0)defer { close(fd) }try #require(flock(fd, LOCK_EX | LOCK_NB) == 0)@@ -365,7 +365,7 @@ struct TemplateCleanupTests {func swiftLockBlocksShell() throws {let layout = try Layout()defer { try? FileManager.default.removeItem(at: layout.root) }- let fd = open(layout.lock.path, O_RDWR | O_CREAT | O_NOFOLLOW, 0o600)+ let fd = open(layout.lock.path, O_RDWR | O_CREAT | O_NOFOLLOW | O_CLOEXEC, 0o600)try #require(fd >= 0)defer { close(fd) }try #require(flock(fd, LOCK_EX | LOCK_NB) == 0)