FIX: Rescue from readonly errors when looking up auth tokens.

FIX: Rescue from readonly errors when looking up auth tokens.

Since this is rare, we don’t want to check for Discourse.pg_readonly_mode? on every request since we have to reach for Redis. Instead, just rescue the error here.

diff --git a/lib/auth/default_current_user_provider.rb b/lib/auth/default_current_user_provider.rb
index 36ba510..f79728d 100644
--- a/lib/auth/default_current_user_provider.rb
+++ b/lib/auth/default_current_user_provider.rb
@@ -93,11 +93,17 @@ class Auth::DefaultCurrentUserProvider
       limiter = RateLimiter.new(nil, "cookie_auth_#{request.ip}", COOKIE_ATTEMPTS_PER_MIN , 60)
 
       if limiter.can_perform?
-        @user_token = UserAuthToken.lookup(auth_token,
-                                           seen: true,
-                                           user_agent: @env['HTTP_USER_AGENT'],
-                                           path: @env['REQUEST_PATH'],
-                                           client_ip: @request.ip)
+        @user_token = begin
+          UserAuthToken.lookup(
+            auth_token,
+            seen: true,
+            user_agent: @env['HTTP_USER_AGENT'],
+            path: @env['REQUEST_PATH'],
+            client_ip: @request.ip
+          )
+        rescue ActiveRecord::ReadOnlyError
+          nil
+        end
 
         current_user = @user_token.try(:user)
       end

GitHub sha: d3c972c3

1 Like

This commit appears in #9971 which was approved by eviltrout. It was merged by tgxworld.