From a7749d663f6de73984fb17b9b1c30f827c6feeb4 Mon Sep 17 00:00:00 2001 From: Ryan Lue Date: Fri, 20 Jul 2018 13:08:26 +0800 Subject: [PATCH] Handle race condition when calling ActivityStream#assets (fixes #2066) --- lib/sessions/backend/activity_stream.rb | 7 +++++- .../sessions/backend/activity_stream_spec.rb | 24 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 spec/lib/sessions/backend/activity_stream_spec.rb diff --git a/lib/sessions/backend/activity_stream.rb b/lib/sessions/backend/activity_stream.rb index 4ec3e5abf..fbea4557f 100644 --- a/lib/sessions/backend/activity_stream.rb +++ b/lib/sessions/backend/activity_stream.rb @@ -31,8 +31,13 @@ class Sessions::Backend::ActivityStream assets = {} item_ids = [] activity_stream.each do |item| + begin + assets = item.assets(assets) + rescue ActiveRecord::RecordNotFound + next + end + item_ids.push item.id - assets = item.assets(assets) end { diff --git a/spec/lib/sessions/backend/activity_stream_spec.rb b/spec/lib/sessions/backend/activity_stream_spec.rb new file mode 100644 index 000000000..071cc28fd --- /dev/null +++ b/spec/lib/sessions/backend/activity_stream_spec.rb @@ -0,0 +1,24 @@ +require 'rails_helper' + +RSpec.describe Sessions::Backend::ActivityStream do + context 'when async processes affect associated objects / DB records (#2066)' do + let(:subject) { described_class.new(user, {}) } + let(:user) { create(:agent_user, groups: [group]) } + let(:group) { Group.find_by(name: 'Users') } + let(:associated_tickets) { create_list(:ticket, ticket_count, group: group) } + let(:ticket_count) { 20 } + + before do + Setting.set('system_init_done', true) + + # these records must be created before the example begins + # (same as `let!`, but harder to miss) + associated_tickets + end + + it 'manages race condition' do + Thread.new { associated_tickets.each(&:destroy) } + expect { subject.load }.not_to raise_error + end + end +end