julia icon indicating copy to clipboard operation
julia copied to clipboard

fsync and/or fdatasync missing (for portability)

Open PallHaraldsson opened this issue 4 months ago • 3 comments

From AI conversation:

Does Julia have a built-in portable fsync?

  • No, not directly. Unlike flush, Julia’s Base/Libc doesn’t expose fsync or fdatasync as a standard API. On Windows, FlushFileBuffers isn’t wrapped either.
  • That means if you want a true durability guarantee, you do have to go through ccall (or use a package that wraps it).

[I did search for and found uv_fs_fdatasync, but not in code, only in one issue).]

There is JL_O_FSYNC in .jl/FileSystem, not sure if enough, or complementary. And O_FSYNC in .h file.

vtjnash/Flock.jl was hallucinated...:

There are packages that already wrap this for you:

  • FilePathsBase.jl → has cross-platform file ops, though not fsync yet.
  • Flock.jl → portable advisory file locking (better than lockfiles).
  • Some database bindings (SQLite.jl, Postgres.jl) already do the right fsync/FlushFileBuffers internally, so you don’t have to.

I'm not sure what other languages expose as API for their stdlib...

PallHaraldsson avatar Aug 19 '25 15:08 PallHaraldsson

I was wanting this today. java.nio.channels.FileChannel.force(boolean) in Java does either an fsync (if passed true) or fdatasync (if passed false). Seems like a good place for C interop for now in Julia :)

alexandergunnarson avatar Dec 02 '25 07:12 alexandergunnarson

If anyone is interested in adding it, it would probably mean adding uv_fsync (see https://docs.libuv.org/en/v1.x/fs.html) to https://github.com/JuliaLang/julia/blob/2a920801860621fcfdd4c672c7533592aa06bba3/src/jl_uv.c#L586

And then exposing that in Julia proper

vchuravy avatar Dec 02 '25 08:12 vchuravy

Sounds good!

For now, I'm just using io_uring with fsync/fdatasync (e.g. io_uring_prep_fsync) rather than libuv. Turns out it's not too hard in Julia.

alexandergunnarson avatar Dec 02 '25 08:12 alexandergunnarson