Skip to content

vm.c: tell the end of a task body from a fiber's by owner, not status - #7845

Merged
matz merged 1 commit into
mruby:masterfrom
hasumikin:task-end-by-owner
Nov 20, 2026
Merged

matz merged 1 commit into
mruby:masterfrom
hasumikin:task-end-by-owner

Conversation

@hasumikin

@hasumikin hasumikin commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Background

At the return from the base frame, the VM decided that a task body had ended by mrb->c->status == MRB_TASK_CREATED, and sent everything else to fiber_terminate(). A task's own context keeps CREATED only until it resumes a fiber: fiber_switch() writes RESUMED over it, and RUNNING when the fiber returns. So the body of a task that had resumed any fiber ended in fiber_terminate(), which freed the task's call info, stack and svars. The task still stopped with the same status value, so a task used once showed nothing.

A task run again on its context does show it. picoruby-sandbox runs each irb line on one task with mrb_task_reset_context() and mrb_task_proc_set(). After a line that resumed a fiber (an Enumerator#next is enough), the context had no call info, so the scheduler retired the task at once: no later line ran, and every one answered the value of the line with the fiber.

Fix

Tell the two by owner: a fiber's context always holds its RFiber in c->fib, and a task's own context holds none, or the one Fiber.current borrowed for it. This is the test mrb_task_across_c_boundary() already uses for the same question.

TaskTest.rerun drives the sandbox sequence (suspend, reset_context, proc_set, resume), and a test runs a task again after it resumed a fiber, and after it left one suspended.

Summary by CodeRabbit

  • Bug Fixes
    • Resetting a task after it resumes a completed fiber now allows it to run a replacement body and return the new result.
    • Resetting a task with a suspended fiber now abandons that fiber, allowing the task to run again successfully.
  • Tests
    • Added regression coverage for task resets after completed and suspended fiber execution.

## Background

At the return from the base frame, the VM decided that a task body had
ended by `mrb->c->status == MRB_TASK_CREATED`, and sent everything else
to fiber_terminate(). A task's own context keeps CREATED only until it
resumes a fiber: fiber_switch() writes RESUMED over it, and RUNNING when
the fiber returns. So the body of a task that had resumed any fiber
ended in fiber_terminate(), which freed the task's call info, stack and
svars. The task still stopped with the same status value, so a task
used once showed nothing.

A task run again on its context does show it. picoruby-sandbox runs
each irb line on one task with mrb_task_reset_context() and
mrb_task_proc_set(). After a line that resumed a fiber (an
Enumerator#next is enough), the context had no call info, so the
scheduler retired the task at once: no later line ran, and every one
answered the value of the line with the fiber.

## Fix

Tell the two by owner: a fiber's context always holds its RFiber in
c->fib, and a task's own context holds none, or the one Fiber.current
borrowed for it. This is the test mrb_task_across_c_boundary() already
uses for the same question.

TaskTest.rerun drives the sandbox sequence (suspend, reset_context,
proc_set, resume), and a test runs a task again after it resumed a
fiber, and after it left one suspended.
@coderabbitai

coderabbitai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The VM now identifies task-owned contexts by fiber ownership when handling scheduler returns. A new test helper resets a task and installs a replacement block. Regression tests check task results after rerunning tasks that resumed or left a fiber suspended.

Changes: Task rerun

Layer / File(s) Summary
Task scheduler return handling
src/vm.c
The scheduler now selects task-return handling when the context has no fiber or has a borrowed fiber, instead of checking for MRB_TASK_CREATED. Other contexts continue to fiber_terminate.
Reset and rerun regression coverage
mrbgems/mruby-task/test/tasktest.c, mrbgems/mruby-task/test/task.rb
TaskTest.rerun suspends a task, resets its context, installs a required replacement block, and resumes it. The regression test checks results after a completed fiber is resumed and after a fiber remains suspended.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Possibly related PRs

  • mruby/mruby#7747: Adds a fiber-versus-task context distinction that is also relevant to the scheduler’s context-ownership check.

Suggested labels: core, mrbgems

Suggested reviewers: amatsuda

Merge Risk: Minimal · up to 60f27

This change fixes task-body completion detection so a task that resumed a fiber can be rerun and run its later lines. No concrete merge-blocking risk was found, and regression tests cover the scenario.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the actual change: the VM's end-of-task-body detection in src/vm.c now uses context ownership (c->fib) instead of the task status. The changeset matches this. The title is short, speci…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the actual change: the VM's end-of-task-body detection in src/vm.c now uses context ownership (c->fib) instead of the task status. The changeset matches this. The title is short, specific, and says what changed and why. It is not vague or unrelated.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 4002b4d9-8143-4223-b3e9-38d7e99b80e8
Commits

Reviewing files that changed from the base of the PR and between fc55b04 and 60f27af.

Files selected for processing: 3

  • mrbgems/mruby-task/test/task.rb
  • mrbgems/mruby-task/test/tasktest.c
  • src/vm.c

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

@matz
matz merged commit 8587881 into mruby:master Nov 20, 2026
23 checks passed
@hasumikin
hasumikin deleted the task-end-by-owner branch November 20, 2026 06:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants