Repository navigation
Closed
Description
mruby version:3.2.0
Hello. I am developing with ngx_mruby and I have encountered a SEGV issue related to Fiber and would like to consult with you about it.
mrb_value fiber_class = mrb_obj_value(mrb_class_get(mrb, "Fiber"));
handler_proc = mrb_obj_value(mrb_closure_new(mrb, rproc->body.irep));
if (mrb->exc) {
...
}
fiber_proc = (mrb_value *)ngx_palloc(r->pool, sizeof(mrb_value));
*fiber_proc = mrb_funcall_with_block(mrb, fiber_class, mrb_intern_cstr(mrb, "new"), 0, NULL, handler_proc);
...
handler_result = mrb_fiber_resume(mrb, *fiber_proc, 0, NULL);A SEGV occurs when performing operations such as redirects in Nginx, moving back and forth between the Nginx context and the mruby context. The error appears at the following location.
Program received signal SIGSEGV, Segmentation fault.
0x00005555557a1ae3 in fiber_switch (mrb=0x555555ad01c0, self=..., len=0, a=0x0, resume=true, vmexec=true) at /home/pyama/src/github.com/matsumotory/ngx_mruby/mruby/mrbgems/mruby-fiber/src/fiber.c:260
260 value = c->stbase[0] = MRB_PROC_ENV(c->cibase->proc)->stack[0];
- Here is the state during the first execution. At this point, c->cibase->proc->flags equals 1536.
com/matsumotory/ngx_mruby/src/http/ngx_http_mruby_module.c:841
841 if (mrb_test(ngx_mrb_start_fiber(r, state->mrb, code->proc, mrb_result))) {
(gdb) p *code
$40 = {code = {
file = 0x555555b9bb20 "Nginx::Request.new.headers_out[\"Server\"] = \"global_ngx_mruby\"; Nginx.return Nginx::DECLINED",
string = 0x555555b9bb20 "Nginx::Request.new.headers_out[\"Server\"] = \"global_ngx_mruby\"; Nginx.return Nginx::DECLINED"},
code_type = NGX_MRB_CODE_TYPE_STRING, n = 0, cache = 1, proc = 0x555555b35500, ctx = 0x555555b8f0e0}
ory/ngx_mruby/mruby/mrbgems/mruby-fiber/src/fiber.c:260
260 value = c->stbase[0] = MRB_PROC_ENV(c->cibase->proc)->stack[0];
(gdb) p c->cibase->proc->flags
$42 = 1536
- For the second execution, c->cibase->proc->flags equals 512.
com/matsumotory/ngx_mruby/src/http/ngx_http_mruby_module.c:841
841 if (mrb_test(ngx_mrb_start_fiber(r, state->mrb, code->proc, mrb_result))) {
(gdb) p *code
$45 = {code = {
file = 0x555555b9bb20 "Nginx::Request.new.headers_out[\"Server\"] = \"global_ngx_mruby\"; Nginx.return Nginx::DECLINED",
string = 0x555555b9bb20 "Nginx::Request.new.headers_out[\"Server\"] = \"global_ngx_mruby\"; Nginx.return Nginx::DECLINED"},
code_type = NGX_MRB_CODE_TYPE_STRING, n = 0, cache = 1, proc = 0x555555b35500, ctx = 0x555555b8f0e0}
(gdb) c
Continuing.
Breakpoint 2, fiber_switch (mrb=0x555555ad01c0, self=..., len=0, a=0x0, resume=true, vmexec=true) at /home/pyama/src/github.com/matsumotory/ngx_mruby/mruby/mrbgems/mruby-fiber/src/fiber.c:260
260 value = c->stbase[0] = MRB_PROC_ENV(c->cibase->proc)->stack[0];
(gdb) p c->cibase->proc->flags
$46 = 512
As a result, it seems that the following process is diverged and NULL is returned.
https://github.com/mruby/mruby/blob/master/include/mruby/proc.h#L69
#define MRB_PROC_ORPHAN 512
#define MRB_PROC_ORPHAN_P(p) (((p)->flags & MRB_PROC_ORPHAN) != 0)
#define MRB_PROC_ENVSET 1024
#define MRB_PROC_ENV_P(p) (((p)->flags & MRB_PROC_ENVSET) != 0)
#define MRB_PROC_ENV(p) (MRB_PROC_ENV_P(p) ? (p)->e.env : NULL)
If you have any information necessary to resolve this issue, please let me know
Metadata
Metadata
Assignees
Labels
No labels
Activity
matz commentedon Sep 20, 2023
Can you please run the reproduction code with the address sanitizer?
pyama86 commentedon Sep 20, 2023
Matz, thank you for your response. I added
enable_sanitizer 'address,undefined,leak'to the build_config.rb file and executed it. Initially, there was an error related to mruby-userdata, but after fixing that, there were no further outputs or reports from the sanitizer.pyama86 commentedon Sep 20, 2023
Additional information:
It appears that in cases where
struct RProc *rp = mrb_closure_new(mrb, rproc->body.irep);is generated as shown, the RProc's upper becomes NULL.First request:
Second request:
It seems that the issue is similar to the one described in this gist.
https://gist.github.com/yyamano/603b6934cafb68d63278f2739bc21c5e
matz commentedon Sep 20, 2023
Some questions:
presult from the following?handler_procc*c*c->cibasedearblue commentedon Sep 20, 2023
@pyama86,
I tried the combination of ngx_mruby + mruby 3.2.0 on FreeBSD, and no SEGFAULT occurred.
However, ngx_mruby uses
Fiber#resumeinmrb_funcall(), which causes aFiberErrorinfiber_resume()and the HTTP response returns "500 Internal Server Error".What will happen after applying the following patch?
If you are interested in why the changes were made to cause the
FiberError, please see the following issue:Fiber.yieldis called from a loop #5855pyama86 commentedon Sep 20, 2023
@matz
No, I am rewriting it in C because in mruby 3.2, it is no longer possible to execute across C and mruby contexts using Fiber.
Here are the results of the print:
matz commentedon Sep 20, 2023
Thank you. Could you show us values for both cases, I mean, when
proc->upperis a valid pointer, and when it isNULL?pyama86 commentedon Sep 20, 2023
@dearblue
Thank you.
The compilation succeeds, but the tests executed with
./test.shin the top directory of ngx_mruby did not pass.pyama86 commentedon Sep 20, 2023
@matz
The value entering
proc->upperactually appears to bemrb->c->ci->proc, indicating its value.https://github.com/mruby/mruby/blob/master/src/proc.c#L60
When processing successfully:
When SEGV occurs:
tmp commit
pyama86 commentedon Sep 20, 2023
I'm not sure if it will be helpful, but here is the code I'm currently working on: link.
pyama86 commentedon Sep 20, 2023
The memory address changed because of the compilation, so I'll repost the answer to the previous question.
pyama86 commentedon Sep 21, 2023
I have created a minimal implementation for reproducing the issue, so I'm sharing it with you.
https://github.com/pyama86/fiber-segv
pyama86 commentedon Sep 21, 2023
If proc is orphaned, I was wondering if the following would be acceptable. What do you think?
matz commentedon Sep 21, 2023
Thank you for the reproducing code!!! That will be a great help.
Let me investigate the real issue. Stay tuned.
4 remaining items
pyama86 commentedon Sep 23, 2023
I've resolved the issue regarding SEGV, but another mysterious phenomenon is occurring. When I create two fibers using the same mrb state, the second loop seems to behave strangely. Here's the reproduction code: Link to GitHub
The output looks like this:
Ideally, "Hello, World2" should be printed three times. If there's a possibility of another issue, please let me know, and we can create a separate issue for it.
dearblue commentedon Sep 23, 2023
Currently I remember that
mrb_fiber_resume()is not supposed to be called from inside a C function defined as a method.However, calling the
mrb_segv_init()function directly frommain(), which is prepared separately, causes the same phenomenon.Instead of
mrb_parse_string()+mrb_generate_code(), usemrb_load_string_cxt()withmrbc_context::no_exec = TRUEand it may work without problems.mrbc_context *cxt1 = mrbc_context_new(mrb); mrbc_context *cxt2 = mrbc_context_new(mrb); - mrb_value str1 = mrb_str_new_cstr(mrb, code1); - mrb_value str2 = mrb_str_new_cstr(mrb, code2); - mrb_value fiber_class1 = mrb_obj_value(mrb_class_get(mrb, "Fiber")); mrb_value fiber_class2 = mrb_obj_value(mrb_class_get(mrb, "Fiber")); - struct mrb_parser_state *p1 = mrb_parse_string(mrb, RSTRING_PTR(str1), cxt1); - struct mrb_parser_state *p2 = mrb_parse_string(mrb, RSTRING_PTR(str2), cxt2); + cxt1->no_exec = TRUE; + cxt2->no_exec = TRUE; - struct RProc *proc1 = mrb_generate_code(mrb, p1); - mrb_value fiber1 = mrb_funcall_with_block(mrb, fiber_class1, new, 0, NULL, mrb_obj_value(mrb_closure_new(mrb, proc1->body.irep))); + mrb_value proc1 = mrb_load_string_cxt(mrb, code1, cxt1); + mrb_value proc2 = mrb_load_string_cxt(mrb, code2, cxt2); + + mrb_value fiber1 = mrb_funcall_with_block(mrb, fiber_class1, new, 0, NULL, proc1); p(mrb, mrb_fiber_resume(mrb, fiber1, 0, NULL)); p(mrb, mrb_fiber_resume(mrb, fiber1, 0, NULL)); p(mrb, mrb_fiber_resume(mrb, fiber1, 0, NULL)); - struct RProc *proc2 = mrb_generate_code(mrb, p2); - mrb_value fiber2 = mrb_funcall_with_block(mrb, fiber_class2, new, 0, NULL, mrb_obj_value(mrb_closure_new(mrb, proc2->body.irep))); + mrb_value fiber2 = mrb_funcall_with_block(mrb, fiber_class2, new, 0, NULL, proc2); p(mrb, mrb_fiber_resume(mrb, fiber2, 0, NULL)); p(mrb, mrb_fiber_resume(mrb, fiber2, 0, NULL)); p(mrb, mrb_fiber_resume(mrb, fiber2, 0, NULL));pyama86 commentedon Sep 23, 2023
Yes, that's correct. So, up until version 3.1, we were creating and running Fibers in Ruby context. However, starting from version 3.2, when we switch back and forth between the C and Ruby contexts using Fibers, exceptions started to occur. Therefore, when transitioning to version 3.2, we are attempting to execute it only within the C context.
Reference: Link to the source code
In our current ngx_mruby implementation, we are generating closures from body->irep. However, when it comes to doing this successfully using the provided method, we would greatly appreciate any advice on the optimal approach.
(In the minimal implementation, we were generating a Proc from a string for convenience.)
dearblue commentedon Sep 23, 2023
By setting
mrbc_context::no_exec = TRUEand callingmrb_load_XXX_cxt(), we get a parsed and compiled executable proc object instead of an evaluated result.Also, by setting
mrbc_context::dump_result = TRUEand callingmrb_load_XXX_cxt(), we can removemrb_codedump_all()since we don't need to call it.Here is an example patch for one of the three locations found by
git grep -wn mrb_generate_code.dearblue commentedon Sep 23, 2023
If you must use mruby 3.2.0 as is, you can create an env object right after
code->proc = mrb_proc_ptr(ret)to avoid the SEGFAULT, as far as I checked with https://github.com/pyama86/fiber-segv.I would like to think there are no side effects.
pyama86 commentedon Sep 24, 2023
Thank you for your suggestion.
However, I feel it is a bit risky to rewrite the parser part of ngx_mruby due to the fact that the Fiber implementation, which worked in mruby 3.1, no longer works in 3.2.
I should have said this from the beginning, but in mruby 3.1, it was possible to resume a Fiber created in a ruby context in a C context like this , but why is this no longer possible in 3.2?
ref: https://github.com/matsumotory/ngx_mruby/blob/master/mrbgems/ngx_mruby_mrblib/mrblib/mrb_nginx.rb#L134
dearblue commentedon Sep 24, 2023
The current mruby API does not appear to provide a regular means of obtaining a proc object from an irep structure.
There is just
mrb_proc_new()andmrb_closure_new()without theMRB_APImodifier.In my opinion, the only way to turn Ruby code into a proc object is to use
mrbc_context::no_exec = TRUE+mrb_load_XXX_cxt().The reason is that there was a problem when
Fiber#resumewas called likemrb_funcall(mrb, fiber_obj, "resume", 0).Fiber.yieldis called from a loop #5855pyama86 commentedon Sep 24, 2023
Thank you for your response. I have one more question at the end.
In our use case, when a user writes the following configuration:
Internally, we want to execute it as follows:
However, with mruby 3.2 and later, since it does not allow Fibers to switch between C and Ruby contexts, am I correct in understanding that this interface is no longer usable?
Regarding the following statement:
This statement seems somewhat unrelated, and what we need is to execute the Ruby code written by the user within a Fiber. The reason is that we need to provide non-blocking APIs, and we require the use of Fiber's yield and resume, without wanting users to implement them themselves.
dearblue commentedon Sep 24, 2023
Please note at the outset that I do not have a clear understanding of the ngx_mruby process flow, so I can't say for sure.
From outside the mruby VM (e.g.
main()), switching withmrb_fiber_resume()is no problem.Likewise, from outside the mruby VM (e.g.
main()), there is no problem to run the proc object that does theFiber#resumewithmrb_top_run().It is best to assume that switching is not possible from within the mruby VM.
# 念のため、DeepL で変換する前の日本語を以下に残します。
僕は ngx_mruby の処理の流れを把握できているわけではないので、確かなことが言えないことを最初にご承知ください。
mruby VM の外側 (例えば
main()) から、mrb_fiber_resume()で切り替えることは問題ありません。同じく、mruby VM の外側 (例えば
main()) から、Fiber#resumeを行う proc オブジェクトをmrb_top_run()で実行させることに問題はありません。mruby VM の内部からは切り替えが出来ないと考えたほうが良いでしょう。
pyama86 commentedon Sep 24, 2023
Thank you for your response.
To provide a bit more historical context, the issue with Fiber has been present for quite some time, as discussed in this issue. H2O and ngx_mruby had been using a workaround by encapsulating Fiber with Proc. However, starting from version 3.2, it became evident that it wouldn't work without applying some sort of patch to mruby 3.2, so we have decided to address it with a patch.
As far as I can see, a patch that reverses this commit should suffice, at least in my preliminary assessment. This should help mitigate the impact.
Thank you for your response.
Fiber workaround does not work in mruby 3.2
Fixed unwinding block that could point to invalid PC