Skip to content

fix: stop replacing TERM/INT handlers on require - #257

Merged
aaron-congo merged 2 commits into
mainfrom
fix/signal-traps
Dec 1, 2026
Merged

aaron-congo merged 2 commits into
mainfrom
fix/signal-traps

Conversation

@aaron-congo

Copy link
Copy Markdown
Contributor

Summary

This addresses #253.

Problem

Requiring the gem (e.g. via Bundler.require) installed process-wide TERM and INT handlers that called exit(0). In any process without its own handlers (rails runner, Rake tasks, cron/batch jobs), this meant:

  • Processes stopped with Ctrl-C or SIGTERM exited with status 0, reporting interrupted jobs as successful.
  • Handlers the application registered before the require were replaced.
  • rescue Interrupt no longer caught Ctrl-C.

The handlers were also unnecessary: the wrapper's cleanup already runs from an at_exit hook, which Ruby runs when a process is ended by INT or TERM under its default handlers.

Description

Change

  • Removed the TERM/INT traps from lib/aws_advanced_ruby_driver_wrapper.rb. The at_exit shutdown hook is unchanged.
  • Updated the related comments.

Behavior change

  • A process interrupted by SIGINT or SIGTERM is now terminated by the signal (exit status 130 / 143 in a shell) instead of exiting with 0.
  • Signal handlers registered before the gem is required are no longer replaced.
  • Ctrl-C raises Interrupt again, so rescue Interrupt and ensure blocks behave normally.
  • The wrapper's background resources are still released on shutdown.

Testing

New specs in spec/unit/shutdown_spec.rb, run in a child Ruby process since signal handlers are process-wide:

  • TERM/INT handlers registered before the require are still installed after it.
  • On TERM and INT, the process is terminated by the signal and the wrapper shutdown runs. On INT, rescue Interrupt catches it.
  • On TERM, a running background monitor's loop finishes on its own and shutdown completes promptly.
  • Prism parses every file under lib/ to make sure none calls trap. 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 with 130 and kill -TERM with 143, where the 1.1.0 code exited with 0 for both.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

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.
@aaron-congo
aaron-congo requested a review from a team as a code owner December 1, 2026 16:58
@aaron-congo
aaron-congo merged commit 1089cd8 into main Dec 1, 2026
14 checks passed
@aaron-congo
aaron-congo deleted the fix/signal-traps branch December 1, 2026 19:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants