Skip to content

Lock scratch directory during tool execution #7269

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Sources/Basics/FileSystem/FileSystem+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ extension FileSystem {
}

/// Execute the given block while holding the lock.
public func withLock<T>(on path: AbsolutePath, type: FileLock.LockType, _ body: () throws -> T) throws -> T {
try self.withLock(on: path.underlying, type: type, body)
public func withLock<T>(on path: AbsolutePath, type: FileLock.LockType, blocking: Bool = true, _ body: () throws -> T) throws -> T {
try self.withLock(on: path.underlying, type: type, blocking: blocking, body)
}

/// Returns any known item replacement directories for a given path. These may be used by platform-specific
Expand Down
26 changes: 20 additions & 6 deletions Sources/CoreCommands/SwiftTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import func TSCBasic.exec
import protocol TSCBasic.OutputByteStream
import class TSCBasic.Process
import enum TSCBasic.ProcessEnv
import enum TSCBasic.ProcessLockError
import var TSCBasic.stderrStream
import class TSCBasic.TerminalController
import class TSCBasic.ThreadSafeOutputByteStream
Expand Down Expand Up @@ -108,14 +109,27 @@ extension SwiftCommand {
workspaceLoaderProvider: self.workspaceLoaderProvider
)
swiftTool.buildSystemProvider = try buildSystemProvider(swiftTool)
var toolError: Error? = .none

// Try a non-blocking lock first so that we can inform the user about an already running SwiftPM.
do {
try self.run(swiftTool)
if swiftTool.observabilityScope.errorsReported || swiftTool.executionStatus == .failure {
throw ExitCode.failure
try swiftTool.fileSystem.withLock(on: swiftTool.scratchDirectory, type: .exclusive, blocking: false) {}
} catch let ProcessLockError.unableToAquireLock(errno) {
if errno == EWOULDBLOCK {
swiftTool.outputStream.write("Another instance of SwiftPM is already running using '\(swiftTool.scratchDirectory)', waiting until that process has finished execution...".utf8)
swiftTool.outputStream.flush()
}
}

var toolError: Error? = .none
try swiftTool.fileSystem.withLock(on: swiftTool.scratchDirectory, type: .exclusive) {
do {
try self.run(swiftTool)
if swiftTool.observabilityScope.errorsReported || swiftTool.executionStatus == .failure {
throw ExitCode.failure
}
} catch {
toolError = error
}
} catch {
toolError = error
}

// wait for all observability items to process
Expand Down