Skip to content

Eio.Path.load has race condition and fails on /proc #835

@vog

Description

@vog

The current implementation of Eio.Path.load first determines the file size, then reads the file contents into a buffer of that size. This approach has two major downsides:

  1. This introduces a race condition if the file is changing between stat and open/read. It can fail in two directions:
    • If the file became larger in between, Eio.Path.load will load too few data without noticing
    • If the file became smaller in between, Eio.Path.load will reach EOF earlier than expected
  2. This makes reading from /proc largely impossible via Eio.Path.load, as those "files" are generated at the moment they are read, so the kernel simply reports size 0 for them.

Especially the second case caused quite some debugging time for me, which I'd like to save future Eio users from.

For example:

# Eio_main.run @@ fun env ->
  Eio.Path.load Eio.Path.(env#fs / "/proc/sys/fs/file-nr")
- : string = ""

but:

$ cat /proc/sys/fs/file-nr
16320   0       9223372036854775807

I propose to:

  • change Eio.Path.load so that it uses the file size only as a hint for the initial buffer size, and to
  • make Eio.Path.load path otherwise largely equivalent to Eio.Path.with_open_in path Eio.Flow.read_all

What do you think?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions