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.in_channel_of_descr causes the underlying Unix FD to be closed when the channel is GC'd #6657

Closed
vicuna opened this issue Nov 13, 2014 · 6 comments

Comments

@vicuna
Copy link

vicuna commented Nov 13, 2014

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.

@vicuna
Copy link
Author

vicuna commented Nov 13, 2014

Comment author: berke.durak

Quick workaround is to use Unix.dup.

@vicuna
Copy link
Author

vicuna commented Nov 13, 2014

Comment author: berke.durak

Erratum.

  1. Flags in example should be changed to O_WRONLY
  2. When that is done the issue no longer manifests itself.
  3. However replacing Gc.major () with close_in re-creates the issue.
  4. I noticed the issue in code using a mixture Lwt, Lablgtk and Unix, and using Unix.dup fixed my problem.

@vicuna
Copy link
Author

vicuna commented Nov 17, 2014

Comment author: @damiendoligez

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.

@vicuna
Copy link
Author

vicuna commented Dec 4, 2014

Comment author: gerd

Damien is right, and the test is invalid. write() raises EBADF because the descriptor is opened read-only.

@vicuna
Copy link
Author

vicuna commented Jan 13, 2015

Comment author: @damiendoligez

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.

@vicuna
Copy link
Author

vicuna commented Dec 6, 2015

Comment author: @xavierleroy

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.

@vicuna vicuna closed this as completed Feb 16, 2017
@vicuna vicuna added this to the 4.03.0 milestone Mar 14, 2019
@vicuna vicuna added the bug label Mar 20, 2019
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

1 participant