From 78d8458773f9538ae75f3c0074c6903ff1b5ec06 Mon Sep 17 00:00:00 2001
From: DJ Mountney <david@twkie.net>
Date: Fri, 27 May 2016 14:28:04 -0700
Subject: [PATCH] Only use RequestStore for current_application_settings during
 a request

This fixes an issue where the RequestStore was being populated with the settings in the unicorn master
during the rails initializers. Each forked worker would then start their first request with an uncleaned RequestStore.
---
 lib/gitlab/current_settings.rb | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)

diff --git a/lib/gitlab/current_settings.rb b/lib/gitlab/current_settings.rb
index f44d1b3a44e..59e86707d3e 100644
--- a/lib/gitlab/current_settings.rb
+++ b/lib/gitlab/current_settings.rb
@@ -1,18 +1,22 @@
 module Gitlab
   module CurrentSettings
     def current_application_settings
-      key = :current_application_settings
-
-      RequestStore.store[key] ||= begin
-        settings = nil
+      if RequestStore.active?
+        RequestStore.fetch(:current_application_settings) { ensure_application_settings! }
+      else
+        ensure_application_settings!
+      end
+    end
 
-        if connect_to_db?
-          settings = ::ApplicationSetting.current
-          settings ||= ::ApplicationSetting.create_from_defaults unless ActiveRecord::Migrator.needs_migration?
-        end
+    def ensure_application_settings!
+      settings = nil
 
-        settings || fake_application_settings
+      if connect_to_db?
+        settings = ::ApplicationSetting.current
+        settings ||= ::ApplicationSetting.create_from_defaults unless ActiveRecord::Migrator.needs_migration?
       end
+
+      settings || fake_application_settings
     end
 
     def fake_application_settings
-- 
GitLab