Skip to content

Commit 63d9ab3

Browse files
committed
merge revision(s) 62934,63210,63215,63309: [Backport #14634]
thread_sync.c: avoid reaching across stacks of dead threads rb_ensure is insufficient cleanup for fork and we must reinitialize all waitqueues in the child process. Unfortunately this increases the footprint of ConditionVariable, Queue and SizedQueue by 8 bytes on 32-bit (16 bytes on 64-bit). [ruby-core:86316] [Bug #14634] variable.c: fix thread + fork errors in autoload This is fairly non-intrusive bugfix to prevent children from trying to reach into thread stacks of the parent. I will probably reuse this idea and redo r62934, too (same bug). * vm_core.h (typedef struct rb_vm_struct): add fork_gen counter * thread.c (rb_thread_atfork_internal): increment fork_gen * variable.c (struct autoload_data_i): store fork_gen * variable.c (check_autoload_data): remove (replaced with get_...) * variable.c (get_autoload_data): check fork_gen when retrieving * variable.c (check_autoload_required): use get_autoload_data * variable.c (rb_autoloading_value): ditto * variable.c (rb_autoload_p): ditto * variable.c (current_autoload_data): ditto * variable.c (autoload_reset): reset fork_gen, adjust indent * variable.c (rb_autoload_load): set fork_gen when setting state * test/ruby/test_autoload.rb (test_autoload_fork): new test [ruby-core:86410] [Bug #14634] thread_sync: redo r62934 to use fork_gen Instead of maintaining linked-lists to store all rb_queue/rb_szqueue/rb_condvar structs; store only a fork_gen serial number to simplify management of these items. This reduces initialization costs and avoids the up-front cost of resetting all Queue/SizedQueue/ConditionVariable objects at fork while saving 8 bytes per-structure on 64-bit. There are no savings on 32-bit. * thread.c (rb_thread_atfork_internal): remove rb_thread_sync_reset_all call * thread_sync.c (rb_thread_sync_reset_all): remove * thread_sync.c (queue_live): remove * thread_sync.c (queue_free): remove * thread_sync.c (struct rb_queue): s/live/fork_gen/ * thread_sync.c (queue_data_type): use default free * thread_sync.c (queue_alloc): remove list_add * thread_sync.c (queue_fork_check): new function * thread_sync.c (queue_ptr): call queue_fork_check * thread_sync.c (szqueue_free): remove * thread_sync.c (szqueue_data_type): use default free * thread_sync.c (szqueue_alloc): remove list_add * thread_sync.c (szqueue_ptr): check fork_gen via queue_fork_check * thread_sync.c (struct rb_condvar): s/live/fork_gen/ * thread_sync.c (condvar_free): remove * thread_sync.c (cv_data_type): use default free * thread_sync.c (condvar_ptr): check fork_gen * thread_sync.c (condvar_alloc): remove list_add [ruby-core:86316] [Bug #14634] thread_sync.c (condvar_ptr): reset fork_gen after forking Otherwise the condition variable waiter list will always be empty, which is wrong :x [Bug #14725] [Bug #14634] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_5@66912 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent e42dcdb commit 63d9ab3

File tree

8 files changed

+162
-15
lines changed

8 files changed

+162
-15
lines changed

test/ruby/test_autoload.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,32 @@ class AutoloadTest
285285
end
286286
end
287287

288+
def test_autoload_fork
289+
EnvUtil.default_warning do
290+
Tempfile.create(['autoload', '.rb']) {|file|
291+
file.puts 'sleep 0.3; class AutoloadTest; end'
292+
file.close
293+
add_autoload(file.path)
294+
begin
295+
thrs = []
296+
3.times do
297+
thrs << Thread.new { AutoloadTest; nil }
298+
thrs << Thread.new { fork { AutoloadTest } }
299+
end
300+
thrs.each(&:join)
301+
thrs.each do |th|
302+
pid = th.value or next
303+
_, status = Process.waitpid2(pid)
304+
assert_predicate status, :success?
305+
end
306+
ensure
307+
remove_autoload_constant
308+
assert_nil $!, '[ruby-core:86410] [Bug #14634]'
309+
end
310+
}
311+
end
312+
end if Process.respond_to?(:fork)
313+
288314
def add_autoload(path)
289315
(@autoload_paths ||= []) << path
290316
::Object.class_eval {autoload(:AutoloadTest, path)}

test/thread/test_cv.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,4 +217,23 @@ def test_dump
217217
Marshal.dump(condvar)
218218
end
219219
end
220+
221+
def test_condvar_fork
222+
mutex = Mutex.new
223+
condvar = ConditionVariable.new
224+
thrs = (1..10).map do
225+
Thread.new { mutex.synchronize { condvar.wait(mutex) } }
226+
end
227+
thrs.each { 3.times { Thread.pass } }
228+
pid = fork do
229+
mutex.synchronize { condvar.broadcast }
230+
exit!(0)
231+
end
232+
_, s = Process.waitpid2(pid)
233+
assert_predicate s, :success?, 'no segfault [ruby-core:86316] [Bug #14634]'
234+
until thrs.empty?
235+
mutex.synchronize { condvar.broadcast }
236+
thrs.delete_if { |t| t.join(0.01) }
237+
end
238+
end if Process.respond_to?(:fork)
220239
end

test/thread/test_queue.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,4 +565,52 @@ def test_queue_with_trap
565565
puts 'exit'
566566
INPUT
567567
end
568+
569+
def test_fork_while_queue_waiting
570+
q = Queue.new
571+
sq = SizedQueue.new(1)
572+
thq = Thread.new { q.pop }
573+
thsq = Thread.new { sq.pop }
574+
Thread.pass until thq.stop? && thsq.stop?
575+
576+
pid = fork do
577+
exit!(1) if q.num_waiting != 0
578+
exit!(2) if sq.num_waiting != 0
579+
exit!(6) unless q.empty?
580+
exit!(7) unless sq.empty?
581+
q.push :child_q
582+
sq.push :child_sq
583+
exit!(3) if q.pop != :child_q
584+
exit!(4) if sq.pop != :child_sq
585+
exit!(0)
586+
end
587+
_, s = Process.waitpid2(pid)
588+
assert_predicate s, :success?, 'no segfault [ruby-core:86316] [Bug #14634]'
589+
590+
q.push :thq
591+
sq.push :thsq
592+
assert_equal :thq, thq.value
593+
assert_equal :thsq, thsq.value
594+
595+
sq.push(1)
596+
th = Thread.new { q.pop; sq.pop }
597+
thsq = Thread.new { sq.push(2) }
598+
Thread.pass until th.stop? && thsq.stop?
599+
pid = fork do
600+
exit!(1) if q.num_waiting != 0
601+
exit!(2) if sq.num_waiting != 0
602+
exit!(3) unless q.empty?
603+
exit!(4) if sq.empty?
604+
exit!(5) if sq.pop != 1
605+
exit!(0)
606+
end
607+
_, s = Process.waitpid2(pid)
608+
assert_predicate s, :success?, 'no segfault [ruby-core:86316] [Bug #14634]'
609+
610+
assert_predicate thsq, :stop?
611+
assert_equal 1, sq.pop
612+
assert_same sq, thsq.value
613+
q.push('restart th')
614+
assert_equal 2, th.value
615+
end if Process.respond_to?(:fork)
568616
end

thread.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4217,6 +4217,8 @@ rb_thread_atfork_internal(rb_thread_t *th, void (*atfork)(rb_thread_t *, const r
42174217
}
42184218
rb_vm_living_threads_init(vm);
42194219
rb_vm_living_threads_insert(vm, th);
4220+
vm->fork_gen++;
4221+
42204222
vm->sleeper = 0;
42214223
clear_coverage();
42224224
}

thread_sync.c

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44
static VALUE rb_cMutex, rb_cQueue, rb_cSizedQueue, rb_cConditionVariable;
55
static VALUE rb_eClosedQueueError;
66

7+
/*
8+
* keep these globally so we can walk and reinitialize them at fork
9+
* in the child process
10+
*/
11+
static LIST_HEAD(szqueue_list);
12+
static LIST_HEAD(queue_list);
13+
static LIST_HEAD(condvar_list);
14+
715
/* sync_waiter is always on-stack */
816
struct sync_waiter {
917
rb_thread_t *th;
@@ -556,6 +564,7 @@ void rb_mutex_allow_trap(VALUE self, int val)
556564
#define queue_waitq(q) UNALIGNED_MEMBER_PTR(q, waitq)
557565
PACKED_STRUCT_UNALIGNED(struct rb_queue {
558566
struct list_head waitq;
567+
rb_serial_t fork_gen;
559568
const VALUE que;
560569
int num_waiting;
561570
});
@@ -601,12 +610,29 @@ queue_alloc(VALUE klass)
601610
return obj;
602611
}
603612

613+
static int
614+
queue_fork_check(struct rb_queue *q)
615+
{
616+
rb_serial_t fork_gen = GET_VM()->fork_gen;
617+
618+
if (q->fork_gen == fork_gen) {
619+
return 0;
620+
}
621+
/* forked children can't reach into parent thread stacks */
622+
q->fork_gen = fork_gen;
623+
list_head_init(queue_waitq(q));
624+
q->num_waiting = 0;
625+
return 1;
626+
}
627+
604628
static struct rb_queue *
605629
queue_ptr(VALUE obj)
606630
{
607631
struct rb_queue *q;
608632

609633
TypedData_Get_Struct(obj, struct rb_queue, &queue_data_type, q);
634+
queue_fork_check(q);
635+
610636
return q;
611637
}
612638

@@ -649,6 +675,11 @@ szqueue_ptr(VALUE obj)
649675
struct rb_szqueue *sq;
650676

651677
TypedData_Get_Struct(obj, struct rb_szqueue, &szqueue_data_type, sq);
678+
if (queue_fork_check(&sq->q)) {
679+
list_head_init(szqueue_pushq(sq));
680+
sq->num_waiting_push = 0;
681+
}
682+
652683
return sq;
653684
}
654685

@@ -885,7 +916,7 @@ queue_do_pop(VALUE self, struct rb_queue *q, int should_block)
885916
list_add_tail(&qw.as.q->waitq, &qw.w.node);
886917
qw.as.q->num_waiting++;
887918

888-
rb_ensure(queue_sleep, Qfalse, queue_sleep_done, (VALUE)&qw);
919+
rb_ensure(queue_sleep, self, queue_sleep_done, (VALUE)&qw);
889920
}
890921
}
891922

