Skip to content

Commit

Permalink
Merge pull request #520 from kyswtnb/improve-fiber-gc-registration
Browse files Browse the repository at this point in the history
Improve fiber gc registration
  • Loading branch information
matsumotory authored Feb 26, 2024
2 parents af42288 + caddfe6 commit 3badaa1
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/http/ngx_http_mruby_async.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ mrb_value ngx_mrb_start_fiber(ngx_http_request_t *r, mrb_state *mrb, struct RPro
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__);
return mrb_false_value();
} else {
// keeps the object from GC when can resume the fiber
// Don't forget to remove the object using
// mrb_gc_unregister, otherwise your object will leak
mrb_gc_register(mrb, *fiber_proc);
}

return ngx_mrb_run_fiber(mrb, fiber_proc, result);
Expand All @@ -73,12 +78,14 @@ mrb_value ngx_mrb_run_fiber(mrb_state *mrb, mrb_value *fiber_proc, mrb_value *re
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__);
mrb_gc_unregister(mrb, *fiber_proc);
return mrb_false_value();
}

aliving = mrb_fiber_alive_p(mrb, *fiber_proc);

if (!mrb_test(aliving) && result != NULL) {
mrb_gc_unregister(mrb, *fiber_proc);
*result = handler_result;
}

Expand Down Expand Up @@ -198,11 +205,6 @@ static mrb_value ngx_mrb_async_sleep(mrb_state *mrb, mrb_value self)
ctx = ngx_mrb_http_get_module_ctx(mrb, r);
re->fiber = ctx->fiber_proc;

// keeps the object from GC when can resume the fiber
// Don't forget to remove the object using
// mrb_gc_unregister, otherwise your object will leak
mrb_gc_register(mrb, *re->fiber);

ev = (ngx_event_t *)p;
ngx_memzero(ev, sizeof(ngx_event_t));
ev->handler = ngx_mrb_timer_handler;
Expand Down

0 comments on commit 3badaa1

Please sign in to comment.