Skip to content

Commit 3dd4e0a

Browse files
committed
Use non-blocking lock on non-interactive terminals
I don't think this is shippable, but it may help to find out more about the issues we saw in self-hosted CI jobs.
1 parent 19184ad commit 3dd4e0a

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

Sources/CoreCommands/SwiftTool.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,16 @@ public protocol SwiftCommand: ParsableCommand, _SwiftCommand {
102102
extension SwiftCommand {
103103
public static var _errorLabel: String { "error" }
104104

105+
private func acquireScratchDirectoryLockIfNeeded<T>(swiftTool: SwiftTool, blocking: Bool = true, _ body: () throws -> T) throws {
106+
let interactive = isatty(STDOUT_FILENO) == 1
107+
_ = try swiftTool.fileSystem.withLock(
108+
on: swiftTool.scratchDirectory,
109+
type: .exclusive,
110+
blocking: interactive ? blocking : false,
111+
body
112+
)
113+
}
114+
105115
public func run() throws {
106116
let swiftTool = try SwiftTool(
107117
options: globalOptions,
@@ -113,7 +123,7 @@ extension SwiftCommand {
113123

114124
// Try a non-blocking lock first so that we can inform the user about an already running SwiftPM.
115125
do {
116-
try swiftTool.fileSystem.withLock(on: swiftTool.scratchDirectory, type: .exclusive, blocking: false) {}
126+
try acquireScratchDirectoryLockIfNeeded(swiftTool: swiftTool, blocking: false) {}
117127
} catch let ProcessLockError.unableToAquireLock(errno) {
118128
if errno == EWOULDBLOCK {
119129
swiftTool.outputStream.write("Another instance of SwiftPM is already running using '\(swiftTool.scratchDirectory)', waiting until that process has finished execution...".utf8)
@@ -122,7 +132,7 @@ extension SwiftCommand {
122132
}
123133

124134
var toolError: Error? = .none
125-
try swiftTool.fileSystem.withLock(on: swiftTool.scratchDirectory, type: .exclusive) {
135+
try acquireScratchDirectoryLockIfNeeded(swiftTool: swiftTool) {
126136
do {
127137
try self.run(swiftTool)
128138
if swiftTool.observabilityScope.errorsReported || swiftTool.executionStatus == .failure {

0 commit comments

Comments
 (0)