@@ -1127,7 +1158,7 @@ rb_szqueue_push(int argc, VALUE *argv, VALUE self)
11271158
list_add_tail(pushq, &qw.w.node);
11281159
sq->num_waiting_push++;
11291160

1130-
rb_ensure(queue_sleep, Qfalse, szqueue_sleep_done, (VALUE)&qw);
1161+
rb_ensure(queue_sleep, self, szqueue_sleep_done, (VALUE)&qw);
11311162
}
11321163
}
11331164

@@ -1228,9 +1259,9 @@ rb_szqueue_empty_p(VALUE self)
12281259

12291260

12301261
/* ConditionalVariable */
1231-
/* TODO: maybe this can be IMEMO */
12321262
struct rb_condvar {
12331263
struct list_head waitq;
1264+
rb_serial_t fork_gen;
12341265
};
12351266

12361267
/*
@@ -1277,9 +1308,15 @@ static struct rb_condvar *
12771308
condvar_ptr(VALUE self)
12781309
{
12791310
struct rb_condvar *cv;
1311+
rb_serial_t fork_gen = GET_VM()->fork_gen;
12801312

12811313
TypedData_Get_Struct(self, struct rb_condvar, &cv_data_type, cv);
12821314

1315+
/* forked children can't reach into parent thread stacks */
1316+
if (cv->fork_gen != fork_gen) {
1317+
list_head_init(&cv->waitq);
1318+
}
1319+
12831320
return cv;
12841321
}
12851322

