Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions mrbgems/mruby-task/test/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,28 @@ def ==(other)
assert_equal [:other, :passed, :returned], order
end

# Resuming a fiber writes RESUMED, then RUNNING, over the CREATED status of
# the task's own context. The end of the task body was told from the end of
# a fiber body by that status, so it went to fiber_terminate(), which freed
# the task's stacks; a task run again on its context (as picoruby-sandbox
# runs each irb line) then stopped without running its new body.
assert("a task that resumed a fiber runs a new body after reset_context") do
skip unless Object.const_defined?(:Fiber)
t = Task.new { Fiber.new { :fiber }.resume }
Task.run
assert_equal :fiber, t.value
TaskTest.rerun(t) { :second }
Task.run
assert_equal :second, t.value
# a fiber left suspended is abandoned by the reset, not resumed
TaskTest.rerun(t) { f = Fiber.new { Fiber.yield 1; 2 }; f.resume }
Task.run
assert_equal 1, t.value
TaskTest.rerun(t) { :fourth }
Task.run
assert_equal :fourth, t.value
end

assert("exception raised from C after blocking is not leaked into Task#value") do
child = nil

Expand Down
20 changes: 20 additions & 0 deletions mrbgems/mruby-task/test/tasktest.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,25 @@ tasktest_proc_set_stack(mrb_state *mrb, mrb_value self)
return mrb_ary_new_from_values(mrb, 2, r);
}

/* Run a finished task again with another body, the way picoruby-sandbox
runs each irb line on one task: suspend it (as Sandbox#suspend does after
each run; resume only wakes a suspended task), reset the context, set the
proc, resume. The caller runs the scheduler for the result. */
static mrb_value
tasktest_rerun(mrb_state *mrb, mrb_value self)
{
mrb_value task, blk;
mrb_get_args(mrb, "o&", &task, &blk);
if (mrb_nil_p(blk)) {
mrb_raise(mrb, E_ARGUMENT_ERROR, "block required");
}
mrb_suspend_task(mrb, task);
mrb_task_reset_context(mrb, task);
mrb_task_proc_set(mrb, task, mrb_proc_ptr(blk));
mrb_resume_task(mrb, task);
return mrb_nil_value();
}

struct extend_probe {
struct mrb_context *c;
mrb_int room;
Expand Down Expand Up @@ -417,6 +436,7 @@ mrb_mruby_task_gem_test(mrb_state* mrb)
mrb_define_module_function(mrb, tasktest, "run_sync", tasktest_run_sync, MRB_ARGS_BLOCK());
mrb_define_module_function(mrb, tasktest, "set_svar", tasktest_set_svar, MRB_ARGS_NONE());
mrb_define_module_function(mrb, tasktest, "proc_set_stack", tasktest_proc_set_stack, MRB_ARGS_REQ(2));
mrb_define_module_function(mrb, tasktest, "rerun", tasktest_rerun, MRB_ARGS_REQ(1) | MRB_ARGS_BLOCK());
mrb_define_module_function(mrb, tasktest, "extend_past_stend", tasktest_extend_past_stend, MRB_ARGS_REQ(3));
mrb_define_module_function(mrb, tasktest, "proc_set_deep", tasktest_proc_set_deep, MRB_ARGS_REQ(2));
#ifdef MRB_USE_TASK_TRACE
Expand Down
9 changes: 8 additions & 1 deletion src/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -4428,7 +4428,14 @@ mrb_vm_exec(mrb_state *mrb, const struct RProc *begin_proc, const mrb_code *iseq
}

#ifdef MRB_USE_TASK_SCHEDULER
if (mrb->c->status == MRB_TASK_CREATED) {
/* A task's own context ends here, a fiber's goes on below. Tell
them by owner, not by status: a task that resumed a fiber has
RESUMED and then RUNNING written over its CREATED by
fiber_switch(), and fiber_terminate() would then free the stacks
of a context the task is reset and run on again (Sandbox). A
fiber's context always has its RFiber in c->fib; a task's has
none, or the one Fiber.current borrowed for it. */
if (!c->fib || MRB_FLAG_TEST(c->fib, MRB_FL_FIBER_BORROWED)) {
mrb_gc_arena_restore(mrb, ai);
mrb->jmp = prev_jmp;
TASK_STOP(mrb);
Expand Down
Loading