|Anonymous | Login | Signup for a new account||2017-05-28 01:11 CEST|
|Main | My View | View Issues | Change Log | Roadmap|
|View Issue Details|
|ID||Project||Category||View Status||Date Submitted||Last Update|
|0000335||OCaml||~DO NOT USE (was: OCaml general)||public||2001-04-12 01:19||2001-04-12 14:31|
|Status||closed||Resolution||no change required|
|Target Version||Fixed in Version|
|Summary||0000335: close_out erroneously raising Sys_error|
|Description||Full_Name: Matt Liggett|
Version: OCaml 3.01
OS: OpenBSD/i386 2.8
Submission from: chapman.ciswired.com (184.108.40.206)
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
Objective Caml version 3.01
# let file = open_out_gen ~mode:[ Open_append ] ~perm:0o644 "foo" in
output_string file "test";
Uncaught exception: Sys_error "Bad file descriptor".
# let file = open_out "floop" in
output_string file "test";
- : unit = ()
|Tags||No tags attached.|
> 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.
> output_string file "test";
Here, Caml writes to the I/O buffer. No actual Unix I/O yet, so no
> 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
Hope this clarifies the issue.
- Xavier Leroy
Normal Unix behavior. Maybe documentation should say that Open_write must be
present in the flags to open_out_gen.
|2005-11-18 10:13||administrator||New Issue|
|2017-02-23 16:36||doligez||Category||OCaml general => -OCaml general|
|2017-03-03 17:55||doligez||Category||-OCaml general => -(deprecated) general|
|2017-03-03 18:01||doligez||Category||-(deprecated) general => ~deprecated (was: OCaml general)|
|2017-03-06 17:04||doligez||Category||~deprecated (was: OCaml general) => ~DO NOT USE (was: OCaml general)|
|Copyright © 2000 - 2011 MantisBT Group|