variable.c

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "ccan/list/list.h"
2121
#include "id_table.h"
2222
#include "debug_counter.h"
23+
#include "vm_core.h"
2324

2425
struct rb_id_table *rb_global_tbl;
2526
static ID autoload, classpath, tmp_classpath, classid;
@@ -1859,6 +1860,7 @@ struct autoload_data_i {
18591860
rb_const_flag_t flag;
18601861
VALUE value;
18611862
struct autoload_state *state; /* points to on-stack struct */
1863+
rb_serial_t fork_gen;
18621864
};
18631865

18641866
static void
@@ -1881,8 +1883,18 @@ static const rb_data_type_t autoload_data_i_type = {
18811883
0, 0, RUBY_TYPED_FREE_IMMEDIATELY
18821884
};
18831885

1884-
#define check_autoload_data(av) \
1885-
(struct autoload_data_i *)rb_check_typeddata((av), &autoload_data_i_type)
1886+
static struct autoload_data_i *
1887+
get_autoload_data(VALUE av)
1888+
{
1889+
struct autoload_data_i *ele = rb_check_typeddata(av, &autoload_data_i_type);
1890+
1891+
/* do not reach across stack for ->state after forking: */
1892+
if (ele && ele->state && ele->fork_gen != GET_VM()->fork_gen) {
1893+
ele->state = 0;
1894+
ele->fork_gen = 0;
1895+
}
1896+
return ele;
1897+
}
18861898

