From c19bd8eb8304d66da0b965c317c84c896594fbeb Mon Sep 17 00:00:00 2001
From: evanlok <evan.lok@upstart.com>
Date: Mon, 5 Dec 2022 15:29:49 -0800
Subject: [PATCH] Fix deadlock issue from threads autoloading and requiring the
 same dependency

---
 lib/spring/configuration.rb | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/lib/spring/configuration.rb b/lib/spring/configuration.rb
index cbd3d5e3..1fc3683a 100644
--- a/lib/spring/configuration.rb
+++ b/lib/spring/configuration.rb
@@ -11,6 +11,11 @@ def gemfile
       if /\s1.9.[0-9]/ ===  Bundler.ruby_scope.gsub(/[\/\s]+/,'')
         Pathname.new(ENV["BUNDLE_GEMFILE"] || "Gemfile").expand_path
       else
+        # default_gemfile autoloads SharedHelpers, but this causes deadlocks because it occurs in a separate thread.
+        # application/boot.rb loads the application in the main thread which calls bundler/setup and requires
+        # shared_helpers instead of autoloading. Due to a ruby bug, autoloading and requiring the same file in separate
+        # threads can cause deadlocks. Requiring shared_helpers here prevents it from being autoloaded.
+        require "bundler/shared_helpers"
         Bundler.default_gemfile
       end
     end