Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0000335OCamlOCaml generalpublic2001-04-12 01:192001-04-12 14:31
Reporteradministrator 
Assigned To 
PrioritynormalSeverityminorReproducibilityalways
StatusclosedResolutionno change required 
PlatformOSOS Version
Product Version 
Target VersionFixed in Version 
Summary0000335: close_out erroneously raising Sys_error
DescriptionFull_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 = ()
#

TagsNo tags attached.
Attached Files

- Relationships

-  Notes
(0002251)
administrator (administrator)
2001-04-12 14:30

> 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

(0002252)
administrator (administrator)
2001-04-12 14:31

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

- Issue History
Date Modified Username Field Change
2005-11-18 10:13 administrator New Issue


Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker