From ee610cdbb6fdaf54803b075285804541ec233dff Mon Sep 17 00:00:00 2001 From: HASUMI Hitoshi Date: Fri, 13 Feb 2026 13:59:48 +0900 Subject: [PATCH] Set initial task receiver to top_self for stability The current implementation of `task_init_context` inheriting a receiver from the parent task is unstable and causes critical faults, especially on microcontrollers. - It leads to a HardFault on devices like Raspberry Pi Pico 2 by accessing a potentially NULL `mrb->c->ci`. - Even when `mrb->c->ci` is not NULL, this incomplete context copy causes other memory errors (SEGV). This patch reverts to the safer, previous behavior, that I implemented in picoruby/picoruby, of always initializing a new task's receiver to `top_self`, ensuring predictable and robust operation. The issue was likely masked on POSIX systems due to the unpredictable nature of undefined behavior. --- mrbgems/mruby-task/src/task.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mrbgems/mruby-task/src/task.c b/mrbgems/mruby-task/src/task.c index 3ca692487..01cd0d4c0 100644 --- a/mrbgems/mruby-task/src/task.c +++ b/mrbgems/mruby-task/src/task.c @@ -278,8 +278,8 @@ task_init_context(mrb_state *mrb, mrb_task *t, const struct RProc *proc) } } - /* Copy receiver from current context */ - c->stbase[0] = mrb->c->ci->stack[0]; + /* Set receiver to top self */ + c->stbase[0] = mrb_top_self(mrb); /* Initialize callinfo stack */ static const mrb_callinfo ci_zero = { 0 };