Skip to content

Commit ff96f9d

Browse files
ko1claude
andcommitted
thread: build a new Ractor's interrupt queue on its own main thread
Defer creating a Ractor main thread's pending-interrupt queue and mask stack from thread_create_core (the creating thread) to thread_start_func_2 (the new Ractor's own main thread). The mask stack starts empty rather than duplicating the creating thread's, so a new Ractor does not inherit its Thread.handle_interrupt state. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent f5ef758 commit ff96f9d

1 file changed

Lines changed: 20 additions & 4 deletions

File tree

thread.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,13 @@ thread_start_func_2(rb_thread_t *th, VALUE *stack_start)
680680
r->r_stdin = rb_io_prep_stdin();
681681
r->r_stdout = rb_io_prep_stdout();
682682
r->r_stderr = rb_io_prep_stderr();
683+
684+
/* Build the interrupt queue and mask stack here, on the new Ractor's
685+
* own main thread, instead of carrying over the ones the creating
686+
* thread made. The mask stack starts empty so a new Ractor does not
687+
* inherit the creating thread's Thread.handle_interrupt state. */
688+
th->pending_interrupt_queue = rb_ary_hidden_new(0);
689+
th->pending_interrupt_mask_stack = rb_ary_hidden_new(0);
683690
}
684691
RB_VM_UNLOCK();
685692
}
@@ -887,10 +894,19 @@ thread_create_core(VALUE thval, struct thread_create_params *params)
887894
th->priority = current_th->priority;
888895
th->thgroup = current_th->thgroup;
889896

890-
th->pending_interrupt_queue = rb_ary_hidden_new(0);
891-
th->pending_interrupt_queue_checked = 0;
892-
th->pending_interrupt_mask_stack = rb_ary_dup(current_th->pending_interrupt_mask_stack);
893-
RBASIC_CLEAR_CLASS(th->pending_interrupt_mask_stack);
897+
if (th->invoke_type == thread_invoke_type_ractor_proc) {
898+
/* A new Ractor's main thread builds these on start
899+
* (thread_start_func_2); leave them unset until then. */
900+
th->pending_interrupt_queue = 0;
901+
th->pending_interrupt_mask_stack = 0;
902+
th->pending_interrupt_queue_checked = 0;
903+
}
904+
else {
905+
th->pending_interrupt_queue = rb_ary_hidden_new(0);
906+
th->pending_interrupt_queue_checked = 0;
907+
th->pending_interrupt_mask_stack = rb_ary_dup(current_th->pending_interrupt_mask_stack);
908+
RBASIC_CLEAR_CLASS(th->pending_interrupt_mask_stack);
909+
}
894910

895911
rb_native_mutex_initialize(&th->interrupt_lock);
896912

0 commit comments

Comments
 (0)