Bug #19865


Segfault when calling user signal handlers during VM shutdown

Added by ivoanjo (Ivo Anjo) 3 months ago. Updated 3 months ago.

Target version:
ruby -v:
ruby 3.2.2 (2023-03-30 revision e51014f9c0) [x86_64-linux]


Howdy 👋! I work for Datadog on the ddtrace gem . I've found this issue while investigating a customer crash report.


The original issue was found in a production app. A number of things need to be in play to cause it.

The ruby-odbc gem provides a way of accessing databases through the ODBC API. It wraps a database connection as a Data object, with a free function that, prior to freeing the native resources, disconnects from the database if the connection was still active.

Because disconnecting from the database is a blocking operation, the gem (reasonably, in my opinion), releases the global VM lock before disconnecting.

The trigger for the crash is:

  1. The app in question used puma, and puma installs a Signal.trap('TERM')
  2. The database object was still connected when the app started to shut down
  3. A VM shutdown starts...
  4. Half-way through shutdown, the VM received a SIGTERM signal, and queued it for processing
  5. The VM calls the free function on all objects
  6. The ruby-odbc gem sees there's an active database connection, and tries to release the GVL to call the blocking disconnect
  7. Before releasing the GVL, the VM checks for pending interruptions
  8. The VM tries to run the Ruby-level signal handler method half-way through VM shutdown, when you can no longer run Ruby code
  9. Segfault

How to reproduce (Ruby version & script)

I was able to reproduce on a minimal example on Ruby 3.2.2 (ruby 3.2.2 (2023-03-30 revision e51014f9c0) [x86_64-linux]) and recent master (ruby 3.3.0dev (2023-08-17T07:30:01Z master d26b015e83) [x86_64-linux]).

I've put the test-case up on github as well, but here's the important bits:


require 'signal_bug_testcase'

Signal.trap("TERM") { puts "Hello, world" }



#include <ruby.h>
#include <ruby/thread.h>
#include <signal.h>
#include <unistd.h>

typedef struct { int dummy; } BugTestcase;

