WebAssembly / wasi-filesystem

Filesystem API for WASI
Other
179 stars 19 forks source link

File locking #2

Open sunfishcode opened 4 years ago

sunfishcode commented 4 years ago

Sqlite and the Emscripten cache file are two cases where file locking has come up in real-world use cases.

The POSIX way to do file locking, with fcntl and F_SETLK/F_GETLK-style locks has the unfortunate property of being per-process, which is unfortunate both for being contrary to what applications actually want, and because we'd prefer to avoid exposing details like which host processes things are running in to wasm programs. (POSIX is considering fixing this, but until they do and systems update to the new mechanism, we need to work with what current systems provide.)

Here are some notes on a WASI file locking API might want to consider:

Mohsen7s commented 2 years ago

Any update on this? Is it going to be implemented ?

sunfishcode commented 2 years ago

The next step here is for someone to propose a specific feature set, so that we can evaluate whether it's sufficient for meaningful use cases, and whether it's implementable on popular platforms.

As an aside, looking at the FreeBSD flock man page, it's not clear that it really is per-file-descriptor. It says it's per-file, which suggests that it's process-wide like fcntl locking is, which would similarly introduce a dependency on the host process boundary.

sunfishcode commented 1 year ago

FreeBSD's flock does appear to be per-file-description, rather than per-process.

I'm going to propose an API which is essentially flock, with shared (read-only) and exclusive (read-write) locking. It applies only to whole files, and not byte ranges. It is advisory, meaning there's no guarantee other processes aren't accessing the file. We can implement it on Windows with LockFileEx. It's on Linux, macOS, and most Unix variants as flock.

andrewrk commented 1 year ago

Thanks @sunfishcode - looking forward to this.

https://github.com/ziglang/zig/issues/13589

https://github.com/ziglang/zig/blob/2823fcabd1974550b889a56e8ada0eb52f3d2080/src/Cache.zig#L859-L886

Please share with me an early draft if you can and I will test-pilot the API.

bjorn3 commented 1 year ago

This is implemented by https://github.com/WebAssembly/wasi-filesystem/pull/69, right?

andrewrk commented 1 year ago

Thanks for pointing that out - I hadn't noticed.

sunfishcode commented 1 year ago

69 is now merged, and on track to be in preview2. This doesn't yet cover byte ranges or mandatory locking, but those can be considered separately.

cberner commented 2 days ago

Was this released as part of preview2? I'm not able to find it in the wasi crate

sunfishcode commented 1 day ago

Unfortunately, file locking was removed around the time of preview2's release in order to keep the scope down, as there were a lot of other things to do to get preview2 released. Now that preview2 is released and the release cadence is established, we could re-introduce it. So I'll re-open this issue to track that.