From 4f6e0c254db8e0c01141b37a8acd8a2ea8cf4f26 Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Sat, 29 Apr 2023 14:33:12 -0700 Subject: [PATCH 1/2] Fix runtime check for Puma v6 https://github.com/puma/puma/pull/3061 renamed `Rack::Handler::Puma` to `Puma::RackHandler`. When `bin/rails server` initializes, it registers the Puma handler, which then defines the `Puma` module. However, this causes GitLab to think Puma v6 has started when only Rails itself has begun to load. To avoid this problem, tighten up the runtime check by checking for `Puma::Server`, which is loaded when `require 'puma'` is run. This change is compatible with both Puma v5 and v6 because `Puma::Server` is auto-loaded when `require 'puma'` is run. This happens in two cases: 1. `bin/puma` is executed. The CLI calls `require 'puma'`. 2. `bin/rails server` is executed. `Rack::Handler` attempts to load `puma` first via `require 'rack/handler/puma'`. In Puma v5, this defines `Rack::Handler::Puma`, but in Puma v6, this defines `Puma::RackHandler`. As a result, even before Puma has been spawned, the Rails initializers will think the Puma runtime has been loaded, but crash because the memory watchdog will attempt to access `::Puma.cli_config`, which is `nil`. Changelog: changed --- lib/gitlab/runtime.rb | 2 +- spec/lib/gitlab/runtime_spec.rb | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/gitlab/runtime.rb b/lib/gitlab/runtime.rb index 7e9fb82fb8b536..f74f1489405651 100644 --- a/lib/gitlab/runtime.rb +++ b/lib/gitlab/runtime.rb @@ -38,7 +38,7 @@ def safe_identify end def puma? - !!defined?(::Puma) + !!defined?(::Puma::Server) end def sidekiq? diff --git a/spec/lib/gitlab/runtime_spec.rb b/spec/lib/gitlab/runtime_spec.rb index 181a911c667135..fa0fad6552068f 100644 --- a/spec/lib/gitlab/runtime_spec.rb +++ b/spec/lib/gitlab/runtime_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -RSpec.describe Gitlab::Runtime do +RSpec.describe Gitlab::Runtime, feature_category: :application_performance do shared_examples "valid runtime" do |runtime, max_threads| it "identifies itself" do expect(subject.identify).to eq(runtime) @@ -39,9 +39,21 @@ end end + context 'with Puma' do + before do + stub_const('::Puma::Server', double) + end + + describe '.puma?' do + it 'returns true' do + expect(subject.puma?).to be true + end + end + end + context "on multiple matches" do before do - stub_const('::Puma', double) + stub_const('::Puma::Server', double) stub_const('::Rails::Console', double) end @@ -64,6 +76,7 @@ before do stub_const('::Puma', puma_type) + allow(described_class).to receive(:puma?).and_return(true) end it_behaves_like "valid runtime", :puma, 1 + Gitlab::ActionCable::Config.worker_pool_size @@ -75,6 +88,7 @@ before do stub_const('::Puma', puma_type) + allow(described_class).to receive(:puma?).and_return(true) allow(puma_type).to receive_message_chain(:cli_config, :options).and_return(max_threads: 2, workers: max_workers) end -- GitLab From fc4457f473dbd3a754287329b40dc149a1a5c10a Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Fri, 5 May 2023 06:56:22 -0700 Subject: [PATCH 2/2] Require time gem for iso8601 support in Puma JSON formatter https://github.com/puma/puma/pull/3035 dropped the use of the time gem in Puma, but our Puma JSON-formatter needs it. --- lib/gitlab/puma_logging/json_formatter.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/gitlab/puma_logging/json_formatter.rb b/lib/gitlab/puma_logging/json_formatter.rb index 9eeb980fc534d2..6d97b6615aa5c9 100644 --- a/lib/gitlab/puma_logging/json_formatter.rb +++ b/lib/gitlab/puma_logging/json_formatter.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require 'json' +require 'time' module Gitlab module PumaLogging -- GitLab