void *test_nogvl(void *unused) {
    fprintf(stderr, "GVL released!\n");
    return NULL;

static void bug_testcase_free(void* ptr) {
    fprintf(stderr, "Free getting called! Sending signal...\n");
    kill(getpid(), SIGTERM);
    fprintf(stderr, "SIGTERM signal queued, trying to release GVL...\n");
    rb_thread_call_without_gvl(test_nogvl, NULL, NULL, NULL);
    fprintf(stderr, "After releasing GVL!\n");

const rb_data_type_t bug_testcase_data_type = {
    .wrap_struct_name = "SignalBugTestcase",
    .function = { NULL, bug_testcase_free, NULL },

VALUE bug_testcase_alloc(VALUE klass) {
    BugTestcase* obj = calloc(1, sizeof(BugTestcase));
    return TypedData_Make_Struct(klass, BugTestcase, &bug_testcase_data_type, obj);

void Init_signal_bug_testcase(void) {
    VALUE cBugTestcase = rb_define_class("SignalBugTestcase", rb_cObject);

    rb_define_alloc_func(cBugTestcase, bug_testcase_alloc);

Expectation and result

No segfault happens.

Interestingly, on Ruby 2.7, the VM exits half-way through but doesn't always segfault, but running it a few times always triggers the issue. On 3.2+ it crashes every time for me.

I suspect the right thing here is to no longer accept/try to run any Ruby-level signal handlers after VM shutdown starts.

Here's what I see with this test-case:

$ bundle exec ruby lib/signal-bug-testcase.rb 
Free getting called! Sending signal...
SIGTERM signal queued, trying to release GVL...
lib/signal-bug-testcase.rb:3: [BUG] Segmentation fault at 0x0000000000000007
ruby 3.2.2 (2023-03-30 revision e51014f9c0) [x86_64-linux]

-- Control frame information -----------------------------------------------
c:0003 p:---- s:0011 e:000010 CFUNC  :puts
c:0002 p:0005 s:0006 e:000005 BLOCK  lib/signal-bug-testcase.rb:3 [FINISH]
c:0001 p:0000 s:0003 E:0001a0 DUMMY  [FINISH]

-- Ruby level backtrace information ----------------------------------------
lib/signal-bug-testcase.rb:3:in `block in <main>'
lib/signal-bug-testcase.rb:3:in `puts'

-- Machine register context ------------------------------------------------
 RIP: 0x000070aa64cedbe7 RBP: 0x000070aa648e8fd0 RSP: 0x00007ffc5c057608
 RAX: 0x0000000000004171 RBX: 0x00007ffc5c057630 RCX: 0x0000000000000001
 RDX: 0x00007ffc5c057630 RDI: 0x0000000000000007 RSI: 0x0000000000004171
  R8: 0x000000000000021b  R9: 0x0000000000000000 R10: 0x000070aa63eff048
 R11: 0x0000000000000000 R12: 0x000070aa648e8fd0 R13: 0x0000000000004171
 R14: 0x0000000000000007 R15: 0x000070aa648e8ff0 EFL: 0x0000000000010202

-- C level backtrace information -------------------------------------------
ruby-3.2.2/lib/ [0x70aa64d5bb5f] ruby-3.2.2/vm_dump.c:785
ruby-3.2.2/lib/ ruby-3.2.2/vm_dump.c:1080
ruby-3.2.2/lib/ [0x70aa64b52164] ruby-3.2.2/error.c:813
ruby-3.2.2/lib/ [0x70aa64cab0fd] ruby-3.2.2/signal.c:964
/lib/x86_64-linux-gnu/ [0x70aa64642520]
ruby-3.2.2/lib/ [0x70aa64cedbe7] ruby-3.2.2/symbol.h:72
ruby-3.2.2/lib/ ruby-3.2.2/id_table.c:230
ruby-3.2.2/lib/ [0x70aa64d356bb] ruby-3.2.2/vm_method.c:1295
ruby-3.2.2/lib/ ruby-3.2.2/vm_method.c:1365
ruby-3.2.2/lib/ ruby-3.2.2/vm_method.c:1402
ruby-3.2.2/lib/ ruby-3.2.2/vm_method.c:1409
ruby-3.2.2/lib/ [0x70aa64d36258] ruby-3.2.2/vm_eval.c:434
ruby-3.2.2/lib/ [0x70aa64d4e877] ruby-3.2.2/vm_eval.c:483
ruby-3.2.2/lib/ [0x70aa64d4f406] ruby-3.2.2/vm_eval.c:877
ruby-3.2.2/lib/ ruby-3.2.2/vm_eval.c:1074
ruby-3.2.2/lib/ [0x70aa64d30277] ruby-3.2.2/vm_insnhelper.c:3268
ruby-3.2.2/lib/ [0x70aa64d407a4] ruby-3.2.2/vm_insnhelper.c:5080
ruby-3.2.2/lib/ ruby-3.2.2/insns.def:820
ruby-3.2.2/lib/ [0x70aa64d460d3] ruby-3.2.2/vm.c:2374
ruby-3.2.2/lib/ [0x70aa64d4bfcf] ruby-3.2.2/vm.c:1603
ruby-3.2.2/lib/ [0x70aa64d4c5cf] ruby-3.2.2/vm_eval.c:274
ruby-3.2.2/lib/ [0x70aa64d4e7e7] ruby-3.2.2/vm_eval.c:87
ruby-3.2.2/lib/ ruby-3.2.2/vm_eval.c:551
ruby-3.2.2/lib/ [0x70aa64d4f406] ruby-3.2.2/vm_eval.c:877
ruby-3.2.2/lib/ ruby-3.2.2/vm_eval.c:1074
ruby-3.2.2/lib/ [0x70aa64d4f562] ruby-3.2.2/vm_eval.c:1923
ruby-3.2.2/lib/ [0x70aa64caae16] ruby-3.2.2/signal.c:1064
ruby-3.2.2/lib/ [0x70aa64cf7078] ruby-3.2.2/thread.c:2334
ruby-3.2.2/lib/ ruby-3.2.2/thread.c:2291
ruby-3.2.2/lib/ [0x70aa64cf7ac5] ruby-3.2.2/vm_core.h:1994
ruby-3.2.2/lib/ ruby-3.2.2/vm_core.h:1985
ruby-3.2.2/lib/ ruby-3.2.2/thread.c:320
ruby-3.2.2/lib/ ruby-3.2.2/thread.c:1485
ruby-3.2.2/lib/ [0x70aa64cf90cf] ruby-3.2.2/thread.c:1548
signal-bug-testcase-2/lib/ [0x70aa6518f299] ../../../../ext/signal_bug_testcase/signal_bug_testcase.c:17
signal-bug-testcase-2/lib/ ../../../../ext/signal_bug_testcase/signal_bug_testcase.c:18
ruby-3.2.2/lib/ [0x70aa64b73172] ruby-3.2.2/gc.c:4388
ruby-3.2.2/lib/ ruby-3.2.2/gc.c:4407
ruby-3.2.2/lib/ ruby-3.2.2/gc.c:4436
ruby-3.2.2/lib/ [0x70aa64b80d70] ruby-3.2.2/gc.c:4573
ruby-3.2.2/lib/ [0x70aa64b5d6d1] ruby-3.2.2/eval.c:168
ruby-3.2.2/lib/ ruby-3.2.2/eval.c:262
ruby-3.2.2/lib/ [0x70aa64b5d91d] ruby-3.2.2/eval.c:330
ruby-3.2.2/bin/ruby(rb_main+0x21) [0x5d5d1295f187] ./main.c:38
ruby-3.2.2/bin/ruby(main) ./main.c:57
/lib/x86_64-linux-gnu/ [0x70aa64629d90] ../sysdeps/nptl/libc_start_call_main.h:58
/lib/x86_64-linux-gnu/ [0x70aa64629e40] ../csu/libc-start.c:392
/lib/x86_64-linux-gnu/ ../csu/libc-start.c:379
/lib/x86_64-linux-gnu/ (null):0

Files (7.73 KB) Testcase packaged as a gem ivoanjo (Ivo Anjo), 09/06/2023 08:48 AM

Updated by nobu (Nobuyoshi Nakada) 3 months ago

While what I saw was failure at allocation of an internal array to call the signal handler instead of segfault, it can't work anyway.
This is because, in VM finalization phase, any object allocation is no longer possible and even exception can't raise.

I suspect the right thing here is to no longer accept/try to run any Ruby-level signal handlers after VM shutdown starts.


Updated by ivoanjo (Ivo Anjo) 3 months ago

Thanks for the quick fix! :)


Also available in: Atom PDF