New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Resuming fiber from C seems to loop infinitely #5261
Comments
I have been able to come up with a workaround involving a trampoline proc that calls #include <mruby.h>
#include <mruby/compile.h>
#include <mruby/string.h>
#include <stdio.h>
#include <string.h>
mrb_state* mrb;
mrb_value trampoline;
void p(mrb_value value) {
printf("--> %s\n", mrb_str_to_cstr(mrb, mrb_inspect(mrb, value)));
}
mrb_value fiber_resume(mrb_value fiber, size_t argc, mrb_value* argv) {
mrb_value args[argc + 1];
args[0] = fiber;
memcpy(args + 1, argv, argc * sizeof(mrb_value));
return mrb_funcall_argv(mrb, trampoline, mrb_intern_lit(mrb, "call"), argc + 1, args);
}
int main() {
mrb = mrb_open();
trampoline = mrb_load_string(mrb,
"proc do |fiber, *args| fiber.resume(*args) end");
mrb_gc_protect(mrb, trampoline);
mrb_value fiber_class = mrb_obj_value(mrb_class_get(mrb, "Fiber"));
mrb_value proc = mrb_load_string(mrb,
"proc do |a| Fiber.yield(a + 1); Fiber.yield(a + 2); end");
mrb_gc_protect(mrb, proc);
mrb_sym new = mrb_intern_cstr(mrb, "new");
mrb_value fiber = mrb_funcall_with_block(mrb, fiber_class, new, 0, NULL, proc);
mrb_gc_protect(mrb, fiber);
mrb_value start_arg = mrb_fixnum_value(123);
p(fiber_resume(fiber, 1, &start_arg));
p(fiber_resume(fiber, 0, NULL));
p(fiber_resume(fiber, 0, NULL));
p(fiber_resume(fiber, 0, NULL));
p(fiber_resume(fiber, 0, NULL));
p(fiber_resume(fiber, 0, NULL));
p(fiber_resume(fiber, 0, NULL));
p(fiber_resume(fiber, 0, NULL));
p(fiber_resume(fiber, 0, NULL));
} This correctly outputs:
This seems to suggest that |
dearblue
added a commit
to dearblue/mruby
that referenced
this issue
Jan 10, 2021
dearblue
added a commit
to dearblue/mruby
that referenced
this issue
Jan 11, 2021
dearblue
added a commit
to dearblue/mruby
that referenced
this issue
Jan 12, 2021
matz
added a commit
that referenced
this issue
Jan 14, 2021
Capture the return value of `Fiber.yield` via C; ref #5261
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This example code behaves incorrectly:
I would expect this code to output:
Instead it outputs:
I think we are not correctly updating
ci->pc
when the fiber yields but I am not familiar enough with the VM to understand what should be happening.The text was updated successfully, but these errors were encountered: