-
Notifications
You must be signed in to change notification settings - Fork 103
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
Add directory locking mechanism #500
Conversation
10a4e0c
to
a9c338a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Bit unfortunate that this is Unix-only, but I guess we can always add Windows support later if we continue working on that.
internal/api/lib.go
Outdated
_ = syscall.Flock(int(cache.lockfile.Fd()), syscall.LOCK_UN) | ||
cache.lockfile.Close() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Closing the file releases the lock anyways, so no need for the explicit syscall
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, are you sure? I found it this way in some example. Don't know. Should we remove the unlock?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The linux manpage for flock
mentions:
[...] the lock is released either by an explicit LOCK_UN operation on any of these duplicate descriptors, or when all such descriptors have been closed.
For bsd, it's described in the page about close
:
on the last close of a file holding an advisory lock the lock is released
Whether to remove it or not is probably more of a taste question, since this is not a hot path (In fact, as far as I can see, wasmd never calls this).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and on my Macbook, the TestInitLockingPreventsConcurrentAccess
test still passes if I remove the syscall
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll remove it and just add a comment that closing the file releases the lock for simplicity
Oh, interesting. Do you have more information we can add ass a comment or something? I was assuming this is a wrapper around some system specific locking. The Rust one has some sort of Windows support too. |
I don't know about file locking specifically, but the Rust stdlib tries to provide cross-platform wrappers wherever possible and all platform specific stuff is clearly marked in corresponding packages.
So, maybe we should use sys/unix instead? |
a9c338a
to
cf4ca13
Compare
Oh that's great insights. I was not aware that those different APIs per target platform exist. Reading through https://go.googlesource.com/proposal/+/refs/heads/master/design/freeze-syscall.md makes me think using golang.org/x/sys/unix is a good approach as it makes explicit what we do here. We can build a cross-platform solution ourselves if needed one day. But right now all of internal/api cannot work on Windows since it requires a working libwasmvm build which we don't have. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good 👍
In the past we have seen cases in which multiple instaces ran on the same directory in parallel (especially test setups), leading to unreliable and hard to debug issues down the road.