getsentry / sentry-ruby

Sentry SDK for Ruby
https://sentry.io/for/ruby
MIT License
927 stars 493 forks source link

Fix: Get connection from connection_pool instead of ActiveRecord::Base.connection #2278

Closed Iwaide closed 5 months ago

Iwaide commented 5 months ago

Issue

When attempting to retrieve a connection in ActiveRecordSubscriber, a loop occurs, eventually leading to ActiveRecord::ConnectionTimeoutError.

In versions of Rails lower than 6, since the payload does not contain a connection, it tries to use ActiveRecord::Base.connection to get the current connection. However, it seems that this causes subscribe! to be called again. I am not sure if this issue is specific to MySQL. Please refer to the following repository for a reproduction environment: https://github.com/Iwaide/re_app_for_sentry/

Fix

I have modified the code to search for and retrieve a connection from ActiveRecord::Base.connection_pool.connections. I am not very familiar with connection management, so I am not entirely confident that this is the appropriate approach. Please make any necessary corrections.

codecov[bot] commented 5 months ago

Codecov Report

Merging #2278 (11a1f51) into master (7d29d3c) will increase coverage by 0.02%. Report is 2 commits behind head on master. The diff coverage is 0.00%.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #2278 +/- ## ========================================== + Coverage 97.61% 97.64% +0.02% ========================================== Files 112 112 Lines 4155 4154 -1 ========================================== Hits 4056 4056 + Misses 99 98 -1 ``` | [Components](https://app.codecov.io/gh/getsentry/sentry-ruby/pull/2278/components?src=pr&el=components&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=getsentry) | Coverage Δ | | |---|---|---| | [sentry-ruby](https://app.codecov.io/gh/getsentry/sentry-ruby/pull/2278/components?src=pr&el=component&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=getsentry) | `98.30% <ø> (-0.04%)` | :arrow_down: | | [sentry-rails](https://app.codecov.io/gh/getsentry/sentry-ruby/pull/2278/components?src=pr&el=component&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=getsentry) | `95.22% <0.00%> (+0.17%)` | :arrow_up: | | [sentry-sidekiq](https://app.codecov.io/gh/getsentry/sentry-ruby/pull/2278/components?src=pr&el=component&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=getsentry) | `94.70% <ø> (ø)` | | | [sentry-resque](https://app.codecov.io/gh/getsentry/sentry-ruby/pull/2278/components?src=pr&el=component&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=getsentry) | `92.30% <ø> (+1.53%)` | :arrow_up: | | [sentry-delayed_job](https://app.codecov.io/gh/getsentry/sentry-ruby/pull/2278/components?src=pr&el=component&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=getsentry) | `95.60% <ø> (ø)` | | | [sentry-opentelemetry](https://app.codecov.io/gh/getsentry/sentry-ruby/pull/2278/components?src=pr&el=component&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=getsentry) | `100.00% <ø> (ø)` | | | [Files](https://app.codecov.io/gh/getsentry/sentry-ruby/pull/2278?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=getsentry) | Coverage Δ | | |---|---|---| | [...b/sentry/rails/tracing/active\_record\_subscriber.rb](https://app.codecov.io/gh/getsentry/sentry-ruby/pull/2278?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=getsentry#diff-c2VudHJ5LXJhaWxzL2xpYi9zZW50cnkvcmFpbHMvdHJhY2luZy9hY3RpdmVfcmVjb3JkX3N1YnNjcmliZXIucmI=) | `85.71% <0.00%> (+2.95%)` | :arrow_up: | ... and [2 files with indirect coverage changes](https://app.codecov.io/gh/getsentry/sentry-ruby/pull/2278/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=getsentry)