Skip to content

Commit 10421fb

Browse files
author
normal
committed
IO#close: do not enqueue redundant interrupts (take #2)
Enqueuing multiple errors for one event causes spurious errors down the line, as reported by Nikolay Vashchenko in https://bugs.ruby-lang.org/issues/13632 This should fix bad interactions with test_race_gets_and_close in test/ruby/test_io.rb since we ensure rb_notify_fd_close continues returning the busy flag after enqueuing the interrupt. Backporting changes to 2.4 and earlier releases will be more challenging... * thread.c (rb_notify_fd_close): do not enqueue multiple interrupts [ruby-core:81581] [Bug ruby#13632] * test/ruby/test_io.rb (test_single_exception_on_close): new test based on script from Nikolay git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59028 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent 95af329 commit 10421fb

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

test/ruby/test_io.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2823,6 +2823,28 @@ def test_cross_thread_close_stdio
28232823
end;
28242824
end
28252825

2826+
def test_single_exception_on_close
2827+
a = []
2828+
t = []
2829+
10.times do
2830+
r, w = IO.pipe
2831+
a << [r, w]
2832+
t << Thread.new do
2833+
while r.gets
2834+
end rescue IOError
2835+
Thread.current.pending_interrupt?
2836+
end
2837+
end
2838+
a.each do |r, w|
2839+
w.write -"\n"
2840+
w.close
2841+
r.close
2842+
end
2843+
t.each do |th|
2844+
assert_equal false, th.value, '[ruby-core:81581] [Bug #13632]'
2845+
end
2846+
end
2847+
28262848
def test_open_mode
28272849
feature4742 = "[ruby-core:36338]"
28282850
bug6055 = '[ruby-dev:45268]'

thread.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2212,10 +2212,16 @@ rb_notify_fd_close(int fd)
22122212
list_for_each(&vm->waiting_fds, wfd, wfd_node) {
22132213
if (wfd->fd == fd) {
22142214
rb_thread_t *th = wfd->th;
2215-
VALUE err = th->vm->special_exceptions[ruby_error_stream_closed];
2215+
VALUE err;
2216+
2217+
busy = 1;
2218+
if (!th) {
2219+
continue;
2220+
}
2221+
wfd->th = 0;
2222+
err = th->vm->special_exceptions[ruby_error_stream_closed];
22162223
rb_threadptr_pending_interrupt_enque(th, err);
22172224
rb_threadptr_interrupt(th);
2218-
busy = 1;
22192225
}
22202226
}
22212227
return busy;

0 commit comments

Comments
 (0)