Skip to content

Commit

Permalink
Replacement for mrb_fiber_new() and mrb_fiber_resume()
Browse files Browse the repository at this point in the history
The use of `Fiber#resume` via `mrb_funcall()` is no longer allowed in mruby 3.2.
  • Loading branch information
dearblue committed Dec 15, 2023
1 parent 77683b2 commit e186a1f
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 34 deletions.
10 changes: 0 additions & 10 deletions mrbgems/ngx_mruby_mrblib/mrblib/mrb_nginx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,4 @@ module Kernel
def get_server_class
Nginx
end

def _ngx_mrb_prepare_fiber(nginx_handler)
fiber_handler = Fiber.new { nginx_handler.call }

lambda do
# BUG?: return nginx_handler directly from fiber, not proc in any case.
result = fiber_handler.resume
[fiber_handler.alive?, result]
end
end
end
15 changes: 3 additions & 12 deletions src/http/ngx_http_mruby_async.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ mrb_value ngx_mrb_start_fiber(ngx_http_request_t *r, mrb_state *mrb, struct RPro

handler_proc = mrb_closure_new(mrb, rproc->body.irep);
fiber_proc = (mrb_value *)ngx_palloc(r->pool, sizeof(mrb_value));
*fiber_proc =
mrb_funcall(mrb, mrb_obj_value(mrb->kernel_module), "_ngx_mrb_prepare_fiber", 1, mrb_obj_value(handler_proc));
*fiber_proc = mrb_fiber_new(mrb, rproc);
if (mrb->exc) {
ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0,
"%s NOTICE %s:%d: preparing fiber got the raise, leave the fiber", MODULE_NAME, __func__, __LINE__);
Expand All @@ -60,7 +59,6 @@ mrb_value ngx_mrb_start_fiber(ngx_http_request_t *r, mrb_state *mrb, struct RPro

mrb_value ngx_mrb_run_fiber(mrb_state *mrb, mrb_value *fiber_proc, mrb_value *result)
{
mrb_value resume_result = mrb_nil_value();
ngx_http_request_t *r = ngx_mrb_get_request();
mrb_value aliving = mrb_false_value();
mrb_value handler_result = mrb_nil_value();
Expand All @@ -69,21 +67,14 @@ mrb_value ngx_mrb_run_fiber(mrb_state *mrb, mrb_value *fiber_proc, mrb_value *re
ctx = ngx_mrb_http_get_module_ctx(mrb, r);
ctx->fiber_proc = fiber_proc;

resume_result = mrb_funcall(mrb, *fiber_proc, "call", 0, NULL);
handler_result = mrb_fiber_resume(mrb, *fiber_proc, 0, NULL);
if (mrb->exc) {
ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0, "%s NOTICE %s:%d: fiber got the raise, leave the fiber",
MODULE_NAME, __func__, __LINE__);
return mrb_false_value();
}

if (!mrb_array_p(resume_result)) {
mrb->exc = mrb_obj_ptr(mrb_exc_new_lit(
mrb, E_RUNTIME_ERROR,
"_ngx_mrb_prepare_fiber proc must return array included handler_return and fiber alive status"));
return mrb_false_value();
}
aliving = mrb_ary_entry(resume_result, 0);
handler_result = mrb_ary_entry(resume_result, 1);
aliving = mrb_fiber_alive_p(mrb, *fiber_proc);

if (!mrb_test(aliving) && result != NULL) {
*result = handler_result;
Expand Down
15 changes: 3 additions & 12 deletions src/stream/ngx_stream_mruby_async.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ typedef struct {

static mrb_value ngx_stream_mrb_run_fiber(mrb_state *mrb, mrb_value *fiber_proc, mrb_value *result)
{
mrb_value resume_result = mrb_nil_value();
mrb_value aliving = mrb_false_value();
mrb_value handler_result = mrb_nil_value();
ngx_stream_mruby_ctx_t *ctx;
Expand All @@ -33,21 +32,14 @@ static mrb_value ngx_stream_mrb_run_fiber(mrb_state *mrb, mrb_value *fiber_proc,
ctx = ngx_stream_mrb_get_module_ctx(mrb, ictx->s);
ctx->fiber_proc = fiber_proc;

resume_result = mrb_funcall(mrb, *fiber_proc, "call", 0, NULL);
handler_result = mrb_fiber_resume(mrb, *fiber_proc, 0, NULL);
if (mrb->exc) {
ngx_log_error(NGX_LOG_NOTICE, ictx->s->connection->log, 0, "%s NOTICE %s:%d: fiber got the raise, leave the fiber",
MODULE_NAME, __func__, __LINE__);
return mrb_false_value();
}

if (!mrb_array_p(resume_result)) {
mrb->exc = mrb_obj_ptr(mrb_exc_new_lit(
mrb, E_RUNTIME_ERROR,
"_ngx_mrb_prepare_fiber proc must return array included handler_return and fiber alive status"));
return mrb_false_value();
}
aliving = mrb_ary_entry(resume_result, 0);
handler_result = mrb_ary_entry(resume_result, 1);
aliving = mrb_fiber_alive_p(mrb, *fiber_proc);

if (!mrb_test(aliving) && result != NULL) {
*result = handler_result;
Expand All @@ -67,8 +59,7 @@ mrb_value ngx_stream_mrb_start_fiber(ngx_stream_session_t *s, mrb_state *mrb, st

handler_proc = mrb_closure_new(mrb, rproc->body.irep);
fiber_proc = (mrb_value *)ngx_palloc(s->connection->pool, sizeof(mrb_value));
*fiber_proc =
mrb_funcall(mrb, mrb_obj_value(mrb->kernel_module), "_ngx_mrb_prepare_fiber", 1, mrb_obj_value(handler_proc));
*fiber_proc = mrb_fiber_new(mrb, rproc);
if (mrb->exc) {
ngx_log_error(NGX_LOG_NOTICE, s->connection->log, 0,
"%s NOTICE %s:%d: preparing fiber got the raise, leave the fiber", MODULE_NAME, __func__, __LINE__);
Expand Down

0 comments on commit e186a1f

Please sign in to comment.