Skip to content

Fix at_exit hooks running twice and unrescuable install errors with bundler/inline - #10033

Closed
smasry wants to merge 1 commit into
ruby:masterfrom
smasry:inline-fork-exit-and-errors
Closed

smasry wants to merge 1 commit into
ruby:masterfrom
smasry:inline-fork-exit-and-errors

Conversation

@smasry

@smasry smasry commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

What was the end-user or developer problem that led to this PR?

Since bundler/inline started installing gems in a forked subprocess (4.1.0.beta), three regressions compared to 4.0.24 showed up:

  1. The installer child fell off the end of the Process.fork block, so it went through the normal Ruby shutdown and ran the script's at_exit handlers a second time. ruby -e 'at_exit { puts "AT_EXIT #{$$}" }; require "bundler/inline"; gemfile(true, quiet: true) { source "https://rubygems.org"; gem "rake" }' prints AT_EXIT twice, and a minitest/autorun style script runs its whole suite inside the installer child.
  2. When installation failed, the child printed a raw Ruby backtrace and the parent called exit(status), so begin; gemfile { ... }; rescue Bundler::BundlerError; end (which worked when installation happened in-process) never ran.
  3. "Fetching gem metadata ..." and "Resolving dependencies..." were printed twice, once by the child and once by the parent's re-resolution.

What is your fix for the problem, implemented in this PR?

  • The child now always ends with exit! (after flushing $stdout/$stderr), so no at_exit handlers run in it.
  • Install errors are marshaled through a pipe and re-raised in the parent: Bundler::BundlerErrors are raised as their original class (with the child's backtrace preserved), other exceptions become a Bundler::InstallError carrying the message, and a child that dies some other way raises a Bundler::BundlerError with its status. An unrescued error therefore prints the same message/backtrace as before and exits non-zero, while scripts can rescue it again.
  • The parent's re-resolution after the subprocess install is silenced, since the child already printed that output.

The non-fork path (platforms without Process.fork) is unchanged. Specs added for all three cases.

Make sure the following tasks are checked

  • Describe the problem / feature
  • Write tests for the feature / bug fix
  • Write code for the feature / bug fix

Since `bundler/inline` started installing gems in a forked subprocess,
the child process fell off the end of the fork block, so it exited
through the regular Ruby shutdown path and ran the script's `at_exit`
handlers a second time (a minitest/autorun suite, for instance, ran once
in the installer child and once in the script itself).

A failing install in the child also printed a raw Ruby backtrace and the
parent then called `exit` with its status, so a script that rescued
`Bundler::BundlerError` around `gemfile` (which worked when installation
happened in-process) never got the chance to handle it.

The child now always ends with `exit!` after flushing its output, so no
`at_exit` handlers run in it. Errors raised while installing are
marshaled through a pipe and re-raised in the parent: Bundler errors are
raised as the original class, other exceptions are wrapped in a
`Bundler::InstallError` carrying the message, and a child that dies some
other way raises a `Bundler::BundlerError` with its status. Since the
child's backtrace is preserved, an unrescued error prints the same
message and backtrace as before and exits with a non-zero status.

Finally, the parent re-resolves the Gemfile after the child has installed
the gems, which repeated the "Fetching gem metadata" and "Resolving
dependencies..." output already printed by the child. That re-resolution
is now silent.

The non-fork code path, used on platforms without `Process.fork`, is
unchanged.
@hsbt

hsbt commented Nov 10, 2026

Copy link
Copy Markdown
Member

I merged #10048 instead, which fixes the same regressions and includes the specs from this PR. Please don't open pull requests as drafts and then leave them.

@hsbt hsbt closed this Nov 10, 2026
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