Skip to content

Commit

Permalink
io.c: close before wait
Browse files Browse the repository at this point in the history
* 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/trunk@57422 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
nobu committed Jan 25, 2017
1 parent a7bc6c1 commit 61701ae
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
18 changes: 15 additions & 3 deletions io.c
Original file line number Diff line number Diff line change
Expand Up @@ -4271,7 +4271,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;
Expand Down Expand Up @@ -4331,6 +4331,12 @@ fptr_finalize(rb_io_t *fptr, int noraise)
else
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);
Expand Down Expand Up @@ -4410,13 +4416,15 @@ 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)
{
rb_io_t *fptr;
int fd;
VALUE write_io;
rb_io_t *write_fptr;
int busy;

write_io = GetWriteIO(io);
if (io != write_io) {
Expand All @@ -4431,7 +4439,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;
}
Expand Down Expand Up @@ -6762,7 +6774,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);
Expand Down
21 changes: 21 additions & 0 deletions test/ruby/test_io.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3397,6 +3397,27 @@ def test_race_gets_and_close
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

if RUBY_ENGINE == "ruby" # implementation details
def test_foreach_rs_conversion
make_tempfile {|t|
Expand Down
10 changes: 3 additions & 7 deletions thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -2163,14 +2163,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) {
Expand All @@ -2180,10 +2179,7 @@ rb_thread_fd_close(int fd)
busy = 1;
}
}
if (busy) {
rb_thread_schedule_limits(0);
goto retry;
}
return busy;
}

/*
Expand Down

0 comments on commit 61701ae

Please sign in to comment.