Skip to content

Update FileLock API #2036

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

Closed
wants to merge 1 commit into from
Closed

Update FileLock API #2036

wants to merge 1 commit into from

Conversation

krzyzanowskim
Copy link
Contributor

Update FileLock API.

This is a preparation work for workspace locking.

@krzyzanowskim krzyzanowskim mentioned this pull request Mar 8, 2019
private var fd: CInt?
private var fd: FileDescriptor?

private let _lock = NSLock()
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we need to make FileLock thread-safe. In the future, Workspace itself should implement thread safety for its operations.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

anything wrong with thread safety here?

Copy link
Contributor

Choose a reason for hiding this comment

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

IMO it's just a little premature to add it unless there is a clear use case. It can manifest in weird bugs or can end up hiding real bugs since it'll kind of make Workspace thread-safe as a side-effect.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If making a class (that is used once, in tests, and works just fine) thread-safe is a blocker, I don't know, I can only give up. Unless there is an actual problem spotted.

Copy link
Contributor

Choose a reason for hiding this comment

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

It's not about how much it's being used right now. It's about what kind of abstraction we want to design for flock. In many cases, thread-safety will need to be implemented at a higher-level than FileLock anyway and then we end up with an unnecessary lock. Since this class abstracting flock, maybe we should see if flock itself is thread-safe. From my very little search, it doesn't look like it is explicitly marked thread-safe.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

normally this kind of access if thread safe, if you look at foundation too. since this is protecting access to the resource, I find lock appropriate to use.

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it is internally though, if you check sources

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed NSLock hoping this was the only obstacle.

@krzyzanowskim
Copy link
Contributor Author

The lock if for the FileLock operations only. It wont lock the workspace in any way though

@krzyzanowskim
Copy link
Contributor Author

@aciidb0mb3r please re-review this PR.

@@ -85,19 +114,22 @@ public final class FileLock {
#else
// Open the lock file.
if fileDescriptor == nil {
let fd = SPMLibc.open(lockFile.pathString, O_WRONLY | O_CREAT | O_CLOEXEC, 0o666)
let fd = SPMLibc.open(path.pathString, O_WRONLY | O_CREAT | O_CLOEXEC, 0o666)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you update this to use the new openFile(at:) helper?

/// that indicates whether the attempt was successful.
///
/// - Returns: `true` if the lock was acquired, otherwise `false`.
public func `try`() -> Bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

This won't compile on windows so should guard it with #if os() checks.

let fileLock = FileLock(name: "test.lock", in: tempDir.path)

let lockThread = Thread {
try! SwiftPMProduct.TestSupportExecutable.execute(["fileLockLock", "test.lock", tempDir.path.pathString, "3"])
Copy link
Contributor

Choose a reason for hiding this comment

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

Given the trivial implementation, I don't think it's worth adding a test case that will sleep for 3 seconds. It seems fine to land this PR without a test for the new try method.

@tomerd
Copy link
Contributor

tomerd commented Aug 31, 2020

@krzyzanowskim sorry for the slow response. is this still something you still want to pursue?

@krzyzanowskim
Copy link
Contributor Author

@tomerd yes

@tomerd
Copy link
Contributor

tomerd commented Sep 1, 2020

@krzyzanowskim cool. can you share a bit of background on this, i.e. how is this feature being used? also we'd need to rebase to catch up on changes (sorry)

@tomerd
Copy link
Contributor

tomerd commented Oct 5, 2020

hey @krzyzanowskim did you want to update this PR so we can take it forward?

@krzyzanowskim
Copy link
Contributor Author

@tomerd I may work on it, do you want this functionality in?

@tomerd
Copy link
Contributor

tomerd commented Oct 5, 2020

@krzyzanowskim yes I believe we do, there area also potential touchpoint with swiftlang/swift-tools-support-core#130 which is designed to enable #2835, so ideally we come together to finalize both so we can work on the features that need this

/cc @abertelrud @neonichu

@krzyzanowskim
Copy link
Contributor Author

Would you also be interested in #2037 ? This was the tool to the actual feature I needed at that time which is workspace lock.

@shahmishal shahmishal closed this Oct 6, 2020
@krzyzanowskim
Copy link
Contributor Author

ok computer. what just happened?

@shahmishal
Copy link
Member

@krzyzanowskim I just deleted master branch, please update the base branch to main

@tomerd
Copy link
Contributor

tomerd commented Oct 6, 2020

Hi @krzyzanowskim, the Swift project moved the default branch to main and deleted master branch, so GitHub automatically closed the PR. Please rebase on top of the main and we can reopen.

More detail about the branch update - https://forums.swift.org/t/updating-branch-names/40412

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.

4 participants