Skip to content
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

Make windows filelocking exclusive #3285

Merged
merged 1 commit into from
Aug 12, 2024

Conversation

apostasie
Copy link
Contributor

@apostasie apostasie commented Aug 9, 2024

See #3284

Very likely fix #3228 and possibly a range of other issues...

@AkihiroSuda AkihiroSuda added this to the v2.0.0 milestone Aug 9, 2024
@AkihiroSuda AkihiroSuda added the platform/Windows/Non-WSL2 Microsoft Windows (non-WSL2) label Aug 9, 2024
@apostasie
Copy link
Contributor Author

apostasie commented Aug 9, 2024

Windows experts review highly welcome here, though the win-CI is green.

Signed-off-by: apostasie <spam_blackhole@farcloser.world>
@@ -31,13 +31,12 @@ func WithDirLock(dir string, fn func() error) error {
}
defer dirFile.Close()
// see https://msdn.microsoft.com/en-us/library/windows/desktop/aa365203(v=vs.85).aspx
// 1 lock immediately
Copy link
Contributor Author

@apostasie apostasie Aug 9, 2024

Choose a reason for hiding this comment

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

This was never "1 = lock immediately" 🤦‍♂️. It is "1 = The function returns immediately if it is unable to acquire the requested lock. Otherwise, it waits.". We obviously want "wait".

@AkihiroSuda AkihiroSuda requested a review from jsturtevant August 9, 2024 06:29
@@ -31,13 +31,12 @@ func WithDirLock(dir string, fn func() error) error {
}
defer dirFile.Close()
// see https://msdn.microsoft.com/en-us/library/windows/desktop/aa365203(v=vs.85).aspx
// 1 lock immediately
if err = windows.LockFileEx(windows.Handle(dirFile.Fd()), 1, 0, 1, 0, &windows.Overlapped{}); err != nil {
if err = windows.LockFileEx(windows.Handle(dirFile.Fd()), windows.LOCKFILE_EXCLUSIVE_LOCK, 0, ^uint32(0), ^uint32(0), new(windows.Overlapped)); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

The docs say:

If an exclusive lock is requested for a range of a file that already has a shared or exclusive lock, the function
 returns the error ERROR_IO_PENDING. The system will signal the event specified in the [OVERLAPPED]
(https://learn.microsoft.com/en-us/windows/desktop/api/minwinbase/ns-minwinbase-overlapped) structure
 after the lock is granted.

So it might be possible that this fails if something already has the lock and the file was opened with asynchronous i/o. It doesn't appear to be the case here so we should be good with this approach, I mention it just in case something changes in the future.

Copy link
Contributor

@jsturtevant jsturtevant left a comment

Choose a reason for hiding this comment

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

LGTM

@apostasie
Copy link
Contributor Author

LGTM

Thanks a lot for the extra info and the review @jsturtevant .

Honestly, I am very close to biting the bullet and writing tests for this stuff.
It is such a miserable experience to debug downstream problems caused by issues in this, as symptoms are by nature misleading / flaky / racy...

I wish we had something usable off the bat instead of having to maintain that ( https://github.com/golang/proposal/blob/master/design/33974-add-public-lockedfile-pkg.md ).

@apostasie
Copy link
Contributor Author

@AkihiroSuda do you think we can get that in?

Copy link
Member

@AkihiroSuda AkihiroSuda left a comment

Choose a reason for hiding this comment

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

Yes, thanks

@AkihiroSuda AkihiroSuda merged commit 728421a into containerd:main Aug 12, 2024
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
platform/Windows/Non-WSL2 Microsoft Windows (non-WSL2)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TestVolumeLsFilter is failing a lot on Windows
3 participants