From 6437f27f9091d00cfa65062a2519bdaa91ff9e28 Mon Sep 17 00:00:00 2001 From: Alan Guo Xiang Tan Date: Fri, 23 Feb 2024 13:37:37 +0800 Subject: [PATCH] DEV: Debug AR connection pool queue on CI (#25828) Why this change? On CI, we have been seeing flaky system tests because ActiveRecord is unable to checkout a connection. This patch is meant to help us debug which thread is not returning the connection to the queue. Example of timeout issue: https://github.com/discourse/discourse/actions/runs/8012541636/job/21888013082 --- .github/workflows/tests.yml | 1 + lib/freedom_patches/debug_connection_queue.rb | 40 +++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 lib/freedom_patches/debug_connection_queue.rb diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 22c4ed07369..f2d18c2cf4f 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -254,6 +254,7 @@ jobs: if: matrix.build_type == 'system' && matrix.target == 'chat' env: CHECKOUT_TIMEOUT: 10 + DEBUG_AR_CONNECTION_QUEUE: 1 run: LOAD_PLUGINS=1 RAILS_ENABLE_TEST_LOG=1 RAILS_TEST_LOG_LEVEL=error PARALLEL_TEST_PROCESSORS=4 bin/turbo_rspec --use-runtime-info --profile=50 --verbose --format documentation plugins/chat/spec/system timeout-minutes: 30 diff --git a/lib/freedom_patches/debug_connection_queue.rb b/lib/freedom_patches/debug_connection_queue.rb new file mode 100644 index 00000000000..fd70e6bcd83 --- /dev/null +++ b/lib/freedom_patches/debug_connection_queue.rb @@ -0,0 +1,40 @@ +# frozen_string_literal: true + +if ENV["DEBUG_AR_CONNECTION_QUEUE"] == "1" + module QueuePatch + # Add +element+ to the queue. Never blocks. + def add(element) + puts "::group::##{Process.pid} Adding element to the queue" + puts Thread.current.backtrace.first(30).join("\n") + puts "::endgroup::" + super + end + + # If +element+ is in the queue, remove and return it, or +nil+. + def delete(element) + puts "::group::##{Process.pid} Delete element from the queue" + puts Thread.current.backtrace.first(30).join("\n") + puts "::endgroup::" + super + end + + # Remove all elements from the queue. + def clear + puts "::group::##{Process.pid} Clear all elements from the queue" + puts Thread.current.backtrace.first(30).join("\n") + puts "::endgroup::" + super + end + + private + + def remove + puts "::group::##{Process.pid} Removing element from the queue" + puts Thread.current.backtrace.first(30).join("\n") + puts "::endgroup::" + super + end + end + + ActiveRecord::ConnectionAdapters::ConnectionPool::Queue.prepend(QueuePatch) +end