Skip to content

ProcessID enhacement #8618

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

johnbute
Copy link
Contributor

@johnbute johnbute commented May 5, 2025

Displays the PID of the SwiftPM instance currently holding the lock

Motivation:

Fixes #8528

Modifications:

Added a new abstraction for handling a .pid file which contains the pID of the currently running SwiftPM instance.

introduced the pidfileManipulator protocol and its implementation in pidFile struct to handle reading, writing, and deleting the PID file

Updated SwiftCommandState to use the pidFileManipulator to handle the pid file when attempting to acquire or release the lock.

Result:

Enhancement of knowing which processes are currently holding the lock + automatic cleanup

@johnbute johnbute marked this pull request as ready for review May 5, 2025 15:04
@johnbute johnbute marked this pull request as draft May 5, 2025 15:04
@johnbute
Copy link
Contributor Author

johnbute commented May 5, 2025

Still need to add tests + rename to something clearer. if you have any suggestions on naming, please let me know :)

@johnbute johnbute marked this pull request as ready for review May 7, 2025 14:54
@johnbute
Copy link
Contributor Author

johnbute commented May 7, 2025

@swift-ci please test

@johnbute
Copy link
Contributor Author

johnbute commented May 7, 2025

@swift-ci

@johnbute
Copy link
Contributor Author

johnbute commented May 7, 2025

@swift-ci please test

@johnbute johnbute marked this pull request as draft May 7, 2025 16:55
@johnbute johnbute force-pushed the feature/pid-enhancement branch from 0e93697 to 1963d73 Compare May 7, 2025 18:50
@johnbute johnbute marked this pull request as ready for review May 7, 2025 18:51
@johnbute
Copy link
Contributor Author

johnbute commented May 7, 2025

@swift-ci please test

1 similar comment
@johnbute
Copy link
Contributor Author

johnbute commented May 7, 2025

@swift-ci please test

//
// This source file is part of the Swift open source project
//
// Copyright (c) 2023 Apple Inc. and the Swift project authors
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I'm unsure as to whether this code was pre-existing elsewhere, but if this is entirely new then I'd suggest pasting this updated header from swift.org! :)

@johnbute johnbute force-pushed the feature/pid-enhancement branch from 10b1dd4 to 670f56a Compare May 12, 2025 16:24
@johnbute
Copy link
Contributor Author

@swift-ci please test

// Check if the file exists
let filePath = self.lockFilePath.pathString
guard FileManager.default.fileExists(atPath: filePath) else {
print("File does not exist at path: \(filePath)")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (blocking): I think that these print statements need to be emitted through observability scopes instead of printing directly to stdout here.

@@ -533,6 +533,45 @@ final class SwiftCommandStateTests: CommandsTestCase {
}
}
}

func testPIDFileHandlerLifecycle() async throws {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: These new tests are validating the PIDFile structure. Can we move the tests to a new location? Maybe under Tests/Basics/Concurrency/PIDTests.swift

Also, consider using Swift Testing instead of XCTest

do {
try self.pidManipulator.deletePIDFile()
} catch {
self.observabilityScope.emit(warning: "Failed to delete PID file: \(error)")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thought: this message may be misleading. As a developer, I might wonder "what is the PID file" . What is the side effect of the existence of this file? Will the next "write" fail? if not, can we omit notifying the user?

}

/// Read the pid file
public func readPID() -> Int32? {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (non-blocking): consider adding small tests that would validate all code paths in isolation.

}

/// Delete PID file at URL
public func deletePIDFile() throws {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (non-blocking): consider adding small (ie: unit) tests that validate this function in isolation:

  • self.lockFilePath exists before calling function
  • self.locFilePath does no exists before calling function


// Write current PID
let currentPID = pidHandler.getCurrentPID()
try pidHandler.writePID(pid: currentPID)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: can we add a test where multiple writePID(...) are calls sequentially?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Display the process ID in the when running a Swift PM command that has a hold of the .build directory
4 participants