Skip to content

Commit e315e5e

Browse files
committed
merge revision(s) r47288: [Backport ruby#10153]
* io.c (io_close): ignore only "closed stream" IOError and NoMethodError, do not swallow other exceptions at the end of block. [ruby-core:64463] [Bug ruby#10153] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_1@47486 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent 3de9369 commit e315e5e

File tree

4 files changed

+42
-7
lines changed

4 files changed

+42
-7
lines changed

ChangeLog

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
Wed Sep 10 03:29:48 2014 Nobuyoshi Nakada <[email protected]>
2+
3+
* io.c (io_close): ignore only "closed stream" IOError and
4+
NoMethodError, do not swallow other exceptions at the end of
5+
block. [ruby-core:64463] [Bug #10153]
6+
17
Wed Sep 10 03:17:13 2014 Nobuyoshi Nakada <[email protected]>
28

39
* enc/trans/euckr-tbl.rb (EUCKR_TO_UCS_TBL): add missing euro and

io.c

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,8 @@ is_socket(int fd, VALUE path)
590590
}
591591
#endif
592592

593+
static const char closed_stream[] = "closed stream";
594+
593595
void
594596
rb_eof_error(void)
595597
{
@@ -616,7 +618,7 @@ rb_io_check_closed(rb_io_t *fptr)
616618
{
617619
rb_io_check_initialized(fptr);
618620
if (fptr->fd < 0) {
619-
rb_raise(rb_eIOError, "closed stream");
621+
rb_raise(rb_eIOError, closed_stream);
620622
}
621623
}
622624

@@ -1075,7 +1077,7 @@ int
10751077
rb_io_wait_readable(int f)
10761078
{
10771079
if (f < 0) {
1078-
rb_raise(rb_eIOError, "closed stream");
1080+
rb_raise(rb_eIOError, closed_stream);
10791081
}
10801082
switch (errno) {
10811083
case EINTR:
@@ -1101,7 +1103,7 @@ int
11011103
rb_io_wait_writable(int f)
11021104
{
11031105
if (f < 0) {
1104-
rb_raise(rb_eIOError, "closed stream");
1106+
rb_raise(rb_eIOError, closed_stream);
11051107
}
11061108
switch (errno) {
11071109
case EINTR:
@@ -4062,7 +4064,7 @@ finish_writeconv(rb_io_t *fptr, int noalloc)
40624064
}
40634065
if (rb_io_wait_writable(fptr->fd)) {
40644066
if (fptr->fd < 0)
4065-
return noalloc ? Qtrue : rb_exc_new3(rb_eIOError, rb_str_new_cstr("closed stream"));
4067+
return noalloc ? Qtrue : rb_exc_new3(rb_eIOError, rb_str_new_cstr(closed_stream));
40664068
goto retry;
40674069
}
40684070
return noalloc ? Qtrue : INT2NUM(errno);
@@ -4344,13 +4346,31 @@ rb_io_close_m(VALUE io)
43444346
static VALUE
43454347
io_call_close(VALUE io)
43464348
{
4347-
return rb_funcall(io, rb_intern("close"), 0, 0);
4349+
rb_check_funcall(io, rb_intern("close"), 0, 0);
4350+
return io;
4351+
}
4352+
4353+
static VALUE
4354+
ignore_closed_stream(VALUE io, VALUE exc)
4355+
{
4356+
enum {mesg_len = sizeof(closed_stream)-1};
4357+
VALUE mesg = rb_attr_get(exc, rb_intern("mesg"));
4358+
if (!RB_TYPE_P(mesg, T_STRING) ||
4359+
RSTRING_LEN(mesg) != mesg_len ||
4360+
memcmp(RSTRING_PTR(mesg), closed_stream, mesg_len)) {
4361+
rb_exc_raise(exc);
4362+
}
4363+
return io;
43484364
}
43494365

43504366
static VALUE
43514367
io_close(VALUE io)
43524368
{
4353-
return rb_rescue(io_call_close, io, 0, 0);
4369+
VALUE closed = rb_check_funcall(io, rb_intern("closed?"), 0, 0);
4370+
if (closed != Qundef && RTEST(closed)) return io;
4371+
rb_rescue2(io_call_close, io, ignore_closed_stream, io,
4372+
rb_eIOError, (VALUE)0);
4373+
return io;
43544374
}
43554375

43564376
/*

test/ruby/test_io.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2995,4 +2995,13 @@ def test_sysread_unlocktmp_ensure
29952995
ensure
29962996
t.kill
29972997
end
2998+
2999+
def test_exception_at_close
3000+
bug10153 = '[ruby-core:64463] [Bug #10153] exception in close at the end of block'
3001+
assert_raise(Errno::EBADF, bug10153) do
3002+
IO.pipe do |r, w|
3003+
assert_nothing_raised {IO.open(w.fileno) {}}
3004+
end
3005+
end
3006+
end
29983007
end

version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#define RUBY_VERSION "2.1.2"
22
#define RUBY_RELEASE_DATE "2014-09-10"
3-
#define RUBY_PATCHLEVEL 231
3+
#define RUBY_PATCHLEVEL 232
44

55
#define RUBY_RELEASE_YEAR 2014
66
#define RUBY_RELEASE_MONTH 9

0 commit comments

Comments
 (0)