18871899
RUBY_FUNC_EXPORTED void
18881900
rb_autoload(VALUE mod, ID id, const char *file)
@@ -1982,7 +1994,7 @@ check_autoload_required(VALUE mod, ID id, const char **loadingpath)
19821994
const char *loading;
19831995
int safe;
19841996

1985-
if (!(load = autoload_data(mod, id)) || !(ele = check_autoload_data(load))) {
1997+
if (!(load = autoload_data(mod, id)) || !(ele = get_autoload_data(load))) {
19861998
return 0;
19871999
}
19882000
file = ele->feature;
@@ -2020,7 +2032,7 @@ rb_autoloading_value(VALUE mod, ID id, VALUE* value, rb_const_flag_t *flag)
20202032
VALUE load;
20212033
struct autoload_data_i *ele;
20222034

2023-
if (!(load = autoload_data(mod, id)) || !(ele = check_autoload_data(load))) {
2035+
if (!(load = autoload_data(mod, id)) || !(ele = get_autoload_data(load))) {
20242036
return 0;
20252037
}
20262038
if (ele->state && ele->state->thread == rb_thread_current()) {
@@ -2087,8 +2099,9 @@ autoload_reset(VALUE arg)
20872099
int need_wakeups = 0;
20882100

20892101
if (state->ele->state == state) {
2090-
need_wakeups = 1;
2091-
state->ele->state = 0;
2102+
need_wakeups = 1;
2103+
state->ele->state = 0;
2104+
state->ele->fork_gen = 0;
20922105
}
20932106

20942107
/* At the last, move a value defined in autoload to constant table */
@@ -2170,7 +2183,7 @@ rb_autoload_load(VALUE mod, ID id)
21702183
if (src && loading && strcmp(src, loading) == 0) return Qfalse;
21712184

21722185
/* set ele->state for a marker of autoloading thread */
2173-
if (!(ele = check_autoload_data(load))) {
2186+
if (!(ele = get_autoload_data(load))) {
21742187
return Qfalse;
21752188
}
21762189

@@ -2180,6 +2193,7 @@ rb_autoload_load(VALUE mod, ID id)
21802193
state.thread = rb_thread_current();
21812194
if (!ele->state) {
21822195
ele->state = &state;
2196+
ele->fork_gen = GET_VM()->fork_gen;
21832197

21842198
/*
21852199
* autoload_reset will wake up any threads added to this
@@ -2217,7 +2231,7 @@ rb_autoload_p(VALUE mod, ID id)
22172231
}
22182232
load = check_autoload_required(mod, id, 0);
22192233
if (!load) return Qnil;
2220-
return (ele = check_autoload_data(load)) ? ele->feature : Qnil;
2234+
return (ele = get_autoload_data(load)) ? ele->feature : Qnil;
22212235
}
22222236

22232237
void
@@ -2646,7 +2660,7 @@ current_autoload_data(VALUE mod, ID id)
26462660
struct autoload_data_i *ele;
26472661
VALUE load = autoload_data(mod, id);
26482662
if (!load) return 0;
2649-
ele = check_autoload_data(load);
2663+
ele = get_autoload_data(load);
26502664
if (!ele) return 0;
26512665
/* for autoloading thread, keep the defined value to autoloading storage */
26522666
if (ele->state && (ele->state->thread == rb_thread_current())) {

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.5.4"
2-
#define RUBY_RELEASE_DATE "2019-01-20"
3-
#define RUBY_PATCHLEVEL 137
2+
#define RUBY_RELEASE_DATE "2019-01-23"
3+
#define RUBY_PATCHLEVEL 138
44

55
#define RUBY_RELEASE_YEAR 2019
66
#define RUBY_RELEASE_MONTH 1
7-
#define RUBY_RELEASE_DAY 20
7+
#define RUBY_RELEASE_DAY 23
88

99
#include "ruby/version.h"
1010

vm_core.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,7 @@ typedef struct rb_vm_struct {
507507
struct rb_thread_struct *main_thread;
508508
struct rb_thread_struct *running_thread;
509509

510+
rb_serial_t fork_gen;
510511
struct list_head waiting_fds; /* <=> struct waiting_fd */
511512
struct list_head living_threads;
512513
size_t living_thread_num;

0 commit comments

Comments
 (0)