Project

General

Profile

Bug #5952

コマンドラインモードで CGI.new を 2 回実行すると IOError が発生する

Added by hsbt (Hiroshi SHIBATA) almost 6 years ago. Updated over 5 years ago.

Status:
Closed
Priority:
Normal
Target version:
ruby -v:
ruby 1.9.3p28 (2012-01-28 revision 34386) [x86_64-darwin11.2.0]
[ruby-dev:45160]

Description

=begin
1.9.3p28, trunk で以下のスクリプトを実行すると2回目の CGI.new で パラメータを受け取る時に IOError が発生します。
% cat cli.rb
require 'cgi'
CGI.new
CGI.new

% ruby cli.rb

(offline mode: enter name=value pairs on standard input)
/Users/hsbt/.rbenv/versions/2.0.0-dev/lib/ruby/2.0.0/cgi/core.rb:595:in tty?': closed stream (IOError)
from /Users/hsbt/.rbenv/versions/2.0.0-dev/lib/ruby/2.0.0/cgi/core.rb:595:in
read_from_cmdline'
from /Users/hsbt/.rbenv/versions/2.0.0-dev/lib/ruby/2.0.0/cgi/core.rb:645:in initialize_query'
from /Users/hsbt/.rbenv/versions/2.0.0-dev/lib/ruby/2.0.0/cgi/core.rb:806:in
initialize'
from cli.rb:4:in new'
from cli.rb:4:in
'

1.9.2p290 や 1.8.7p357 ではエラーなく2回パラメータを入力可能です。
=end

Associated revisions

Revision 34409
Added by nobu (Nobuyoshi Nakada) almost 6 years ago

  • io.c (argf_close): skip stdin, which should be readable again. [Bug #5952]
  • io.c (argf_readlines): reinitialize after all read to be readable again.

Revision 34409
Added by nobu (Nobuyoshi Nakada) almost 6 years ago

  • io.c (argf_close): skip stdin, which should be readable again. [Bug #5952]
  • io.c (argf_readlines): reinitialize after all read to be readable again.

Revision 34409
Added by nobu (Nobuyoshi Nakada) almost 6 years ago

  • io.c (argf_close): skip stdin, which should be readable again. [Bug #5952]
  • io.c (argf_readlines): reinitialize after all read to be readable again.

Revision 34409
Added by nobu (Nobuyoshi Nakada) almost 6 years ago

  • io.c (argf_close): skip stdin, which should be readable again. [Bug #5952]
  • io.c (argf_readlines): reinitialize after all read to be readable again.

Revision 34417
Added by naruse (Yui NARUSE) almost 6 years ago

  • io.c (argf_next_argv): reset ARGF.next_p on ARGV.replace. r34409 breaks replacing ARGV. [Bug #5952]

Revision 34417
Added by naruse (Yui NARUSE) almost 6 years ago

  • io.c (argf_next_argv): reset ARGF.next_p on ARGV.replace. r34409 breaks replacing ARGV. [Bug #5952]

Revision 34417
Added by naruse (Yui NARUSE) almost 6 years ago

  • io.c (argf_next_argv): reset ARGF.next_p on ARGV.replace. r34409 breaks replacing ARGV. [Bug #5952]

Revision 34417
Added by naruse (Yui NARUSE) almost 6 years ago

  • io.c (argf_next_argv): reset ARGF.next_p on ARGV.replace. r34409 breaks replacing ARGV. [Bug #5952]

History

#1 [ruby-dev:45161] Updated by nagachika (Tomoyuki Chikanaga) almost 6 years ago

  • Category set to core

r29781 以降 Kernel#readlines (ARGF.readlines) で EOF を検出すると stdin を close するようになっていたようです。以下のようなサンプルで現象を確認できます。

readlines.rb:
p STDIN.closed? # => false
p readlines # => [] <- Ctrl-D で EOF を送出
p STDIN.closed? # => true
p readlines # => [] <- すぐにかえってくる

1.9.2 では最後の STDIN.closed? は false のままで、2度目の readlines は入力を待ちます。

ARGF.current_file が標準入力だったら close しないようにするととりあえずこの現象は直りました。これでどうでしょう。

diff --git a/io.c b/io.c
index a3a45c0..e877f13 100644
--- a/io.c
+++ b/io.c
@@ -7684,7 +7684,8 @@ argf_readlines(int argc, VALUE *argv, VALUE argf)
}
else {
lines = rb_io_readlines(argc, argv, ARGF.current_file);
- argf_close(ARGF.current_file);
+ if ( ARGF.current_file != rb_stdin )
+ argf_close(ARGF.current_file);
}
ARGF.next_p = 1;
rb_ary_concat(ary, lines);

#2 Updated by nobu (Nobuyoshi Nakada) almost 6 years ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r34409.
Hiroshi, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


  • io.c (argf_close): skip stdin, which should be readable again. [Bug #5952]
  • io.c (argf_readlines): reinitialize after all read to be readable again.

#3 [ruby-dev:45163] Updated by hsbt (Hiroshi SHIBATA) almost 6 years ago

r34409 で修正されているのを確認しました。ありがとうございます。

#4 [ruby-dev:45164] Updated by naruse (Yui NARUSE) almost 6 years ago

  • Status changed from Closed to Assigned
  • Assignee set to iwamatsu (Nobuhiro Iwamatsu)

#5 [ruby-dev:45165] Updated by naruse (Yui NARUSE) almost 6 years ago

  • Assignee changed from iwamatsu (Nobuhiro Iwamatsu) to nobu (Nobuyoshi Nakada)

#6 [ruby-dev:45168] Updated by naruse (Yui NARUSE) almost 6 years ago

  • Status changed from Assigned to Closed

The test is removed on r34412.

#7 [ruby-dev:45162] Updated by kosaki (Motohiro KOSAKI) over 5 years ago

2012年1月31日9:34 Tomoyuki Chikanaga nagachika00@gmail.com:

Issue #5952 has been updated by Tomoyuki Chikanaga.

Category set to core

r29781 以降 Kernel#readlines (ARGF.readlines) で EOF を検出すると stdin を close するようになっていたようです。以下のようなサンプルで現象を確認できます。

readlines.rb:
p STDIN.closed? # => false
p readlines # => [] <- Ctrl-D で EOF を送出
p STDIN.closed? # => true
p readlines # => [] <- すぐにかえってくる

1.9.2 では最後の STDIN.closed? は false のままで、2度目の readlines は入力を待ちます。

ARGF.current_file が標準入力だったら close しないようにするととりあえずこの現象は直りました。これでどうでしょう。

経緯をよく分かっていないのですが、stdinはcloseしてはいけないけど、pipeはcloseしていいというのはなにか変な感じがします。一切closeしてはいけないのではないでしょうか

Also available in: Atom PDF