You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Original bug ID: 6657 Reporter: berke.durak Status: closed (set by @xavierleroy on 2017-02-16T14:18:30Z) Resolution: not a bug Priority: normal Severity: minor Version: 4.01.0 Target version: 4.03.0+dev / +beta1 Fixed in version: 4.03.0+dev / +beta1 Category: otherlibs Monitored by:@gasche@hcarty@dbuenzli
Bug description
More generally, closing the channel closes the Unix FD even if the Unix FD was opened independently.
Steps to reproduce
open Unix
let _ =
let fd = openfile "/dev/null" [O_RDONLY] 0o644 in
let ic = in_channel_of_descr fd in
Gc.major (); (* Also works with close_in ic; *)
let u = String.make 8 ' ' in
while true do
let _ = write fd u 0 8 in
()
done
The solution is not to increase the refcount. The refcount field is a count of how many heap objects reference this struct channel*, not how many struct channel *s reference the underlying FD.
If you initialize it to nonzero, the finalization function (caml_finalize_channel) will not work properly.
Moreover, the FD is never closed when the channel is GC'd because we are very careful to never automatically close a FD: since closing is a visible side effect, it should only be done explicitly by the user.
What we probably should do, is document the fact that Unix.in_channel_of_descr takes ownership of the underlying FD (and will close it when it is closed) and that the user must dup it if they don't like that.
As of now, the GC doesn't close the underlying FD, and the documentation for Unix.{in,out}_channel_of_descr mentions "Closing the channel also closes the underlying file descriptor". Marking this PR as not needing further action.
Original bug ID: 6657
Reporter: berke.durak
Status: closed (set by @xavierleroy on 2017-02-16T14:18:30Z)
Resolution: not a bug
Priority: normal
Severity: minor
Version: 4.01.0
Target version: 4.03.0+dev / +beta1
Fixed in version: 4.03.0+dev / +beta1
Category: otherlibs
Monitored by: @gasche @hcarty @dbuenzli
Bug description
More generally, closing the channel closes the Unix FD even if the Unix FD was opened independently.
Steps to reproduce
open Unix
let _ =
let fd = openfile "/dev/null" [O_RDONLY] 0o644 in
let ic = in_channel_of_descr fd in
Gc.major (); (* Also works with close_in ic; *)
let u = String.make 8 ' ' in
while true do
let _ = write fd u 0 8 in
()
done
Additional information
Might be fixed by adding refcount argument to
CAMLexport struct channel * caml_open_descriptor_in(int fd)
in byterun/io.c and passing a >0 value from Unix.
The text was updated successfully, but these errors were encountered: