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

close_out erroneously raising Sys_error #2743

Closed
vicuna opened this issue Apr 11, 2001 · 2 comments
Closed

close_out erroneously raising Sys_error #2743

vicuna opened this issue Apr 11, 2001 · 2 comments
Labels

Comments

@vicuna
Copy link

vicuna commented Apr 11, 2001

Original bug ID: 335
Reporter: administrator
Status: closed
Resolution: not a bug
Priority: normal
Severity: minor
Category: ~DO NOT USE (was: OCaml general)

Bug description

Full_Name: Matt Liggett
Version: OCaml 3.01
OS: OpenBSD/i386 2.8
Submission from: chapman.ciswired.com (206.97.67.75)

See transcript below. Note that opening the file with open_out_gen
seems to be the trigger. The problem also exists under Linux, so I presume
it is not an OpenBSD-specific problem.

$ touch foo
$ ocaml
Objective Caml version 3.01

let file = open_out_gen ~mode:[ Open_append ] ~perm:0o644 "foo" in

begin
output_string file "test";
close_out file;
end;;
Uncaught exception: Sys_error "Bad file descriptor".

let file = open_out "floop" in

begin
output_string file "test";
close_out file;
end;;

  • : unit = ()

@vicuna
Copy link
Author

vicuna commented Apr 12, 2001

Comment author: administrator

See transcript below. Note that opening the file with open_out_gen
seems to be the trigger. The problem also exists under Linux, so I presume
it is not an OpenBSD-specific problem.

It's actually normal Unix (POSIX) behavior. Opening a file in
"append" mode doesn't open it in "write" mode automatically:

let file = open_out_gen ~mode:[ Open_append ] ~perm:0o644 "foo" in

Here, you get a Unix file descriptor that is not opened in write mode.
Caml builds an I/O buffer on top of it.

begin
output_string file "test";

Here, Caml writes to the I/O buffer. No actual Unix I/O yet, so no
error.

close_out file;

Here, Caml flushes its buffer in preparation for closing it. Flushing
means performing an actual Unix write() system call, which lets you
know that it doesn't like your file descriptor:

Uncaught exception: Sys_error "Bad file descriptor".

The problem is not with "close_out" -- any operation that actually
performs a Unix write() operation, such as flushing or writing more
data than fits in the buffer, would do the same.

The solution is of course to open in write mode AND append mode:

let file = open_out_gen ~mode:[Open_write;Open_append] ~perm:0o644 "foo"

I agree this is a bit surprising, but it's all standard Unix
behavior. You'd get exactly the same situation in a C program using
stdio.

Hope this clarifies the issue.

  • Xavier Leroy

@vicuna
Copy link
Author

vicuna commented Apr 12, 2001

Comment author: administrator

Normal Unix behavior. Maybe documentation should say that Open_write must be
present in the flags to open_out_gen.

@vicuna vicuna closed this as completed Apr 12, 2001
@vicuna vicuna added the bug label Mar 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant