Skip to content

Commit 1b171f6

Browse files
committed
Run the GC when a Ractor terminates
1 parent 8828883 commit 1b171f6

4 files changed

Lines changed: 21 additions & 6 deletions

File tree

gc.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11166,6 +11166,17 @@ rb_gc_start(void)
1116611166
return Qnil;
1116711167
}
1116811168

11169+
VALUE
11170+
rb_gc_ractor_teardown_cleanup()
11171+
{
11172+
rb_ractor_t *cr = GET_RACTOR();
11173+
cr->during_teardown_cleanup = true;
11174+
rb_gc();
11175+
cr->during_teardown_cleanup = false;
11176+
gc_finalize_deferred(cr->local_objspace);
11177+
return Qnil;
11178+
}
11179+
1116911180
void
1117011181
rb_gc(void)
1117111182
{

gc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ int ruby_get_stack_grow_direction(volatile VALUE *addr);
114114
const char *rb_obj_info(VALUE obj);
115115
const char *rb_raw_obj_info(char *const buff, const size_t buff_size, VALUE obj);
116116

117+
VALUE rb_gc_ractor_teardown_cleanup();
118+
117119
VALUE rb_gc_disable_no_rest(void);
118120

119121
struct rb_thread_struct;

ractor.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,9 @@ rb_ractor_related_objects_mark(rb_ractor_t *r)
203203
ccan_list_for_each(&r->threads.set, th, lt_node) {
204204
VM_ASSERT(th != NULL);
205205
rb_gc_mark(th->self);
206-
rb_thread_fiber_mark(th);
206+
if (!r->during_teardown_cleanup) {
207+
rb_thread_fiber_mark(th);
208+
}
207209
}
208210
}
209211

@@ -931,11 +933,6 @@ ractor_basket_setup(rb_execution_context_t *ec, struct rb_ractor_basket *basket,
931933
basket->sender = rb_ec_ractor_ptr(ec)->pub.self;
932934
basket->exception = exc;
933935

934-
void rb_add_to_exemption_tbl(VALUE obj);
935-
if (!RTEST(move)) {
936-
rb_add_to_exemption_tbl(obj);
937-
}
938-
939936
if (is_will) {
940937
basket->type = basket_type_will;
941938
basket->v = obj;
@@ -958,6 +955,8 @@ ractor_basket_setup(rb_execution_context_t *ec, struct rb_ractor_basket *basket,
958955
basket->v = ractor_move(obj);
959956
}
960957
}
958+
void rb_add_to_exemption_tbl(VALUE obj);
959+
rb_add_to_exemption_tbl(basket->v);
961960
}
962961

963962
static VALUE
@@ -1591,6 +1590,7 @@ ractor_init(rb_ractor_t *r, VALUE name, VALUE loc)
15911590
}
15921591
r->name = name;
15931592
r->loc = loc;
1593+
r->during_teardown_cleanup = false;
15941594
}
15951595

15961596
void
@@ -1681,6 +1681,7 @@ rb_ractor_teardown(rb_execution_context_t *ec)
16811681
cr->threads.main = NULL;
16821682
}
16831683
RB_VM_LOCK_LEAVE();
1684+
rb_gc_ractor_teardown_cleanup();
16841685
}
16851686

16861687
void

ractor_core.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ struct rb_ractor_struct {
110110
struct rb_ractor_sync sync;
111111
VALUE receiving_mutex;
112112
bool yield_atexit;
113+
bool during_teardown_cleanup;
113114

114115
// vm wide barrier synchronization
115116
rb_nativethread_cond_t barrier_wait_cond;

0 commit comments

Comments
 (0)