https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112020-06-16T11:55:06ZRuby Issue Tracking SystemRuby master - Feature #16962: Make IO.for_fd autoclose option default to falsehttps://redmine.ruby-lang.org/issues/16962?journal_id=861882020-06-16T11:55:06Zioquatix (Samuel Williams)samuel@oriontransfer.net
<ul><li><strong>Description</strong> updated (<a title="View differences" href="/journals/86188/diff?detail_id=57344">diff</a>)</li></ul> Ruby master - Feature #16962: Make IO.for_fd autoclose option default to falsehttps://redmine.ruby-lang.org/issues/16962?journal_id=861892020-06-16T11:57:14Zioquatix (Samuel Williams)samuel@oriontransfer.net
<ul><li><strong>Description</strong> updated (<a title="View differences" href="/journals/86189/diff?detail_id=57345">diff</a>)</li></ul> Ruby master - Feature #16962: Make IO.for_fd autoclose option default to falsehttps://redmine.ruby-lang.org/issues/16962?journal_id=917452021-04-29T00:07:56Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul><li><strong>Backport</strong> deleted (<del><i>2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN</i></del>)</li><li><strong>Tracker</strong> changed from <i>Bug</i> to <i>Feature</i></li><li><strong>Subject</strong> changed from <i>IO.close behaviour</i> to <i>Make IO.for_fd autoclose option default to false</i></li></ul><p>I looked into this and I think it's because stdin, stdout, and stderr are special cased:</p>
<pre><code class="c syntaxhl" data-language="c"> <span class="k">if</span> <span class="p">(</span><span class="n">IS_PREP_STDIO</span><span class="p">(</span><span class="n">fptr</span><span class="p">)</span> <span class="o">||</span> <span class="n">fd</span> <span class="o"><=</span> <span class="mi">2</span><span class="p">)</span> <span class="p">{</span>
<span class="cm">/* need to keep FILE objects of stdin, stdout and stderr */</span>
<span class="p">}</span>
</code></pre>
<p>This Ruby program shows the expected behavior:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">out</span> <span class="o">=</span> <span class="no">STDOUT</span><span class="p">.</span><span class="nf">dup</span>
<span class="no">IO</span><span class="p">.</span><span class="nf">for_fd</span><span class="p">(</span><span class="n">out</span><span class="p">.</span><span class="nf">fileno</span><span class="p">,</span> <span class="ss">autoclose: </span><span class="kp">true</span><span class="p">).</span><span class="nf">close</span>
<span class="n">out</span><span class="p">.</span><span class="nf">puts</span> <span class="s2">"Hello World"</span>
</code></pre>
<p>output:</p>
<pre><code>-:3:in `write': Bad file descriptor @ io_writev - <STDOUT> (Errno::EBADF)
from -:3:in `puts'
from -:3:in `<main>'
</code></pre>
<p>I'm not sure it is worth documenting that <code>autoclose</code> does not affect stdin, stdout, or stderr.</p>
<p>I agree that <code>autoclose: false</code> is probably a better default. However, that's a feature request and not a bug fix. Updating subject and switching to feature.</p> Ruby master - Feature #16962: Make IO.for_fd autoclose option default to falsehttps://redmine.ruby-lang.org/issues/16962?journal_id=917462021-04-29T00:21:45Zioquatix (Samuel Williams)samuel@oriontransfer.net
<ul></ul><p>Thanks for looking into this.</p>
<p>Not being able to close some file descriptors when using <code>IO.for_fd</code> is almost certainly a bug.</p>
<pre><code>/* need to keep FILE objects of stdin, stdout and stderr */
</code></pre>
<p>Why?</p> Ruby master - Feature #16962: Make IO.for_fd autoclose option default to falsehttps://redmine.ruby-lang.org/issues/16962?journal_id=917472021-04-29T01:57:04Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul></ul><p>ioquatix (Samuel Williams) wrote in <a href="#note-4">#note-4</a>:</p>
<blockquote>
<p>Thanks for looking into this.</p>
<p>Not being able to close some file descriptors when using <code>IO.for_fd</code> is almost certainly a bug.</p>
<pre><code>/* need to keep FILE objects of stdin, stdout and stderr */
</code></pre>
<p>Why?</p>
</blockquote>
<p>My git blame magic ball shows that Ruby has always done this. It was temporarily changed in <a class="changeset" title="* io.c (fptr_finalize): should close stdin/stdout/stderr when closed explicitly. [ruby-core:2..." href="https://redmine.ruby-lang.org/projects/ruby-master/repository/git/revisions/6f1edacc010c48c5762598fed74ff4f1e62d1b20">6f1edacc010c48c5762598fed74ff4f1e62d1b20</a> and then quickly changed back in <a class="changeset" title="* io.c (fptr_finalize): revert last change. <a href="https://blade.ruby-lang.org/ruby-dev/38648">[ruby-dev:38648]</a> * io.c (fptr_finalize): skip close..." href="https://redmine.ruby-lang.org/projects/ruby-master/repository/git/revisions/d3b852345a646f1058b40033c9f02e3d14c45ab6">d3b852345a646f1058b40033c9f02e3d14c45ab6</a> (both by <a class="user active user-mention" href="https://redmine.ruby-lang.org/users/13">@matz (Yukihiro Matsumoto)</a>).</p> Ruby master - Feature #16962: Make IO.for_fd autoclose option default to falsehttps://redmine.ruby-lang.org/issues/16962?journal_id=925542021-06-17T06:53:08Zakr (Akira Tanaka)akr@fsij.org
<ul></ul><p>We don't close 0 (stdin), 1 (stdout), 2 (stderr) because<br>
they can be used from various library.</p>
<p>If we close 2, next open() return 2.<br>
Assume that we open a database file.<br>
If open() returns 2 for the database,<br>
the database would be corrupted when ruby raises an error that error messages are printed to stderr.</p>
<p>I feel that IO.for_fd with autoclose=true is appropriate for<br>
applications which takes a file descriptor as a command line option.<br>
(valgrind --log-fd=FD, etc.)<br>
In that case, IO object created by IO.for_fd should closed when the IO object is garbage collected.</p>
<p>I agree that autoclose=false is appropriate for testing but<br>
I guess real applications need autoclose=true more.</p> Ruby master - Feature #16962: Make IO.for_fd autoclose option default to falsehttps://redmine.ruby-lang.org/issues/16962?journal_id=925552021-06-17T06:54:52Zakr (Akira Tanaka)akr@fsij.org
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Feedback</i></li></ul>