Skip to content
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

Closed
haileys opened this issue Jan 7, 2021 · 1 comment
Closed

Resuming fiber from C seems to loop infinitely #5261

haileys opened this issue Jan 7, 2021 · 1 comment

Comments

@haileys
Copy link

haileys commented Jan 7, 2021

This example code behaves incorrectly:

#include <mruby.h>
#include <mruby/compile.h>
#include <mruby/string.h>
#include <stdio.h>

mrb_state* mrb;

void p(mrb_value value) {
    printf("--> %s\n", mrb_str_to_cstr(mrb, mrb_inspect(mrb, value)));
}

int main() {
    mrb = mrb_open();

    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); end");

    mrb_sym new = mrb_intern_cstr(mrb, "new");
    mrb_value fiber = mrb_funcall_with_block(mrb, fiber_class, new, 0, NULL, proc);

    mrb_value start_arg = mrb_fixnum_value(123);
    p(mrb_fiber_resume(mrb, fiber, 1, &start_arg));

    p(mrb_fiber_resume(mrb, fiber, 0, NULL));
    p(mrb_fiber_resume(mrb, fiber, 0, NULL));
    p(mrb_fiber_resume(mrb, fiber, 0, NULL));
    p(mrb_fiber_resume(mrb, fiber, 0, NULL));
    p(mrb_fiber_resume(mrb, fiber, 0, NULL));
    p(mrb_fiber_resume(mrb, fiber, 0, NULL));
    p(mrb_fiber_resume(mrb, fiber, 0, NULL));
    p(mrb_fiber_resume(mrb, fiber, 0, NULL));
}

I would expect this code to output:

--> 124
--> nil
resuming dead fiber (FiberError)
Aborted (core dumped)

Instead it outputs:

--> 124
--> 125
--> 126
--> 127
--> 128
--> 129
--> 130
--> 131
--> 132

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.

@haileys
Copy link
Author

haileys commented Jan 8, 2021

I have been able to come up with a workaround involving a trampoline proc that calls Fiber#resume from mruby code rather than calling mrb_fiber_resume directly:

#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:

--> 124
--> 125
--> nil
--> resuming dead fiber (FiberError)
--> resuming dead fiber (FiberError)
--> resuming dead fiber (FiberError)
--> resuming dead fiber (FiberError)
--> resuming dead fiber (FiberError)
--> resuming dead fiber (FiberError)

This seems to suggest that mrb_fiber_resume does not work correctly when the fiber it is called from (in this case, the root fiber) is not in a VM frame.

@matz matz closed this as completed in a0c1e07 Jan 8, 2021
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
matz pushed a commit that referenced this issue Jan 12, 2021
…5261"

This reverts commit a0c1e07.

This is because the `mrb_callinfo::pc` has been reorganized, resulting in over-correction.
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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant