Skip to content

Free fork of NotProton: Windows Steam games in Mac Steam, on CodeWeavers' Wine and Apple's Game Porting Toolkit

Hold the prefix lock when the NotProton app is performing an operation on a prefix

NotProtonNotb5bf2a91bf54
diff --git a/app/Sources/NotProtonApp/Model/PrefixRebuild.swift b/app/Sources/NotProtonApp/Model/PrefixRebuild.swift
index 4c50ba3..36cb57f 100644
--- a/app/Sources/NotProtonApp/Model/PrefixRebuild.swift
+++ b/app/Sources/NotProtonApp/Model/PrefixRebuild.swift
@@ -27,12 +27,13 @@ extension PrefixTools {
now: Date = .now,
keepBackup: Bool = true
) throws -> URL? {
- guard !PrefixStore.isInUse(prefix) else {
+ guard let claim = PrefixStore.claim(prefix) else {
throw StepFailure(
step: String(localized: "Recreate prefix"),
detail: String(localized: "\(prefix.title) is running. Quit the game first.")
)
}
+ defer { claim.release() }
if let held = PrefixStore.backups(of: prefix).last,
!FileManager.default.fileExists(
@@ -98,12 +99,13 @@ extension PrefixTools {
@discardableResult
static func backUp(_ prefix: WinePrefix, now: Date = .now) throws -> URL {
- guard !PrefixStore.isInUse(prefix) else {
+ guard let claim = PrefixStore.claim(prefix) else {
throw StepFailure(
step: String(localized: "Back up prefix"),
detail: String(localized: "\(prefix.title) is running. Quit the game first.")
)
}
+ defer { claim.release() }
let fm = FileManager.default
guard fm.fileExists(atPath: prefix.pfx.path(percentEncoded: false)) else {
diff --git a/app/Sources/NotProtonApp/Model/PrefixStore.swift b/app/Sources/NotProtonApp/Model/PrefixStore.swift
index 5a70148..2c8f4c5 100644
--- a/app/Sources/NotProtonApp/Model/PrefixStore.swift
+++ b/app/Sources/NotProtonApp/Model/PrefixStore.swift
@@ -384,17 +384,50 @@ enum PrefixStore {
_ prefix: WinePrefix, root: URL = serverRoot, lsof: String = "/usr/sbin/lsof",
drainTimeout: DispatchTimeInterval = .seconds(2)
) -> Bool {
- let lock = open(prefix.root.appending(path: ".notproton-prefix.lock").path(percentEncoded: false), O_RDWR | O_NOFOLLOW | O_CLOEXEC)
+ let lock = open(lockPath(of: prefix), O_RDWR | O_NOFOLLOW | O_CLOEXEC)
if lock >= 0 {
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) == 0
- else { return true }
+ guard locked(lock) else { return true }
} else if errno != ENOENT {
return true
}
+ return serverIsBusy(prefix, root: root, lsof: lsof, drainTimeout: drainTimeout)
+ }
+
+ // A launch takes this lock while it sets the prefix up. Holding it stops a launch from
+ // starting partway through a rebuild/delete.
+ static func claim(
+ _ prefix: WinePrefix, root: URL = serverRoot, lsof: String = "/usr/sbin/lsof",
+ drainTimeout: DispatchTimeInterval = .seconds(2)
+ ) -> PrefixClaim? {
+ let lock = open(lockPath(of: prefix), O_RDWR | O_CREAT | O_NOFOLLOW | O_CLOEXEC, 0o600)
+ guard lock >= 0 else {
+ guard errno == ENOENT,
+ !serverIsBusy(prefix, root: root, lsof: lsof, drainTimeout: drainTimeout)
+ else { return nil }
+ return PrefixClaim(fd: -1)
+ }
+ guard locked(lock), !serverIsBusy(prefix, root: root, lsof: lsof, drainTimeout: drainTimeout)
+ else {
+ FileLock.release(lock)
+ return nil
+ }
+ return PrefixClaim(fd: lock)
+ }
+
+ private static func lockPath(of prefix: WinePrefix) -> String {
+ prefix.root.appending(path: ".notproton-prefix.lock").path(percentEncoded: false)
+ }
+ private static func locked(_ fd: Int32) -> Bool {
+ var info = stat()
+ return fstat(fd, &info) == 0 && info.st_mode & S_IFMT == S_IFREG
+ && flock(fd, LOCK_EX | LOCK_NB) == 0
+ }
+
+ private static func serverIsBusy(
+ _ prefix: WinePrefix, root: URL, lsof: String, drainTimeout: DispatchTimeInterval
+ ) -> Bool {
guard let dir = serverDirectory(of: prefix, root: root),
FileManager.default.fileExists(atPath: dir.path(percentEncoded: false))
else { return false }
@@ -408,3 +441,11 @@ enum PrefixStore {
return result.outputLost || !result.stderr.isEmpty
}
}
+
+struct PrefixClaim {
+ fileprivate let fd: Int32
+
+ func release() {
+ if fd >= 0 { FileLock.release(fd) }
+ }
+}
diff --git a/app/Sources/NotProtonApp/Model/PrefixTools.swift b/app/Sources/NotProtonApp/Model/PrefixTools.swift
index 9570410..ed845d5 100644
--- a/app/Sources/NotProtonApp/Model/PrefixTools.swift
+++ b/app/Sources/NotProtonApp/Model/PrefixTools.swift
@@ -297,9 +297,10 @@ enum PrefixTools {
if let refused = verbs.first(where: { $0 == "annihilate" || $0.hasPrefix("prefix=") || $0.hasPrefix("-") }) {
throw StepFailure(step: step, detail: String(localized: "NotProton does not run \(refused) with Winetricks."))
}
- guard !PrefixStore.isInUse(prefix) else {
+ guard let claim = PrefixStore.claim(prefix) else {
throw StepFailure(step: step, detail: String(localized: "\(prefix.title) is running. Quit the game first."))
}
+ defer { claim.release() }
let loader = try readyLoader(step: step, prefix: prefix, runner: runner, flavor: flavor)
let wrapper = script.deletingLastPathComponent().appending(path: "notproton-wine")
do {
@@ -374,12 +375,13 @@ enum PrefixTools {
}
static func delete(_ prefix: WinePrefix) throws {
- guard !PrefixStore.isInUse(prefix) else {
+ guard let claim = PrefixStore.claim(prefix) else {
throw StepFailure(
step: String(localized: "Delete prefix"),
detail: String(localized: "\(prefix.title) is running. Quit the game first.")
)
}
+ defer { claim.release() }
try WriteRefused.catching(prefix.root) {
try FileManager.default.removeItem(at: prefix.root)
}
diff --git a/app/Tests/NotProtonAppTests/PrefixStoreTests.swift b/app/Tests/NotProtonAppTests/PrefixStoreTests.swift
index d7c6d9b..895bb66 100644
--- a/app/Tests/NotProtonAppTests/PrefixStoreTests.swift
+++ b/app/Tests/NotProtonAppTests/PrefixStoreTests.swift
@@ -273,6 +273,20 @@ struct PrefixStoreTests {
#expect(!PrefixStore.isInUse(prefix, root: root))
}
+ @Test("A claim keeps the preparation lock until it is released")
+ func claimHoldsThePreparationLock() throws {
+ let dir = try scratchDirectory("pfx-claim")
+ defer { try? FileManager.default.removeItem(at: dir) }
+ let prefix = try prefix(in: dir)
+ let root = dir.appending(path: "sockets")
+
+ let claim = try #require(PrefixStore.claim(prefix, root: root))
+ #expect(PrefixStore.isInUse(prefix, root: root))
+ #expect(PrefixStore.claim(prefix, root: root) == nil)
+ claim.release()
+ #expect(!PrefixStore.isInUse(prefix, root: root))
+ }
+
@Test("A linked preparation lock is not mistaken for an idle prefix")
func linkedPreparationLockIsInUse() throws {
let dir = try scratchDirectory("pfx-preparation-link")
diff --git a/app/Tests/NotProtonAppTests/PrefixToolsTests.swift b/app/Tests/NotProtonAppTests/PrefixToolsTests.swift
index 11d085f..5d3edca 100644
--- a/app/Tests/NotProtonAppTests/PrefixToolsTests.swift
+++ b/app/Tests/NotProtonAppTests/PrefixToolsTests.swift
@@ -448,6 +448,26 @@ struct PrefixToolsTests {
return try #require(PrefixStore.all(libraries: [library]).first)
}
+ @Test("A launch cannot start while a rebuild is running")
+ func rebuildHoldsThePrefixLock() throws {
+ let fm = FileManager.default
+ let dir = fm.temporaryDirectory.appending(path: "np-rebuild-lock-\(UUID().uuidString)")
+ defer { try? fm.removeItem(at: dir) }
+ let home = dir.appending(path: "home")
+ try fm.createDirectory(at: home, withIntermediateDirectories: true)
+ let prefix = try usedPrefix(in: dir, home: home)
+ let lock = prefix.root.appending(path: ".notproton-prefix.lock").path(percentEncoded: false)
+ let launched = dir.appending(path: "launched")
+ let runner = try fakeRunner(
+ in: dir, home: home,
+ extra: "/usr/bin/lockf -s -t 0 '\(lock)' touch '\(launched.path(percentEncoded: false))' || :")
+
+ try PrefixTools.recreate(prefix, runner: runner)
+
+ #expect(!fm.fileExists(atPath: launched.path(percentEncoded: false)))
+ #expect(!PrefixStore.isInUse(prefix, root: dir.appending(path: "sockets")))
+ }
+
// Why a rebuild is offered instead of a delete: the arch specific half only arrives with
// the template, while the profile has nothing arch specific and is what a player misses.
@Test("A rebuild keeps the Windows user profile and replaces everything else")