Skip to content

Commit

Permalink
Use ruby_thread_set_pthread_native at boot
Browse files Browse the repository at this point in the history
At boot, `th->ractor->threads.running_ec == th->ec` is true, so calling
ruby_thread_set_native will cause an assertion error.

```
Assertion Failed: ractor_core.h:328:rb_ractor_set_current_ec_:ec == ((void*)0) || cr->threads.running_ec != ec

-- C level backtrace information -------------------------------------------
miniruby(rb_vm_bugreport+0xb60) [0x100290f20] vm_dump.c:1151
miniruby(rb_vm_bugreport) (null):0
miniruby(rb_assert_failure_detail+0xa4) [0x1003b533c] error.c:1168
miniruby(rb_assert_failure_detail+0x0) [0x1003b5298] error.c:1148
miniruby(rb_assert_failure) (null):0
miniruby(Init_native_thread.cold.1+0x0) [0x1003d3c24] ractor_core.h:328
miniruby(rb_ractor_set_current_ec_) (null):0
miniruby(ruby_thread_set_native.cold.1) thread_pthread.c:1624
miniruby(Init_native_thread+0x0) [0x100222380] ractor_core.h:328
miniruby(ruby_thread_set_native) (null):0
miniruby(fiber_entry+0x1c) [0x10008a1c4] cont.c:845
miniruby(coroutine_trampoline+0xa8) [0x1003a1d78] coroutine/pthread/Context.c:148
/usr/lib/system/libsystem_pthread.dylib(_pthread_start+0x88) [0x1911fef94]
```
  • Loading branch information
peterzhu2118 committed Apr 9, 2024
1 parent 0bc7182 commit a0735c4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
2 changes: 1 addition & 1 deletion cont.c
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,7 @@ fiber_entry(struct coroutine_context * from, struct coroutine_context * to)
rb_thread_t *thread = fiber->cont.saved_ec.thread_ptr;

#ifdef COROUTINE_PTHREAD_CONTEXT
ruby_thread_set_native(thread);
ruby_thread_set_pthread_native(thread);
#endif

fiber_restore_thread(thread, fiber);
Expand Down
21 changes: 14 additions & 7 deletions thread_pthread.c
Original file line number Diff line number Diff line change
Expand Up @@ -1607,20 +1607,17 @@ ruby_thread_from_native(void)
#endif
}

int
ruby_thread_set_native(rb_thread_t *th)
bool
ruby_thread_set_pthread_native(rb_thread_t *th)
{
RUBY_ASSERT(th->ractor->threads.running_ec == th->ec);

if (th) {
#ifdef USE_UBF_LIST
ccan_list_node_init(&th->sched.node.ubf);
#endif
}

// setup TLS

if (th && th->ec) {
rb_ractor_set_current_ec(th->ractor, th->ec);
}
#ifdef RB_THREAD_LOCAL_SPECIFIER
ruby_native_thread = th;
return 1;
Expand All @@ -1629,6 +1626,16 @@ ruby_thread_set_native(rb_thread_t *th)
#endif
}

int
ruby_thread_set_native(rb_thread_t *th)
{
if (th && th->ec) {
rb_ractor_set_current_ec(th->ractor, th->ec);
}

return ruby_thread_set_pthread_native(th);
}

static void native_thread_setup(struct rb_native_thread *nt);
static void native_thread_setup_on_thread(struct rb_native_thread *nt);

Expand Down
2 changes: 2 additions & 0 deletions thread_pthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,6 @@ native_tls_set(native_tls_key_t key, void *ptr)
RUBY_EXTERN native_tls_key_t ruby_current_ec_key;
#endif

bool ruby_thread_set_pthread_native(struct rb_thread_struct *th);

#endif /* RUBY_THREAD_PTHREAD_H */

0 comments on commit a0735c4

Please sign in to comment.