Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

make op error handler work in cluster mode outside controllers again

Markus Kahl961907110e13
diff --git a/Gemfile b/Gemfile
index ccbbb3d60e4..a00c8a7668a 100644
--- a/Gemfile
+++ b/Gemfile
@@ -174,7 +174,7 @@ gem 'sprockets', '~> 3.7.2' # lock sprockets below 4.0
gem 'sprockets-rails', '~> 3.4.2'
gem 'puma', '~> 5.6'
-gem 'rack-timeout', '~> 0.6.0', require: "rack/timeout/base"
+gem 'rack-timeout', '~> 0.6.3', require: "rack/timeout/base"
gem 'puma-plugin-statsd', '~> 2.0'
gem 'nokogiri', '~> 1.13.4'
diff --git a/Gemfile.lock b/Gemfile.lock
index d35fb1a810b..9d30e9136f9 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -1134,7 +1134,7 @@ DEPENDENCIES
rack-mini-profiler
rack-protection (~> 2.2.0)
rack-test (~> 2.0.0)
- rack-timeout (~> 0.6.0)
+ rack-timeout (~> 0.6.3)
rack_session_access
rails (~> 7.0, >= 7.0.3.1)
rails-controller-testing (~> 1.0.2)
diff --git a/config/initializers/rack_timeout.rb b/config/initializers/rack_timeout.rb
index 17d1f34c936..2f958fa2410 100644
--- a/config/initializers/rack_timeout.rb
+++ b/config/initializers/rack_timeout.rb
@@ -30,18 +30,10 @@ if OpenProject::Configuration.web_workers >= 2
# report the generic internal server error too as it doesn't
# add any more information. Even worse, it's not immediately
# clear that the two reports are related.
- # rubocop:disable Lint/ConstantDefinitionInBlock
- module SuppressInternalErrorReportOnTimeout
- def op_handle_error(message_or_exception, context = {})
- return if request && request.env[Rack::Timeout::ENV_INFO_KEY].try(:state) == :timed_out
+ require 'rack/timeout/suppress_internal_error_report_on_timeout'
- super
- end
- end
-
- OpenProjectErrorHelper.prepend SuppressInternalErrorReportOnTimeout
+ OpenProjectErrorHelper.prepend Rack::Timeout::SuppressInternalErrorReportOnTimeout
end
- # rubocop:enable Lint/ConstantDefinitionInBlock
else
Rails.logger.debug { "Not enabling Rack::Timeout since we are not running in cluster mode with at least 2 workers" }
end
diff --git a/lib/rack/timeout/suppress_internal_error_report_on_timeout.rb b/lib/rack/timeout/suppress_internal_error_report_on_timeout.rb
new file mode 100644
index 00000000000..558859117b1
--- /dev/null
+++ b/lib/rack/timeout/suppress_internal_error_report_on_timeout.rb
@@ -0,0 +1,11 @@
+module Rack
+ class Timeout
+ module SuppressInternalErrorReportOnTimeout
+ def op_handle_error(message_or_exception, context = {})
+ return if respond_to?(:request) && request.env[Rack::Timeout::ENV_INFO_KEY].try(:state) == :timed_out
+
+ super
+ end
+ end
+ end
+end
diff --git a/spec/controllers/application_controller_spec.rb b/spec/controllers/application_controller_spec.rb
index 92c10d2a3d1..e4ac3055f65 100644
--- a/spec/controllers/application_controller_spec.rb
+++ b/spec/controllers/application_controller_spec.rb
@@ -148,4 +148,72 @@ describe ApplicationController, type: :controller do
end
end
end
+
+ describe 'rack timeout duplicate error suppression' do
+ controller do
+ include OpenProjectErrorHelper
+
+ def index
+ op_handle_error "fail"
+
+ redirect_to root_path
+ end
+ end
+
+ before do
+ allow(::OpenProject.logger).to receive(:error)
+ end
+
+ it "doesn't suppress errors when there is no timeout" do
+ get :index
+
+ expect(::OpenProject.logger).to have_received(:error) do |msg, _|
+ expect(msg).to eq "fail"
+ end
+ end
+
+ context "when there is a rack timeout" do
+ controller do
+ include OpenProjectErrorHelper
+ prepend Rack::Timeout::SuppressInternalErrorReportOnTimeout
+
+ def index
+ op_handle_error "fail"
+
+ redirect_to root_path
+ end
+ end
+
+ before do
+ allow(controller.request.env).to receive(:[]).and_call_original
+ allow(controller.request.env)
+ .to receive(:[])
+ .with(Rack::Timeout::ENV_INFO_KEY)
+ .and_return(OpenStruct.new(state: :timed_out))
+ end
+
+ it "suppresses the (duplicate) error report" do
+ get :index
+
+ expect(::OpenProject.logger).not_to have_received(:error)
+ end
+ end
+
+ context "when used outside of a controller" do
+ let(:object) do
+ klass = Class.new(Object) do
+ include OpenProjectErrorHelper
+ prepend Rack::Timeout::SuppressInternalErrorReportOnTimeout
+ end
+
+ klass.new
+ end
+
+ it "does nothing as there is no duplicate to suppress" do
+ expect { object.op_handle_error "fail" }.not_to raise_error
+
+ expect(::OpenProject.logger).to have_received(:error)
+ end
+ end
+ end
end