Skip to content

Commit 2725131

Browse files
committed
merge revision(s) 58284,58812,59028: [Backport ruby#13632]
vm_core.h: ruby_error_stream_closed * vm_core.h (ruby_special_exceptions): renamed ruby_error_closed_stream as ruby_error_stream_closed, like the message. speed up IO#close with many threads Today, it increases IO#close performance with many threads: Execution time (sec) name trunk after vm_thread_close 4.276 3.018 Speedup ratio: compare with the result of `trunk' (greater is better) name after vm_thread_close 1.417 This speedup comes because rb_notify_fd_close only scans threads inside rb_thread_io_blocking_region, not all threads in the VM. In the future, this type data structure may allow us to notify waiters of multiple FDs on a single thread (when using Fibers). * thread.c (struct waiting_fd): declare (rb_thread_io_blocking_region): use on-stack list waiter (rb_notify_fd_close): walk vm->waiting_fds instead (call_without_gvl): remove old field setting (th_init): ditto * vm_core.h (typedef struct rb_vm_struct): add waiting_fds list * (typedef struct rb_thread_struct): remove waiting_fd field (rb_vm_living_threads_init): initialize waiting_fds list I am now kicking myself for not thinking about this 3 years ago when I introduced ccan/list in [Feature ruby#9632] to optimize this same function :< 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/branches/ruby_2_4@59286 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent 86bb8de commit 2725131

File tree

5 files changed

+53
-18
lines changed

5 files changed

+53
-18
lines changed

test/ruby/test_io.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2809,6 +2809,28 @@ def test_cross_thread_close_stdio
28092809
end;
28102810
end
28112811

2812+
def test_single_exception_on_close
2813+
a = []
2814+
t = []
2815+
10.times do
2816+
r, w = IO.pipe
2817+
a << [r, w]
2818+
t << Thread.new do
2819+
while r.gets
2820+
end rescue IOError
2821+
Thread.current.pending_interrupt?
2822+
end
2823+
end
2824+
a.each do |r, w|
2825+
w.write -"\n"
2826+
w.close
2827+
r.close
2828+
end
2829+
t.each do |th|
2830+
assert_equal false, th.value, '[ruby-core:81581] [Bug #13632]'
2831+
end
2832+
end
2833+
28122834
def test_open_mode
28132835
feature4742 = "[ruby-core:36338]"
28142836
bug6055 = '[ruby-dev:45268]'

thread.c

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,11 @@ static int rb_threadptr_pending_interrupt_empty_p(rb_thread_t *th);
9595
#define eTerminateSignal INT2FIX(1)
9696
static volatile int system_working = 1;
9797

98-
#define closed_stream_error GET_VM()->special_exceptions[ruby_error_closed_stream]
98+
struct waiting_fd {
99+
struct list_node wfd_node; /* <=> vm.waiting_fds */
100+
rb_thread_t *th;
101+
int fd;
102+
};
99103

100104
inline static void
101105
st_delete_wrap(st_table *table, st_data_t key)
@@ -1310,7 +1314,6 @@ call_without_gvl(void *(*func)(void *), void *data1,
13101314
rb_thread_t *th = GET_THREAD();
13111315
int saved_errno = 0;
13121316

1313-
th->waiting_fd = -1;
13141317
if (ubf == RUBY_UBF_IO || ubf == RUBY_UBF_PROCESS) {
13151318
ubf = ubf_select;
13161319
data2 = th;
@@ -1433,11 +1436,15 @@ VALUE
14331436
rb_thread_io_blocking_region(rb_blocking_function_t *func, void *data1, int fd)
14341437
{
14351438
volatile VALUE val = Qundef; /* shouldn't be used */
1439+
rb_vm_t *vm = GET_VM();
14361440
rb_thread_t *th = GET_THREAD();
14371441
volatile int saved_errno = 0;
14381442
int state;
1443+
struct waiting_fd wfd;
14391444

1440-
th->waiting_fd = fd;
1445+
wfd.fd = fd;
1446+
wfd.th = th;
1447+
list_add(&vm->waiting_fds, &wfd.wfd_node);
14411448

14421449
TH_PUSH_TAG(th);
14431450
if ((state = EXEC_TAG()) == 0) {
@@ -1448,8 +1455,8 @@ rb_thread_io_blocking_region(rb_blocking_function_t *func, void *data1, int fd)
14481455
}
14491456
TH_POP_TAG();
14501457

1451-
/* clear waiting_fd anytime */
1452-
th->waiting_fd = -1;
1458+
/* must be deleted before jump */
1459+
list_del(&wfd.wfd_node);
14531460

14541461
if (state) {
14551462
TH_JUMP_TAG(th, state);
@@ -2195,16 +2202,23 @@ int
21952202
rb_notify_fd_close(int fd)
21962203
{
21972204
rb_vm_t *vm = GET_THREAD()->vm;
2198-
rb_thread_t *th = 0;
2205+
struct waiting_fd *wfd = 0;
21992206
int busy;
22002207

22012208
busy = 0;
2202-
list_for_each(&vm->living_threads, th, vmlt_node) {
2203-
if (th->waiting_fd == fd) {
2204-
VALUE err = th->vm->special_exceptions[ruby_error_closed_stream];
2209+
list_for_each(&vm->waiting_fds, wfd, wfd_node) {
2210+
if (wfd->fd == fd) {
2211+
rb_thread_t *th = wfd->th;
2212+
VALUE err;
2213+
2214+
busy = 1;
2215+
if (!th) {
2216+
continue;
2217+
}
2218+
wfd->th = 0;
2219+
err = th->vm->special_exceptions[ruby_error_stream_closed];
22052220
rb_threadptr_pending_interrupt_enque(th, err);
22062221
rb_threadptr_interrupt(th);
2207-
busy = 1;
22082222
}
22092223
}
22102224
return busy;
@@ -4839,7 +4853,7 @@ Init_Thread(void)
48394853
rb_define_method(rb_cThread, "name=", rb_thread_setname, 1);
48404854
rb_define_method(rb_cThread, "inspect", rb_thread_inspect, 0);
48414855

4842-
rb_vm_register_special_exception(ruby_error_closed_stream, rb_eIOError, "stream closed");
4856+
rb_vm_register_special_exception(ruby_error_stream_closed, rb_eIOError, "stream closed");
48434857

48444858
cThGroup = rb_define_class("ThreadGroup", rb_cObject);
48454859
rb_define_alloc_func(cThGroup, thgroup_s_alloc);

version.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#define RUBY_VERSION "2.4.2"
2-
#define RUBY_RELEASE_DATE "2017-07-01"
3-
#define RUBY_PATCHLEVEL 132
2+
#define RUBY_RELEASE_DATE "2017-07-08"
3+
#define RUBY_PATCHLEVEL 133
44

55
#define RUBY_RELEASE_YEAR 2017
66
#define RUBY_RELEASE_MONTH 7
7-
#define RUBY_RELEASE_DAY 1
7+
#define RUBY_RELEASE_DAY 8
88

99
#include "ruby/version.h"
1010

vm.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2477,7 +2477,6 @@ th_init(rb_thread_t *th, VALUE self)
24772477
th->status = THREAD_RUNNABLE;
24782478
th->errinfo = Qnil;
24792479
th->last_status = Qnil;
2480-
th->waiting_fd = -1;
24812480
th->root_svar = Qfalse;
24822481
th->local_storage_recursive_hash = Qnil;
24832482
th->local_storage_recursive_hash_for_trace = Qnil;

vm_core.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ enum ruby_special_exceptions {
427427
ruby_error_reenter,
428428
ruby_error_nomemory,
429429
ruby_error_sysstack,
430-
ruby_error_closed_stream,
430+
ruby_error_stream_closed,
431431
ruby_special_error_count
432432
};
433433

@@ -490,6 +490,7 @@ typedef struct rb_vm_struct {
490490
struct rb_thread_struct *main_thread;
491491
struct rb_thread_struct *running_thread;
492492

493+
struct list_head waiting_fds; /* <=> struct waiting_fd */
493494
struct list_head living_threads;
494495
size_t living_thread_num;
495496
VALUE thgroup_default;
@@ -712,8 +713,6 @@ typedef struct rb_thread_struct {
712713
/* passing state */
713714
int state;
714715

715-
int waiting_fd;
716-
717716
/* for rb_iterate */
718717
VALUE passed_block_handler;
719718

@@ -1445,6 +1444,7 @@ void rb_thread_wakeup_timer_thread(void);
14451444
static inline void
14461445
rb_vm_living_threads_init(rb_vm_t *vm)
14471446
{
1447+
list_head_init(&vm->waiting_fds);
14481448
list_head_init(&vm->living_threads);
14491449
vm->living_thread_num = 0;
14501450
}

0 commit comments

Comments
 (0)