A platform for community discussion. Free, open, simple.
FIX: Handle PostgreSQL read-only errors correctly (#42586)
Roman Rizzi1982af36b10b
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rbindex 0c2da748de4..f36cf30d6b3 100644--- a/app/controllers/application_controller.rb+++ b/app/controllers/application_controller.rb@@ -152,7 +152,7 @@ class ApplicationController < ActionController::Baserescue_from PG::ReadOnlySqlTransaction do |e|Discourse.received_postgres_readonly!Rails.logger.error("#{e.class} #{e.message}: #{e.backtrace.join("\n")}")- rescue_with_handler(Discourse::ReadOnly) || raise+ rescue_with_handler(Discourse::ReadOnly.new) || raiseendrescue_from ActionController::ParameterMissing do |e|diff --git a/app/controllers/test_requests_controller.rb b/app/controllers/test_requests_controller.rbindex 6729ad4feb9..e73d3d69a05 100644--- a/app/controllers/test_requests_controller.rb+++ b/app/controllers/test_requests_controller.rb@@ -19,5 +19,9 @@ class TestRequestsController < ApplicationControllerrender json: net_http_getend++ def test_postgres_readonly+ raise PG::ReadOnlySqlTransaction, "cannot execute INSERT in a read-only transaction"+ endendenddiff --git a/config/routes.rb b/config/routes.rbindex 3555889c7bc..4f9a35c4683 100644--- a/config/routes.rb+++ b/config/routes.rb@@ -1984,6 +1984,7 @@ Discourse::Application.routes.draw do# Routes that are only used for testingget "/test_net_http_timeouts" => "test_requests#test_net_http_timeouts"get "/test_net_http_headers" => "test_requests#test_net_http_headers"+ get "/test_postgres_readonly" => "test_requests#test_postgres_readonly"end# This catch-all route should always be lastdiff --git a/spec/requests/application_controller_spec.rb b/spec/requests/application_controller_spec.rbindex bb7fc380eb2..b18469f89e7 100644--- a/spec/requests/application_controller_spec.rb+++ b/spec/requests/application_controller_spec.rb@@ -3,6 +3,20 @@RSpec.describe ApplicationController dofab!(:user)+ describe "handling PostgreSQL read-only errors" do+ after { Discourse.clear_postgres_readonly! }++ it "returns a read-only response" do+ get "/test_postgres_readonly.json"++ expect(response.status).to eq(503)+ expect(response.parsed_body).to eq(+ "errors" => [I18n.t("read_only_mode_enabled")],+ "error_type" => "read_only",+ )+ end+ end+describe "shared session key" dobefore { SiteSetting.long_polling_base_url = "https://mb.example.com/" }