From c557efd2fb6282389f7bcf1ca7e12a9f465c2f30 Mon Sep 17 00:00:00 2001 From: Jonathan del Strother Date: Thu, 2 Sep 2021 15:53:53 +0100 Subject: [PATCH] Preserve BUNDLE_APP_CONFIG on worker fork Without this, if puma is launched with BUNDLE_APP_CONFIG set, that will be lost inside of unbundled_env, which causes the worker processes to use a potentially different bundler configuration. --- lib/puma/launcher.rb | 2 ++ test/bundle_app_config_test/.bundle/config | 2 ++ test/bundle_app_config_test/Gemfile | 1 + test/bundle_app_config_test/config.ru | 1 + test/test_preserve_bundler_env.rb | 19 +++++++++++++++++++ 5 files changed, 25 insertions(+) create mode 100644 test/bundle_app_config_test/.bundle/config create mode 100644 test/bundle_app_config_test/Gemfile create mode 100644 test/bundle_app_config_test/config.ru diff --git a/lib/puma/launcher.rb b/lib/puma/launcher.rb index d1a4b7bb72..589a580a45 100644 --- a/lib/puma/launcher.rb +++ b/lib/puma/launcher.rb @@ -319,10 +319,12 @@ def prune_bundler log '* Pruning Bundler environment' home = ENV['GEM_HOME'] bundle_gemfile = Bundler.original_env['BUNDLE_GEMFILE'] + bundle_app_config = Bundler.original_env['BUNDLE_APP_CONFIG'] with_unbundled_env do ENV['GEM_HOME'] = home ENV['BUNDLE_GEMFILE'] = bundle_gemfile ENV['PUMA_BUNDLER_PRUNED'] = '1' + ENV["BUNDLE_APP_CONFIG"] = bundle_app_config args = [Gem.ruby, puma_wild_location, '-I', dirs.join(':')] + @original_argv # Ruby 2.0+ defaults to true which breaks socket activation args += [{:close_others => false}] diff --git a/test/bundle_app_config_test/.bundle/config b/test/bundle_app_config_test/.bundle/config new file mode 100644 index 0000000000..2369228816 --- /dev/null +++ b/test/bundle_app_config_test/.bundle/config @@ -0,0 +1,2 @@ +--- +BUNDLE_PATH: "vendor/bundle" diff --git a/test/bundle_app_config_test/Gemfile b/test/bundle_app_config_test/Gemfile new file mode 100644 index 0000000000..e0695ee62c --- /dev/null +++ b/test/bundle_app_config_test/Gemfile @@ -0,0 +1 @@ +gem 'puma', path: '../..' diff --git a/test/bundle_app_config_test/config.ru b/test/bundle_app_config_test/config.ru new file mode 100644 index 0000000000..deb8fc8220 --- /dev/null +++ b/test/bundle_app_config_test/config.ru @@ -0,0 +1 @@ +run lambda { |env| [200, {"Content-Type" => "text/plain"}, ["Hello World"]] } diff --git a/test/test_preserve_bundler_env.rb b/test/test_preserve_bundler_env.rb index 4a2649840d..d28faf95c3 100644 --- a/test/test_preserve_bundler_env.rb +++ b/test/test_preserve_bundler_env.rb @@ -39,6 +39,25 @@ def test_usr2_restart_preserves_bundler_environment assert_match("Gemfile.bundle_env_preservation_test", new_reply) end + def test_worker_forking_preserves_bundler_config_path + skip_unless_signal_exist? :TERM + + @tcp_port = UniquePort.call + env = { + # Disable the .bundle/config file in the bundle_app_config_test directory + "BUNDLE_APP_CONFIG" => "/dev/null", + "BUNDLER_ORIG_BUNDLE_GEMFILE" => nil + } + cmd = "bundle exec puma -q -w 1 --prune-bundler -b tcp://#{HOST}:#{@tcp_port}" + Dir.chdir File.expand_path("bundle_app_config_test", __dir__) do + @server = IO.popen(env, cmd.split, "r") + end + wait_for_server_to_boot + @pid = @server.pid + reply = read_body(connect) + assert_equal("Hello World", reply) + end + def test_phased_restart_preserves_unspecified_bundle_gemfile skip_unless_signal_exist? :USR1