Skip to content

Resuming fiber from C seems to loop infinitely #5261

Description

@haileys

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.

No activity

Activity on this issue will appear here.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions