Skip to content

Question: Is replacing SIGTERM/SIGINT handlers with exit(0) on require intentional? #253

Backlog

Description

@yayamochi

Description

When the gem is required (e.g. via Bundler.require in a Rails app), lib/aws_advanced_ruby_driver_wrapper.rb installs process-wide TERM and INT handlers that call exit(0):
https://github.com/aws/aws-advanced-ruby-driver-wrapper/blob/v1.1.0/lib/aws_advanced_ruby_driver_wrapper.rb#L117-L122

Because this happens whenever the gem is loaded—even in apps using a shared Gemfile with mysql2—processes without their own signal handlers (rails runner, Rake tasks, cron/batch jobs) are affected:

  • Processes stopped with Ctrl-C (SIGINT) or SIGTERM exit with status 0, reporting interrupted jobs as successful.
  • Previously registered signal handlers are replaced rather than chained.
  • rescue Interrupt no longer catches Ctrl-C.

Could you help clarify a few questions about this behavior?

  1. Is the current behavior (exiting with status 0 and replacing existing TERM/INT handlers) intentional?
  2. Since cleanup appears to run in at_exit, are these signal handlers still needed for specific environments or edge cases?
  3. In the meantime, is it safe for applications to restore the default handlers right after the gem is loaded (Signal.trap("TERM", "DEFAULT") and Signal.trap("INT", "DEFAULT")), and will the wrapper still shut down cleanly?

Expected Behavior

Requiring the gem preserves the process's existing signal handling, allowing cleanup hooks (ensure/at_exit) to run while exiting with a non-zero status (130 for SIGINT, 143 for SIGTERM) and keeping existing handlers intact.

Current Behavior

An interrupted process exits with status 0 without raising Interrupt.

Reproduction Steps

In a Rails app with gem "aws_advanced_ruby_driver_wrapper" in its Gemfile:

bin/rails runner 'sleep'
# press Ctrl-C after the app has booted
echo $?   # => 0 (expected 130)

bin/rails runner 'sleep' & pid=$!
sleep 10                        # wait for the app to boot
kill -TERM "$pid"; wait "$pid"
echo $?   # => 0 (expected 143)

Versions

  • aws-advanced-ruby-driver-wrapper: 1.1.0 (using aws_trilogy)
  • Ruby: 4.0.10
  • OS: macOS 27.2.1 (local)

Activity

  1. sophia-bq commented on Nov 25, 2026

    @sophia-bq
    Contributor

    Hi @yayamochi ,

    Thanks for reaching out and raising this issue.

    We're taking a look at your questions and will update you once we verify our answers.

    Thank you for your patience!

  2. aaron-congo commented on Nov 27, 2026

    @aaron-congo
    Contributor

    Hi @yayamochi, thanks for the detailed report and reproduction steps - we were able to reproduce this on our side. Answers to your questions:

    1. Is the current behavior intentional?

    No, this is a bug. The wrapper has registered these TERM/INT traps since its first release so that its background resources are cleaned up on shutdown, but we did not realize that calling exit(0) from them also causes the negative side effects you mentioned.

    2. Are these signal handlers still needed?

    No. Ruby's default handlers already raise Interrupt (for SIGINT) or SignalException (for SIGTERM) in the main thread, which unwinds the stack, runs ensure blocks, and then runs at_exit hooks, including the wrapper's shutdown. We were not aware at the time that the signal handlers were unnecessary.

    3. Is it safe to restore the default handlers after the gem is loaded?

    Yes. We verified that after restoring the defaults, rescue Interrupt and ensure behave normally, the wrapper's at_exit shutdown still runs, and the process exits with 130 for SIGINT and 143 for SIGTERM. A couple of notes:

    • The restore needs to run after the gem is required, for example right after Bundler.require in config/application.rb:

      Bundler.require(*Rails.groups)
      
      Signal.trap("TERM", "DEFAULT")
      Signal.trap("INT", "DEFAULT")
    • If your application or a library installed its own TERM/INT handlers before the gem was loaded, re-install those instead of "DEFAULT", since the wrapper's handlers will have replaced them.

    We're treating this as a bug and will look into removing these signal handlers so that requiring the gem no longer changes the process's signal handling. We'll update this issue once a fix is available.

    Thanks again for raising this!

  3. yayamochi commented on Nov 30, 2026

    @yayamochi
    Author

    @sophia-bq @aaron-congo
    Thanks for the quick investigation and clear answers!
    Sounds good, I'll use this approach as a temporary workaround for now.
    Looking forward to the fix—thanks again!

  4. lgeorge-1l commented on Dec 1, 2026

    @lgeorge-1l

    Thanks for confirming the bug. Two more places our initial testing found while trying to adopt the gem

    1. Shoryuken: Shoryuken::Runner#run installs its INT and TERM handlers first and loads the application afterwards (Rails, or the files passed with -r), and nothing re-installs them, so the wrapper's handler replaces them. Both signals pointed at aws_advanced_ruby_driver_wrapper.rb:120 instead of shoryuken/runner.rb. SIGTERM then exits at once instead of running Shoryuken's graceful shutdown, and in-flight messages wait out their visibility timeout. Restoring "DEFAULT" does not help here, because Shoryuken needs its own handler back.

    2. Container tasks: A rake task or rails runner that ECS stops with SIGTERM exits 0, so the stopped task reports exit code 0 as if it had succeeded.

  5. sophia-bq commented on Dec 2, 2026

    @sophia-bq
    Contributor

    Hi @lgeorge-1l ,

    Thank you for raising these additional concerns! We believe we have addressed the underlying cause in #257 which has been merged and should be available in our next release. If you have any comments on the PR or concerns this will not solve your issues feel free to let us know.

    We'll make sure to update this ticket when the next release is out.

  6. mensfeld commented on Dec 3, 2026

    @mensfeld

    Shoryuken maintainer here.

    I can confirm the Shoryuken part of the earlier comment is accurate. shoryuken start sets up its signal handlers first and then loads the app. With 1.1.0, the wrapper replaces the INT and TERM handlers while the app loads, so Shoryuken's graceful shutdown never runs on TERM. As noted, Signal.trap("TERM", "DEFAULT") doesn't help under Shoryuken, because Shoryuken needs its own handler back.

    On the Shoryuken side, we're adding a safeguard so this can't happen with any gem: after loading the app, Shoryuken puts its handlers back and logs a warning naming the file that replaced them. It'll ship in the next Shoryuken release.

    ref ruby-shoryuken/shoryuken#1226

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions