Skip to content

Commit

Permalink
Merge pull request #6230 from dearblue/fiber-err-switch
Browse files Browse the repository at this point in the history
Fix status of fiber after switched by exception raised
  • Loading branch information
matz committed Apr 8, 2024
2 parents f3f7df0 + 4956584 commit 6eeb067
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,20 @@ cipush(mrb_state *mrb, mrb_int push_stacks, uint8_t cci, struct RClass *target_c
return ci;
}

static void
fiber_terminate(mrb_state *mrb, struct mrb_context *c, mrb_callinfo *ci)
{
mrb_assert(c != mrb->root_c);

c->status = MRB_FIBER_TERMINATED;

/* fiber termination should automatic yield or transfer to root */
mrb->c = c->prev;
if (!mrb->c) mrb->c = mrb->root_c;
else c->prev = NULL;
mrb->c->status = MRB_FIBER_RUNNING;
}

mrb_bool
mrb_env_unshare(mrb_state *mrb, struct REnv *e, mrb_bool noraise)
{
Expand Down Expand Up @@ -1777,10 +1791,7 @@ mrb_vm_exec(mrb_state *mrb, const struct RProc *proc, const mrb_code *pc)
else {
struct mrb_context *c = mrb->c;

c->status = MRB_FIBER_TERMINATED;
mrb->c = c->prev;
if (!mrb->c) mrb->c = mrb->root_c;
else c->prev = NULL;
fiber_terminate(mrb, c, ci);
if (!c->vmexec) goto L_RAISE;
mrb->jmp = prev_jmp;
if (!prev_jmp) return mrb_obj_value(mrb->exc);
Expand Down Expand Up @@ -2377,11 +2388,7 @@ mrb_vm_exec(mrb_state *mrb, const struct RProc *proc, const mrb_code *pc)
return v;
}

/* fiber termination should automatic yield or transfer to root */
c->status = MRB_FIBER_TERMINATED;
mrb->c = c->prev ? c->prev : mrb->root_c;
c->prev = NULL;
mrb->c->status = MRB_FIBER_RUNNING;
fiber_terminate(mrb, c, ci);
if (c->vmexec ||
(mrb->c == mrb->root_c && mrb->c->ci == mrb->c->cibase) /* case using Fiber#transfer in mrb_fiber_resume() */) {
mrb_gc_arena_restore(mrb, ai);
Expand Down

0 comments on commit 6eeb067

Please sign in to comment.