Skip to content

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

Fix prefix lock issues

NotProtonNotec19ec29f55f
diff --git a/app/Sources/NotProtonApp/Model/RunnerInstaller.swift b/app/Sources/NotProtonApp/Model/RunnerInstaller.swift
index d4b3f6c..074aa1b 100644
--- a/app/Sources/NotProtonApp/Model/RunnerInstaller.swift
+++ b/app/Sources/NotProtonApp/Model/RunnerInstaller.swift
@@ -80,11 +80,16 @@ enum RunnerInstaller {
// Also catches templates left on a drive that was not present (unplugged or unmounted)
// at the time the deployed copy of CrossOver was removed.
- static func removeStalePrefixTemplates(runners: URL, libraries: [SteamLibrary]) -> [StepFailure] {
- removePrefixTemplates(keeping: Set(RunnerStore.clonedBuilds(in: runners)), libraries: libraries)
+ static func removeStalePrefixTemplates(
+ runners: URL, libraries: [SteamLibrary], reportBusy: Bool = true
+ ) -> [StepFailure] {
+ removePrefixTemplates(
+ keeping: Set(RunnerStore.clonedBuilds(in: runners)), libraries: libraries, reportBusy: reportBusy)
}
- static func removePrefixTemplates(keeping builds: Set<String>, libraries: [SteamLibrary]) -> [StepFailure] {
+ static func removePrefixTemplates(
+ keeping builds: Set<String>, libraries: [SteamLibrary], reportBusy: Bool = true
+ ) -> [StepFailure] {
var failures: [StepFailure] = []
for library in libraries {
let kept = Set(builds.flatMap {
@@ -109,7 +114,10 @@ enum RunnerInstaller {
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) }
- guard flock(lock, LOCK_EX | LOCK_NB) == 0 else { throw POSIXError(POSIXErrorCode(rawValue: errno) ?? .EIO) }
+ guard flock(lock, LOCK_EX | LOCK_NB) == 0 else {
+ if errno == EWOULDBLOCK && !reportBusy { continue }
+ throw POSIXError(POSIXErrorCode(rawValue: errno) ?? .EIO)
+ }
let root = openat(parent, SupportPaths.prefixTemplateFolder, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC)
guard root >= 0 else {
diff --git a/app/Sources/NotProtonApp/SystemStatus.swift b/app/Sources/NotProtonApp/SystemStatus.swift
index b4d193f..c91a4d2 100644
--- a/app/Sources/NotProtonApp/SystemStatus.swift
+++ b/app/Sources/NotProtonApp/SystemStatus.swift
@@ -510,7 +510,7 @@ final class SystemStatus {
let known = runnerSizes
let measured = await Task.detached(priority: .utility) {
let failures = cleanTemplates
- ? RunnerInstaller.removeStalePrefixTemplates(runners: runners, libraries: libraries) : []
+ ? RunnerInstaller.removeStalePrefixTemplates(runners: runners, libraries: libraries, reportBusy: false) : []
var sizes: [String: Int64] = [:]
var templates: [String: [CompatTool.Flavor: Int64]] = [:]
for build in RunnerStore.clonedBuilds(in: runners) {
diff --git a/app/Tests/NotProtonAppTests/TemplateCleanupTests.swift b/app/Tests/NotProtonAppTests/TemplateCleanupTests.swift
index 1d674cf..bcb6605 100644
--- a/app/Tests/NotProtonAppTests/TemplateCleanupTests.swift
+++ b/app/Tests/NotProtonAppTests/TemplateCleanupTests.swift
@@ -124,7 +124,7 @@ struct TemplateCleanupTests {
}
@MainActor
- @Test("Refresh retries mounted libraries with no runners and clears the cleanup report")
+ @Test("Refresh skips a busy template lock without a report and cleans up once it is free")
func refreshRetriesWithoutRunners() async throws {
let layout = try Layout()
defer { try? FileManager.default.removeItem(at: layout.root) }
@@ -135,7 +135,7 @@ struct TemplateCleanupTests {
let status = SystemStatus()
await status.refreshRunnerStorage(runners: layout.runners, libraries: [layout.library])
- #expect(status.templateCleanupFailure?.contains(layout.library.compatdata.path) == true)
+ #expect(status.templateCleanupFailure == nil)
#expect(FileManager.default.fileExists(atPath: layout.template.path))
try #require(flock(fd, LOCK_UN) == 0)
diff --git a/dylib/feats/compat_run.sh b/dylib/feats/compat_run.sh
index fbc7959..60a8de0 100755
--- a/dylib/feats/compat_run.sh
+++ b/dylib/feats/compat_run.sh
@@ -65,6 +65,12 @@ if [ "$np_flavor" = rosetta ] || [ ! -x "$WINELOADER" ] || [ ! -x "$WINESERVER"
[ -x "$WINESERVER" ] || WINESERVER="$CX_ROOT/CrossOver-Hosted Application/wineserver-x86"
fi
export WINELOADER WINESERVER
+
+# Keeps Wine from inheriting the prefix and template locks (fd 8 and 9).
+without_lock_fds() {
+ "$@" 8>&- 9>&-
+}
+
# If two WINEDLLPATH directories have the same DLL, Wine uses the one listed first.
export WINEDLLPATH="$CX_ROOT/lib/wine/x86_64-windows:$wine_unix${WINEDLLPATH:+:$WINEDLLPATH}"
export PATH="$CX_ROOT/bin:$PATH"
@@ -400,10 +406,10 @@ import_prefix_settings() {
[ -z "$settings_file" ] || rm -f "$settings_file"
echo "=== could not write the prefix settings, launching without them ===" >> "$log" 2>&1 || true
# the bridge staging still needs a built prefix
- "$WINELOADER" wineboot --init >> "$log" 2>&1 || true
+ without_lock_fds "$WINELOADER" wineboot --init >> "$log" 2>&1 || true
return 0
fi
- "$WINELOADER" reg import "C:\\${settings_file##*/}" >> "$log" 2>&1 \
+ without_lock_fds "$WINELOADER" reg import "C:\\${settings_file##*/}" >> "$log" 2>&1 \
&& import_status=0 || import_status=$?
rm -f "$settings_file"
[ "$import_status" -eq 0 ] \
@@ -426,7 +432,8 @@ runner_id=""
in_template_env() {
prefix="$1"
shift
- env -i HOME="$HOME" USER="${USER:-}" LOGNAME="${LOGNAME:-}" TMPDIR="${TMPDIR:-/tmp}" \
+ without_lock_fds \
+ env -i HOME="$HOME" USER="${USER:-}" LOGNAME="${LOGNAME:-}" TMPDIR="${TMPDIR:-/tmp}" \
LANG="${LANG:-}" LC_ALL="${LC_ALL:-}" \
PATH="$CX_ROOT/bin:/usr/bin:/bin:/usr/sbin:/sbin" CX_ROOT="$CX_ROOT" CX_HOME="$CX_HOME" \
WINEDLLPATH="$CX_ROOT/lib/wine/x86_64-windows:$wine_unix" \
@@ -704,6 +711,7 @@ prepare_prefix_directory() {
exec 8>> "$prefix_lock"
if ! /usr/bin/lockf -s -t 0 8; then
echo "=== another launch is preparing this prefix ===" >> "$log" 2>&1 || true
+ /usr/sbin/lsof -- "$prefix_lock" >> "$log" 2>&1 || true
return 1
fi
for interrupted in "$STEAM_COMPAT_DATA_PATH"/pfx.replaced.*; do
@@ -717,6 +725,7 @@ prepare_prefix_directory() {
if [ -n "$STEAM_COMPAT_DATA_PATH" ]; then
export WINEPREFIX="$STEAM_COMPAT_DATA_PATH/pfx"
+ stage_step="prefix lock"
prepare_prefix_directory
msync_from=environment
if [ -z "$WINEMSYNC" ] && [ -r "$STEAM_COMPAT_DATA_PATH/notproton-msync" ]; then
@@ -734,7 +743,7 @@ if [ -n "$STEAM_COMPAT_DATA_PATH" ]; then
refuse_foreign_prefix
claim_prefix
echo "sync: WINEMSYNC=$WINEMSYNC from $msync_from" >> "$log" 2>&1 || true
- "$WINESERVER" -k >> "$log" 2>&1 || true
+ without_lock_fds "$WINESERVER" -k >> "$log" 2>&1 || true
stage_step="prefix seed"
seed_prefix_from_template
stage_step="profile layout"
diff --git a/dylib/tests/bridgecheck.sh b/dylib/tests/bridgecheck.sh
index ccc497a..8bf37e3 100644
--- a/dylib/tests/bridgecheck.sh
+++ b/dylib/tests/bridgecheck.sh
@@ -4,11 +4,13 @@ set -e
SRC="${1:-$(dirname "$0")/../feats/compat_run.sh}"
[ -f "$SRC" ] || { echo "bridgecheck: $SRC not present, skipped"; exit 0; }
-block=$(sed -n '/^bridge_files="steamclient64/,/^ verify_runner$/p' "$SRC" | sed '$d')
+block=$(sed -n '/^bridge_files="steamclient64/,/^ verify_runner$/p' "$SRC")
case "$block" in
- *'bridge_matches='*) ;;
+ *'bridge_matches='*'
+ verify_runner') ;;
*) echo "FAIL: the bridge staging block not found in $SRC"; exit 1 ;;
esac
+block=$(printf '%s\n' "$block" | sed '$d')
body="$block
fi"
diff --git a/dylib/tests/seedcheck.sh b/dylib/tests/seedcheck.sh
index 368a135..3352cbb 100644
--- a/dylib/tests/seedcheck.sh
+++ b/dylib/tests/seedcheck.sh
@@ -4,9 +4,19 @@ SRC="${1:-$(dirname "$0")/../feats/compat_run.sh}"
work=$(mktemp -d "${TMPDIR:-/tmp}/np-seedcheck.XXXXXX")
test "$(stat -f %d "$work")" = "$(stat -f %d "${TMPDIR:-/tmp}")"
trap 'chmod -R u+rwX "$work"; rm -rf "$work"' EXIT
-functions=$(awk '/^merge_user_dir\(\) \{/{take=1} /^import_prefix_settings\(\) \{/{take=0} take' "$SRC")
+cut_block() {
+ from="$1" to="$2" keep="$3" awk '
+ BEGIN { from = ENVIRON["from"]; to = ENVIRON["to"]; keep = ENVIRON["keep"] }
+ !on && $0 ~ from { on = 1 }
+ on && $0 ~ to { if (keep) print; found = 1; exit }
+ on { print }
+ END { exit !found }' "$SRC" || { echo "FAIL: no block from /$1/ to /$2/ in $SRC" >&2; exit 1; }
+}
+functions=$(cut_block '^merge_user_dir\(\) \{' '^import_prefix_settings\(\) \{' "")
+functions="$functions
+$(cut_block '^runner_id=""' '^if \[ -n ".STEAM_COMPAT_DATA_PATH" \]; then' "")"
functions="$functions
-$(awk '/^runner_id=""/{take=1} /^if \[ -n "\$STEAM_COMPAT_DATA_PATH" \]; then/{take=0} take' "$SRC")"
+$(cut_block '^without_lock_fds\(\) \{' '^\}' 1)"
np_build=fixture
export np_build CX_ROOT CX_HOME wine_unix np_tool_dir WINELOADER WINESERVER
export STEAM_COMPAT_DATA_PATH WINEPREFIX template_dir log seed_scratch seed_building
@@ -200,15 +210,39 @@ check test "$(printf '%s\n' "$isolated" | grep -Ec '^(WINEDLLOVERRIDES|WINEARCH|
fixture
prepare_prefix_directory
-check test "$(env functions="$functions" /bin/sh -c "
- exec 8>&- 9>&-
- eval \"\$functions\"
- prepare_prefix_directory && echo unlocked || echo locked
-")" = locked
+probe_lock() {
+ env functions="$functions" /bin/sh -c "
+ exec 8>&- 9>&-
+ eval \"\$functions\"
+ prepare_prefix_directory && echo unlocked || echo locked
+ "
+}
+check test "$(probe_lock)" = locked
exec 8>&-
check prepare_prefix_directory
exec 8>&-
+cat > "$work/daemonize" <<'STUB'
+#!/bin/sh
+sleep 30 &
+echo "$!" > "$1"
+STUB
+chmod +x "$work/daemonize"
+
+fixture
+prepare_prefix_directory
+"$work/daemonize" "$case_root/leaked.pid"
+exec 8>&-
+check test "$(probe_lock)" = locked
+kill "$(cat "$case_root/leaked.pid")" 2>/dev/null || true
+
+fixture
+prepare_prefix_directory
+without_lock_fds "$work/daemonize" "$case_root/held.pid"
+exec 8>&-
+check test "$(probe_lock)" = unlocked
+kill "$(cat "$case_root/held.pid")" 2>/dev/null || true
+
fixture
mkdir -p "$STEAM_COMPAT_DATA_PATH/pfx.replaced.123"
printf save > "$STEAM_COMPAT_DATA_PATH/pfx.replaced.123/save"
diff --git a/dylib/tests/settingscheck.sh b/dylib/tests/settingscheck.sh
index c8e580d..4c81680 100644
--- a/dylib/tests/settingscheck.sh
+++ b/dylib/tests/settingscheck.sh
@@ -6,6 +6,10 @@ SRC="${1:-$(dirname "$0")/../feats/compat_run.sh}"
body=$(sed -n '/^import_prefix_settings() {$/,/^}$/p' "$SRC")
[ -n "$body" ] || { echo "FAIL: import_prefix_settings not found in $SRC"; exit 1; }
+wrapper=$(sed -n '/^without_lock_fds() {$/,/^}$/p' "$SRC")
+[ -n "$wrapper" ] || { echo "FAIL: without_lock_fds not found in $SRC"; exit 1; }
+body="$wrapper
+$body"
work=$(mktemp -d)
trap 'chmod -R u+w "$work"; rm -rf "$work"' EXIT