diff options
author | nagachika <nagachika@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-03-27 16:54:09 +0000 |
---|---|---|
committer | nagachika <nagachika@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-03-27 16:54:09 +0000 |
commit | 65639d44e962419bc1da1f1dfbec232abcced6cd (patch) | |
tree | dc2ace0c0068be2cf528397512ef4ead2b4eee3c | |
parent | d8a1986a05110b2468a256ab94936ef482f010bb (diff) |
merge revision(s) 57422: [Backport #13158]
io.c: close before wait
* io.c (io_close_fptr): notify then close, and wait for other
threads before free fptr. [ruby-core:79262] [Bug #13158]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_3@58177 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | io.c | 18 | ||||
-rw-r--r-- | test/ruby/test_io.rb | 21 | ||||
-rw-r--r-- | thread.c | 10 | ||||
-rw-r--r-- | version.h | 2 |
4 files changed, 40 insertions, 11 deletions
@@ -4202,7 +4202,7 @@ static void free_io_buffer(rb_io_buffer_t *buf); static void clear_codeconv(rb_io_t *fptr); static void -fptr_finalize(rb_io_t *fptr, int noraise) +fptr_finalize_flush(rb_io_t *fptr, int noraise) { VALUE err = Qnil; int fd = fptr->fd; @@ -4266,6 +4266,12 @@ fptr_finalize(rb_io_t *fptr, int noraise) rb_exc_raise(err); } } +} + +static void +fptr_finalize(rb_io_t *fptr, int noraise) +{ + fptr_finalize_flush(fptr, noraise); free_io_buffer(&fptr->rbuf); free_io_buffer(&fptr->wbuf); clear_codeconv(fptr); @@ -4345,6 +4351,7 @@ rb_io_memsize(const rb_io_t *fptr) return size; } +int rb_notify_fd_close(int fd); static rb_io_t * io_close_fptr(VALUE io) { @@ -4352,6 +4359,7 @@ io_close_fptr(VALUE io) int fd; VALUE write_io; rb_io_t *write_fptr; + int busy; write_io = GetWriteIO(io); if (io != write_io) { @@ -4366,7 +4374,11 @@ io_close_fptr(VALUE io) if (fptr->fd < 0) return 0; fd = fptr->fd; - rb_thread_fd_close(fd); + busy = rb_notify_fd_close(fd); + fptr_finalize_flush(fptr, FALSE); + if (busy) { + do rb_thread_schedule(); while (rb_notify_fd_close(fd)); + } rb_io_fptr_cleanup(fptr, FALSE); return fptr; } @@ -6684,7 +6696,7 @@ io_reopen(VALUE io, VALUE nfile) rb_update_max_fd(fd); fptr->fd = fd; } - rb_thread_fd_close(fd); + rb_notify_fd_close(fd); if ((orig->mode & FMODE_READABLE) && pos >= 0) { if (io_seek(fptr, pos, SEEK_SET) < 0 && errno) { rb_sys_fail_path(fptr->pathv); diff --git a/test/ruby/test_io.rb b/test/ruby/test_io.rb index 70c46d55b1..13a4af1b82 100644 --- a/test/ruby/test_io.rb +++ b/test/ruby/test_io.rb @@ -3315,4 +3315,25 @@ End end end; end + + def test_race_closed_stream + bug13158 = '[ruby-core:79262] [Bug #13158]' + closed = nil + IO.pipe do |r, w| + thread = Thread.new do + begin + while r.gets + end + ensure + closed = r.closed? + end + end + sleep 0.01 + r.close + assert_raise_with_message(IOError, /stream closed/) do + thread.join + end + assert_equal(true, closed, "#{bug13158}: stream should be closed") + end + end end @@ -2168,14 +2168,13 @@ rb_threadptr_reset_raised(rb_thread_t *th) return 1; } -void -rb_thread_fd_close(int fd) +int +rb_notify_fd_close(int fd) { rb_vm_t *vm = GET_THREAD()->vm; rb_thread_t *th = 0; int busy; - retry: busy = 0; list_for_each(&vm->living_threads, th, vmlt_node) { if (th->waiting_fd == fd) { @@ -2185,10 +2184,7 @@ rb_thread_fd_close(int fd) busy = 1; } } - if (busy) { - rb_thread_schedule_limits(0); - goto retry; - } + return busy; } /* @@ -1,6 +1,6 @@ #define RUBY_VERSION "2.3.3" #define RUBY_RELEASE_DATE "2017-03-28" -#define RUBY_PATCHLEVEL 286 +#define RUBY_PATCHLEVEL 287 #define RUBY_RELEASE_YEAR 2017 #define RUBY_RELEASE_MONTH 3 |