Repository navigation
Kernel#at_exit #7807
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
Kernel#at_exit #7807
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| require 'open3' | ||
|
|
||
| # Kernel#at_exit runs when the program ends, so these run the mruby command. | ||
| def at_exit_run(code) | ||
| out, err, stat = Open3.capture3(*cmd_list("mruby"), "-e", code) | ||
| [out, err, stat.exitstatus] | ||
| end | ||
|
|
||
| def at_exit_mruby? | ||
| File.exist?(cmd_bin("mruby")) | ||
| end | ||
|
|
||
| assert('at_exit runs its blocks when the program ends, the last first') do | ||
| skip "no mruby command" unless at_exit_mruby? | ||
| out, _, status = at_exit_run('at_exit { puts "a" }; at_exit { puts "b" }; puts "main"') | ||
| assert_equal "main\nb\na\n", out | ||
| assert_equal 0, status | ||
| end | ||
|
|
||
| assert('at_exit blocks registered while they run run too') do | ||
| skip "no mruby command" unless at_exit_mruby? | ||
| out, _, _ = at_exit_run('at_exit { at_exit { puts "nested" }; puts "outer" }') | ||
| assert_equal "outer\nnested\n", out | ||
| end | ||
|
|
||
| assert('at_exit sees the exception the program ended with, before it is reported') do | ||
| skip "no mruby command" unless at_exit_mruby? | ||
| _, err, status = at_exit_run('at_exit { $stderr.puts "h #{$!.message}" }; raise "boom"') | ||
| assert_true err.start_with?("h boom\n"), err | ||
| assert_true err.include?("boom (RuntimeError)"), err | ||
| assert_equal 1, status | ||
| end | ||
|
|
||
| assert('exit in an at_exit block sets the exit status') do | ||
| skip "no mruby command" unless at_exit_mruby? | ||
| assert_equal 3, at_exit_run('at_exit { exit 3 }; puts "m"')[2] | ||
| assert_equal 4, at_exit_run('at_exit { exit 4 }; raise "boom"')[2] | ||
| out, _, status = at_exit_run('at_exit { puts $!.class }; exit 5') | ||
| assert_equal "SystemExit\n", out | ||
| assert_equal 5, status | ||
| end | ||
|
|
||
| assert('an at_exit block that raises is reported, and the rest still run') do | ||
| skip "no mruby command" unless at_exit_mruby? | ||
| _, err, status = at_exit_run('at_exit { $stderr.puts "second" }; at_exit { raise "in handler" }') | ||
| assert_true err.include?("in handler (RuntimeError)"), err | ||
| assert_true err.end_with?("second\n"), err | ||
| assert_equal 1, status | ||
| end | ||
|
|
||
| assert('exit! skips the at_exit blocks') do | ||
| skip "no mruby command" unless at_exit_mruby? | ||
| out, _, status = at_exit_run('at_exit { puts "no" }; exit! 2') | ||
| assert_equal "", out | ||
| assert_equal 2, status | ||
| end | ||
|
|
||
| assert('an at_exit block sees, as $!, what a block run before it raised') do | ||
| skip "no mruby command" unless at_exit_mruby? | ||
| out, _, status = at_exit_run('at_exit { puts $!.class }; at_exit { exit 5 }') | ||
| assert_equal "SystemExit\n", out | ||
| assert_equal 5, status | ||
| out, _, _ = at_exit_run('at_exit { puts $!.message }; at_exit { raise "h" }') | ||
| assert_equal "h\n", out | ||
| end | ||
|
|
||
| assert('the exception the program ended with outlives a block that lets go of it') do | ||
| skip "no mruby command" unless at_exit_mruby? | ||
| _, err, status = at_exit_run('at_exit { GC.start; $stderr.puts "sees #{$!.message}" }; at_exit { $! = nil; GC.start }; raise "boom"') | ||
| assert_true err.start_with?("sees boom\n"), err | ||
| assert_true err.include?("boom (RuntimeError)"), err | ||
| assert_equal 1, status | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1043,6 +1043,95 @@ mrb_print_error(mrb_state *mrb) | |
| #endif | ||
| } | ||
|
|
||
| /* | ||
| * Kernel#at_exit keeps its procs here, run last first when the program ends: | ||
| * by the program's runner with mrb_run_at_exit(), or by mrb_close() for any | ||
| * it has left. A program that registers none pays nothing. | ||
| */ | ||
| #define AT_EXIT_LIST MRB_SYM(__at_exit__) | ||
|
|
||
| static void | ||
| at_exit_close(mrb_state *mrb) | ||
| { | ||
| mrb_run_at_exit(mrb, EXIT_SUCCESS); | ||
| } | ||
|
|
||
| /* | ||
| * Registers `proc` to be called when the program ends (Kernel#at_exit). | ||
| */ | ||
| MRB_API void | ||
| mrb_at_exit(mrb_state *mrb, mrb_value proc) | ||
| { | ||
| mrb_value list = mrb_gv_get(mrb, AT_EXIT_LIST); | ||
|
|
||
| if (!mrb_array_p(list)) { | ||
| list = mrb_ary_new(mrb); | ||
| /* the close hook before the list: a list without one would not run */ | ||
| mrb_state_atexit(mrb, at_exit_close); | ||
| mrb_gv_set(mrb, AT_EXIT_LIST, list); | ||
| } | ||
| mrb_ary_push(mrb, list, proc); | ||
| } | ||
|
|
||
| static mrb_value | ||
| at_exit_call(mrb_state *mrb, void *proc) | ||
| { | ||
| return mrb_yield_argv(mrb, *(mrb_value*)proc, 0, NULL); | ||
| } | ||
|
|
||
| /* | ||
| * Runs the procs Kernel#at_exit registered, the last first, those they | ||
| * register in turn included, as the program ends with `status`, and returns | ||
| * the status it ends with then. As in CRuby, `$!` is the exception the | ||
| * program ended with (`mrb->exc`, which is left as it is) while they run, | ||
| * or the last one a proc raised; a proc that calls `exit` sets the status; | ||
| * one that raises anything else has its exception reported, and the status | ||
| * is then a failure. The rest run either way. | ||
| */ | ||
| MRB_API int | ||
| mrb_run_at_exit(mrb_state *mrb, int status) | ||
| { | ||
| mrb_value list = mrb_gv_get(mrb, AT_EXIT_LIST); | ||
| if (!mrb_array_p(list) || RARRAY_LEN(list) == 0) return status; | ||
|
|
||
| struct RObject *exc = mrb->exc; | ||
| mrb_sym bang = mrb_intern_lit(mrb, "$!"); | ||
| mrb_value err = exc ? mrb_obj_value(exc) : mrb_nil_value(); | ||
| int ai = mrb_gc_arena_save(mrb); | ||
| /* rooted while mrb->exc does not hold them, and a proc may set `$!`: | ||
| the exception the program ended with, and the one `$!` is next */ | ||
| mrb_value held = mrb_assoc_new(mrb, err, err); | ||
| int ai2 = mrb_gc_arena_save(mrb); | ||
|
|
||
| mrb->exc = NULL; | ||
| while (RARRAY_LEN(list) > 0) { | ||
| mrb_value proc = mrb_ary_pop(mrb, list); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Root each proc before removing it from the list.
|
||
| mrb_bool error; | ||
|
|
||
| /* out of the list, so rooted before anything allocates */ | ||
| mrb_gc_protect(mrb, proc); | ||
| mrb_gv_set(mrb, bang, RARRAY_PTR(held)[1]); | ||
| mrb_value r = mrb_protect_error(mrb, at_exit_call, &proc, &error); | ||
| if (error && mrb_exception_p(r)) { | ||
| struct RException *e = mrb_exc_ptr(r); | ||
| mrb_ary_set(mrb, held, 1, r); | ||
| if (MRB_EXC_EXIT_P(e)) { | ||
| status = MRB_EXC_EXIT_STATUS(mrb, (struct RObject*)e); | ||
| } | ||
| else { | ||
| mrb->exc = (struct RObject*)e; | ||
| mrb_print_error(mrb); | ||
| mrb->exc = NULL; | ||
| status = EXIT_FAILURE; | ||
| } | ||
| } | ||
| mrb_gc_arena_restore(mrb, ai2); | ||
| } | ||
| mrb->exc = exc; | ||
| mrb_gc_arena_restore(mrb, ai); | ||
| return status; | ||
| } | ||
|
|
||
| /* | ||
| * Clears the current exception status in the mruby state. | ||
| * | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep the entry exception rooted while handlers run.
This assignment removes the GC root for
exc. The localerris not a GC root. Collection can occur before$!stores it, or after a handler changes$!; a later iteration then uses the saved pointer and Line 1122 restores it tomrb->exc. Protecterrbefore clearingmrb->exc, and retain that protection across every arena restore in the loop. (raw.githubusercontent.com)