Skip to content

Commit a7130fb

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 afbe26e commit a7130fb

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
@@ -101,6 +101,16 @@ public protocol SwiftCommand: ParsableCommand, _SwiftCommand {
101101
extension SwiftCommand {
102102
public static var _errorLabel: String { "error" }
103103

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

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

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

0 commit comments

Comments
 (0)