Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sentry-rails/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
/doc/
/pkg/
/spec/reports/
/spec/support/test_rails_app/db
/tmp/

# rspec failure tracking
Expand Down
1 change: 1 addition & 0 deletions sentry-rails/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

- Support performance monitoring on ActiveJob execution [#1304](https://github.com/getsentry/sentry-ruby/pull/1304)
- Prevent background workers from holding ActiveRecord connections [#1320](https://github.com/getsentry/sentry-ruby/pull/1320)

## 4.2.2

Expand Down
1 change: 1 addition & 0 deletions sentry-rails/lib/sentry/rails.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "sentry-ruby"
require "sentry/integrable"
require "sentry/rails/background_worker"
require "sentry/rails/configuration"
require "sentry/rails/engine"
require "sentry/rails/railtie"
Expand Down
14 changes: 14 additions & 0 deletions sentry-rails/lib/sentry/rails/background_worker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Sentry
class BackgroundWorker
alias_method :original_perform, :perform

def perform(&block)
@executor.post do
# make sure the background worker returns AR connection if it accidentally acquire one during serialization
ActiveRecord::Base.connection_pool.with_connection do
block.call
end
end
end
end
end
61 changes: 61 additions & 0 deletions sentry-rails/spec/sentry/rails/client_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
require "spec_helper"

return unless Gem::Version.new(Rails.version) >= Gem::Version.new('5.1.0')

RSpec.describe Sentry::Client, type: :request do
let(:transport) do
Sentry.get_current_client.transport
end

before do
expect(ActiveRecord::Base.connection_pool.stat[:busy]).to eq(1)
end

def send_events
5.times.map do
Thread.new { Sentry::Rails.capture_message("msg") }
end.join
end

context "when serialization triggers ActiveRecord queries" do
before do
make_basic_app do |config|
config.background_worker_threads = 5
# simulate connection being obtained during event serialization
# this could happen when serializing breadcrumbs
config.before_send = lambda do |event, hint|
Post.count
event
end
end
end

it "doesn't hold the ActiveRecord connection after sending the event" do
send_events

sleep(0.5)

expect(transport.events.count).to eq(5)

expect(ActiveRecord::Base.connection_pool.stat[:busy]).to eq(1)
end
end

context "when serialization doesn't trigger ActiveRecord queries" do
before do
make_basic_app do |config|
config.background_worker_threads = 5
end
end

it "doesn't create any extra ActiveRecord connection when sending the event" do
send_events

sleep(0.1)

expect(transport.events.count).to eq(5)

expect(ActiveRecord::Base.connection_pool.stat[:busy]).to eq(1)
end
end
end
9 changes: 5 additions & 4 deletions sentry-rails/spec/support/test_rails_app/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@

ActiveSupport::Deprecation.silenced = true

ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
# need to init app before establish connection so sqlite can place the database file under the correct project root
class TestApp < Rails::Application
end

ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: "db")
ActiveRecord::Base.logger = Logger.new(nil)

ActiveRecord::Schema.define do
Expand All @@ -30,9 +34,6 @@ class Comment < ActiveRecord::Base
belongs_to :post
end

class TestApp < Rails::Application
end

class PostsController < ActionController::Base
def index
Post.all.to_a
Expand Down