Skip to content

Commit efe5d8d

Browse files
committed
merge revision(s) 43148,43149,43152: [Backport ruby#8433]
* thread.c (terminate_atfork_i): fix locking mutexes not unlocked in forks when not tracked in thread. [ruby-core:55102] [Bug ruby#8433] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_9_3@45026 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent 4c58aa8 commit efe5d8d

File tree

4 files changed

+61
-9
lines changed

4 files changed

+61
-9
lines changed

ChangeLog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
Mon Feb 17 18:04:40 2014 Aaron Pfeifer <aaron.pfeifer@gmail.com>
2+
3+
* thread.c (terminate_atfork_i): fix locking mutexes not unlocked in
4+
forks when not tracked in thread. [ruby-core:55102] [Bug #8433]
5+
16
Fri Feb 14 21:01:12 2014 NAKAMURA Usaku <usa@ruby-lang.org>
27

38
* ext/socket: revert r44943 because it causes errors on some linux

test/ruby/test_thread.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,4 +710,31 @@ def test_thread_timer_and_interrupt
710710
end
711711
assert_in_delta(t1 - t0, 1, 1)
712712
end
713+
714+
def test_blocking_mutex_unlocked_on_fork
715+
bug8433 = '[ruby-core:55102] [Bug #8433]'
716+
717+
mutex = Mutex.new
718+
flag = false
719+
mutex.lock
720+
721+
th = Thread.new do
722+
mutex.synchronize do
723+
flag = true
724+
sleep
725+
end
726+
end
727+
728+
Thread.pass until th.stop?
729+
mutex.unlock
730+
731+
pid = Process.fork do
732+
exit(mutex.locked?)
733+
end
734+
735+
th.kill
736+
737+
pid, status = Process.waitpid2(pid)
738+
assert_equal(false, status.success?, bug8433)
739+
end if Process.respond_to?(:fork)
713740
end

thread.c

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,8 @@ typedef struct rb_mutex_struct
345345
} rb_mutex_t;
346346

347347
static void rb_mutex_abandon_all(rb_mutex_t *mutexes);
348+
static void rb_mutex_abandon_keeping_mutexes(rb_thread_t *th);
349+
static void rb_mutex_abandon_locking_mutex(rb_thread_t *th);
348350
static const char* rb_mutex_unlock_th(rb_mutex_t *mutex, rb_thread_t volatile *th);
349351

350352
void
@@ -3109,10 +3111,8 @@ terminate_atfork_i(st_data_t key, st_data_t val, st_data_t current_th)
31093111
GetThreadPtr(thval, th);
31103112

31113113
if (th != (rb_thread_t *)current_th) {
3112-
if (th->keeping_mutexes) {
3113-
rb_mutex_abandon_all(th->keeping_mutexes);
3114-
}
3115-
th->keeping_mutexes = NULL;
3114+
rb_mutex_abandon_keeping_mutexes(th);
3115+
rb_mutex_abandon_locking_mutex(th);
31163116
thread_cleanup_func(th, TRUE);
31173117
}
31183118
return ST_CONTINUE;
@@ -3370,8 +3370,6 @@ thgroup_add(VALUE group, VALUE thread)
33703370
#define GetMutexPtr(obj, tobj) \
33713371
TypedData_Get_Struct((obj), rb_mutex_t, &mutex_data_type, (tobj))
33723372

3373-
static const char *rb_mutex_unlock_th(rb_mutex_t *mutex, rb_thread_t volatile *th);
3374-
33753373
#define mutex_mark NULL
33763374

33773375
static void
@@ -3687,6 +3685,28 @@ rb_mutex_unlock(VALUE self)
36873685
return self;
36883686
}
36893687

3688+
static void
3689+
rb_mutex_abandon_keeping_mutexes(rb_thread_t *th)
3690+
{
3691+
if (th->keeping_mutexes) {
3692+
rb_mutex_abandon_all(th->keeping_mutexes);
3693+
}
3694+
th->keeping_mutexes = NULL;
3695+
}
3696+
3697+
static void
3698+
rb_mutex_abandon_locking_mutex(rb_thread_t *th)
3699+
{
3700+
rb_mutex_t *mutex;
3701+
3702+
if (!th->locking_mutex) return;
3703+
3704+
GetMutexPtr(th->locking_mutex, mutex);
3705+
if (mutex->th == th)
3706+
rb_mutex_abandon_all(mutex);
3707+
th->locking_mutex = Qfalse;
3708+
}
3709+
36903710
static void
36913711
rb_mutex_abandon_all(rb_mutex_t *mutexes)
36923712
{

version.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#define RUBY_VERSION "1.9.3"
2-
#define RUBY_PATCHLEVEL 534
2+
#define RUBY_PATCHLEVEL 535
33

4-
#define RUBY_RELEASE_DATE "2014-02-14"
4+
#define RUBY_RELEASE_DATE "2014-02-17"
55
#define RUBY_RELEASE_YEAR 2014
66
#define RUBY_RELEASE_MONTH 2
7-
#define RUBY_RELEASE_DAY 14
7+
#define RUBY_RELEASE_DAY 17
88

99
#include "ruby/version.h"
1010

0 commit comments

Comments
 (0)