Issue #16962 has been updated by akr (Akira Tanaka).


We don't close 0 (stdin), 1 (stdout), 2 (stderr) because
they can be used from various library.

If we close 2, next open() return 2.
Assume that we open a database file.
If open() returns 2 for the database,
the database would be corrupted when ruby raises an error that error messages are printed to stderr.

I feel that IO.for_fd with autoclose=true is appropriate for
applications which takes a file descriptor as a command line option.
(valgrind --log-fd=FD, etc.)
In that case, IO object created by IO.for_fd should closed when the IO object is garbage collected.

I agree that autoclose=false is appropriate for testing but
I guess real applications need autoclose=true more.









----------------------------------------
Feature #16962: Make IO.for_fd autoclose option default to false
https://bugs.ruby-lang.org/issues/16962#change-92554

* Author: ioquatix (Samuel Williams)
* Status: Open
* Priority: Normal
----------------------------------------
I discussed this with @eregon and I think the goal here is to try and figure out a way these interfaces can be a bit less confusing. 

## 1. I don't understand this behaviour:

```ruby
STDOUT.close
STDOUT.puts "Hello World"
# => closed stream
```

vs

```ruby
IO.for_fd(STDOUT.fileno, autoclose: true).close
STDOUT.puts "Hello World"
# => Hello World
```

## 2. `IO.for_fd(..., autoclose: true/false)`

The documentation for `autoclose` is:

> If the value is false, the fd will be kept open after this IO instance gets finalized.

But it also seems to affect `#close` - i.e. calling close does not close underlying file descriptor.

Should we fix the documentation or is the implementation wrong? Maybe the name `autoclose:` is very confusing. My initial interpretation was it was just 'automatically close this I/O when it is garbage collected'.

## 3. `IO.for_fd(..., autoclose: false)` default

In most cases, it seems like `autoclose: false` would make more sense as the default, since the file descriptor must come from some other place.




-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>