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

Unix.open_process_in can lead to 'Bad file descriptor' exceptions in what seem to me like reasonable use cases #6851

Closed
vicuna opened this issue Apr 28, 2015 · 5 comments
Assignees

Comments

@vicuna
Copy link

vicuna commented Apr 28, 2015

Original bug ID: 6851
Reporter: avaron
Assigned to: @mshinwell
Status: closed (set by @mshinwell on 2015-04-28T15:48:41Z)
Resolution: not a bug
Priority: normal
Severity: minor
Platform: Linux
OS: CentOS
OS Version: 6.6 (Final)
Version: 4.02.1
Category: otherlibs
Monitored by: ascvortov

Bug description

This code snippet is perhaps the easiest way to reproduce the issue:

let a = Unix.open_process_in "echo 'yes'" in
ignore (Unix.close_process_in a);
ignore (input_line a);

I would have expected End_of_file in input_line. The line may or may not have 'yes', I would basically be sold on both options.

@vicuna
Copy link
Author

vicuna commented Apr 28, 2015

Comment author: @dbuenzli

I would actually expect exactly what happened to you. The documentation is quite clear, Unix.close_process_in closes the channel. See the documentation of Pervasives.close_in to see what that means.

@vicuna
Copy link
Author

vicuna commented Apr 28, 2015

Comment author: @mshinwell

Unfortunately I think there may be a bug here. I think the fact that the example involves [Unix.process] functions isn't relevant.

Looking at the documentation for [close_in], I see:

"Input functions raise a Sys_error exception when they are applied to a closed input channel"

which makes me think that [input_line] should raise [Sys_error], not [End_of_file]. However, the implementation of [input_line] (in particular, the function [caml_ml_input_scan_line] and so forth) do not seem to check whether [channel->fd] is minus one before trying to read. This means that it can attempt to read from fd -1, causing "bad file descriptor". (Thankfully I think it always uses -1, so there's no problem with a file descriptor being closed and reopened onto something else.)

The fix is presumably to add the check as to whether [channel->fd == -1], but I'm not sure of all the potential ramifications of such change.

@vicuna
Copy link
Author

vicuna commented Apr 28, 2015

Comment author: @dbuenzli

This means that it can attempt to read from fd -1, causing "bad file descriptor".

Which is reported as a Sys_error "Bad file descriptor" (at least on osx) so I fail to see the problem here.

@vicuna
Copy link
Author

vicuna commented Apr 28, 2015

Comment author: @mshinwell

Ah yes, I failed to notice that the error text is indeed wrapped in Sys_error :) In which case, I think you're right, the semantics appears to be as per the documentation.

@vicuna
Copy link
Author

vicuna commented Apr 28, 2015

Comment author: @mshinwell

(Please re-open this if someone disagrees)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants