Modern Apps git

← xtool.git

xutils: use POSIX open/flock for tmpdir locks on Linux

Commit
3ea061bcbd41be90578802a86bcb38cec4f963c3
Author
Vayun Mathur <[email protected]>
When
2026-11-18 15:10 UTC (17 days ago)
Parents
2a4a067
swift-system's FileDescriptor.open crashes in the xtool binary on Linux (Result<_, Errno>.map traps in swift_getEnumCaseMultiPayload), which blocked even 'xtool --version'. Claim/prune now use Glibc open/flock on Linux; other platforms keep FileDescriptor. This fixes the startup crash; a later ArgumentParser generic-metadata crash remains (see session notes).

Diff

Sources/XUtils/TemporaryDirectory.swift
diff --git a/Sources/XUtils/TemporaryDirectory.swift b/Sources/XUtils/TemporaryDirectory.swiftindex 144f86b..25089aa 100644--- a/Sources/XUtils/TemporaryDirectory.swift+++ b/Sources/XUtils/TemporaryDirectory.swift@@ -1,4 +1,7 @@ import Foundation+#if os(Linux)+import Glibc+#endif  package struct TemporaryDirectory: ~Copyable {     private static let debugTmp = ProcessInfo.processInfo.environment["XTL_DEBUG_TMP"] != nil@@ -101,6 +104,16 @@ private struct TemporaryDirectoryRoot {                 options: [.create, .exclusiveLock],                 permissions: [.ownerReadWrite, .groupRead, .otherRead],             )+            #elseif os(Linux)+            // POSIX directly: swift-system's FileDescriptor.open crashes in+            // this binary on Linux (Result<_, Errno>.map traps in+            // swift_getEnumCaseMultiPayload), so avoid it on the startup path.+            let fd = Glibc.open(lockFile.path, O_WRONLY | O_CREAT, 0o644)+            guard fd >= 0 else { throw Errors.tmpdirClaimFailed(url) }+            guard flock(fd, LOCK_EX | LOCK_NB) == 0 else {+                Glibc.close(fd)+                continue+            }             #else             let fd = try FileDescriptor.open(                 FilePath(lockFile.path),@@ -120,6 +133,24 @@ private struct TemporaryDirectoryRoot {         throw Errors.tmpdirClaimFailed(url)     } +    /// Opens `path` and takes an exclusive non-blocking flock.+    /// Returns true (leaving the fd open/closed by the caller path) only+    /// when the lock was acquired. On Linux this uses POSIX directly:+    /// swift-system's FileDescriptor.open crashes in this binary+    /// (Result<_, Errno>.map traps in swift_getEnumCaseMultiPayload).+    private static func tryLockOrphan(at path: String) throws -> Bool {+        #if os(Linux)+        let fd = Glibc.open(path, O_RDWR)+        guard fd >= 0 else { return false }+        defer { Glibc.close(fd) }+        return flock(fd, LOCK_EX | LOCK_NB) == 0+        #else+        let lockFD = try FileDescriptor.open(FilePath(path), .readWrite)+        defer { try? lockFD.close() }+        return try lockFD.tryLock(mode: .exclusive)+        #endif+    }+     private static func pruneOrphans(in url: URL) {         guard let children = try? FileManager.default.contentsOfDirectory(at: url, includingPropertiesForKeys: nil)             else { return }@@ -132,9 +163,7 @@ private struct TemporaryDirectoryRoot {                     continue                 }                 guard basename.hasPrefix("pid-") && basename.hasSuffix(".lock") else { continue }-                let lockFD = try FileDescriptor.open(FilePath(lock.path), .readWrite)-                defer { try? lockFD.close() }-                if try lockFD.tryLock(mode: .exclusive) {+                if try Self.tryLockOrphan(at: lock.path) {                     // we must remove the directory first. if we instead removed the lock first,                     // we could be killed after the lock was removed but before the dir was                     // removed and therefore leave it hanging around.