summaryrefslogtreecommitdiff
path: root/vm.c
diff options
context:
space:
mode:
authorSamuel Williams <samuel.williams@oriontransfer.co.nz>2021-07-28 19:55:55 +1200
committerSamuel Williams <samuel.williams@oriontransfer.co.nz>2021-08-03 22:23:48 +1200
commit2d4f29e77e883c29e35417799f8001b8046cde03 (patch)
treec2201611275519a9f36b7d3d3ce79583bc7ba14f /vm.c
parent785c70e764c3222f9accac2555246d3921a7263d (diff)
Fix potential hang when joining threads.
If the thread termination invokes user code after `th->status` becomes `THREAD_KILLED`, and the user unblock function causes that `th->status` to become something else (e.g. `THREAD_RUNNING`), threads waiting in `thread_join_sleep` will hang forever. We move the unblock function call to before the thread status is updated, and allow threads to join as soon as `th->value` becomes defined. This reverts commit 6505c77501f1924571b2fe620c5c7b31ede0cd22.
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/4689
Diffstat (limited to 'vm.c')
-rw-r--r--vm.c17
1 files changed, 10 insertions, 7 deletions
diff --git a/vm.c b/vm.c
index b743bbf413..307c5952f4 100644
--- a/vm.c
+++ b/vm.c
@@ -3075,6 +3075,8 @@ th_init(rb_thread_t *th, VALUE self)
th->thread_id_string[0] = '\0';
#endif
+ th->value = Qundef;
+
#if OPT_CALL_THREADED_CODE
th->retval = Qundef;
#endif
@@ -3087,16 +3089,17 @@ static VALUE
ruby_thread_init(VALUE self)
{
rb_thread_t *th = GET_THREAD();
- rb_thread_t *targe_th = rb_thread_ptr(self);
+ rb_thread_t *target_th = rb_thread_ptr(self);
rb_vm_t *vm = th->vm;
- targe_th->vm = vm;
- th_init(targe_th, self);
+ target_th->vm = vm;
+ th_init(target_th, self);
+
+ target_th->top_wrapper = 0;
+ target_th->top_self = rb_vm_top_self();
+ target_th->ec->root_svar = Qfalse;
+ target_th->ractor = th->ractor;
- targe_th->top_wrapper = 0;
- targe_th->top_self = rb_vm_top_self();
- targe_th->ec->root_svar = Qfalse;
- targe_th->ractor = th->ractor;
return self;
}