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

pkg: minor fix is sys_poll library #11497

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

gridbugs
Copy link
Collaborator

Io.String_path.lines_of_file raises Sys_error rather than Unix_error, so the way we were handling errors in sys_poll would not correctly handle cases where files are missing. Since Sys_error does not provide information about the nature of the error, explicitly check that the file exists and is a regular file rather than relying on exceptions.

| exception Unix.Unix_error (Unix.ENOENT, _, _) -> None
if Sys.file_exists p && Sys.is_regular_file p
then Some (Io.String_path.lines_of_file p)
else None
Copy link
Collaborator

Choose a reason for hiding this comment

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

Wouldn't it be better to catch the Sys_error and then do the checks? That way the stat calls only happen on the exception case and don't need to be paid on every invocation of the function.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good point. I've changed it to optimistically read lines and to return None if Sys_error is raised. I'm not sure if I should keep the checks (Sys.file_exists p && Sys.is_regular_file p) in the exception case or just return None in response to all Sys_error exceptions.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sys.is_regular_file only exists starting with 5.1, you'd need to use stat instead, but for now I'd just leave it out until it proves to be a problem.

`Io.String_path.lines_of_file` raises `Sys_error` rather than
`Unix_error`.

Signed-off-by: Stephen Sherratt <[email protected]>
@gridbugs gridbugs force-pushed the sys-poll-read-lines-fix branch from 85c79f0 to e5f87bf Compare February 25, 2025 04:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants