Repository navigation
fix: stop replacing TERM/INT handlers on require - #257
Merged
Merged
Conversation
Requiring the gem installed process-wide TERM and INT traps that called exit(0). This made interrupted processes exit with status 0, replaced any handlers the application had already registered, and stopped `rescue Interrupt` from catching Ctrl-C. The traps were unnecessary because the cleanup already runs from an at_exit hook, which Ruby also runs when the default INT/TERM handlers end the process. Remove the traps and add subprocess specs checking that existing handlers are kept, that the process is ended by the signal, and that the wrapper shutdown still runs. Fixes #253
Add a spec that starts a background monitor in a child process, sends TERM, and checks the at_exit shutdown stopped the monitor cleanly and quickly. Add a spec that parses every file under lib/ with Prism and fails if any calls trap, since many files are only loaded once a connection uses them and are not covered by checking handlers after a bare require. Make the handler-preservation spec compare only stdout and report each signal separately.
sophia-bq
approved these changes
Dec 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This addresses #253.
Problem
Requiring the gem (e.g. via
Bundler.require) installed process-wideTERMandINThandlers that calledexit(0). In any process without its own handlers (rails runner, Rake tasks, cron/batch jobs), this meant:SIGTERMexited with status0, reporting interrupted jobs as successful.rescue Interruptno longer caught Ctrl-C.The handlers were also unnecessary: the wrapper's cleanup already runs from an
at_exithook, which Ruby runs when a process is ended byINTorTERMunder its default handlers.Description
Change
TERM/INTtraps fromlib/aws_advanced_ruby_driver_wrapper.rb. Theat_exitshutdown hook is unchanged.Behavior change
SIGINTorSIGTERMis now terminated by the signal (exit status130/143in a shell) instead of exiting with0.Interruptagain, sorescue Interruptandensureblocks behave normally.Testing
New specs in
spec/unit/shutdown_spec.rb, run in a child Ruby process since signal handlers are process-wide:TERM/INThandlers registered before the require are still installed after it.TERMandINT, the process is terminated by the signal and the wrapper shutdown runs. OnINT,rescue Interruptcatches it.TERM, a running background monitor's loop finishes on its own and shutdown completes promptly.lib/to make sure none callstrap. This covers files that are only loaded once a connection uses them.With the old trap restored, 5 of the 7 specs in the file fail. The full unit test suite passes.
I also reproduced the issue's steps with
bin/rails runner 'sleep'in a Rails app using this branch. Ctrl-C now exits with130andkill -TERMwith143, where the 1.1.0 code exited with0for both.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.