Skip to content

Commit

Permalink
Reduce chances of race conditions when processing deleted toots (mast…
Browse files Browse the repository at this point in the history
…odon#9815)

* Reduce chances of race conditions when processing deleted toots

* Prevent race condition when processing deleted toots
  • Loading branch information
ClearlyClaire authored and hiyuki2578 committed Oct 2, 2019
1 parent 2410b57 commit a192382
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
4 changes: 3 additions & 1 deletion app/lib/activitypub/activity/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ class ActivityPub::Activity::Create < ActivityPub::Activity
CONVERTED_TYPES = %w(Image Video Article Page).freeze

def perform
return if delete_arrived_first?(object_uri) || unsupported_object_type? || invalid_origin?(@object['id'])
return if unsupported_object_type? || invalid_origin?(@object['id'])

RedisLock.acquire(lock_options) do |lock|
if lock.acquired?
return if delete_arrived_first?(object_uri)

@status = find_existing_status

if @status.nil?
Expand Down
10 changes: 8 additions & 2 deletions app/lib/activitypub/activity/delete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ def delete_person
def delete_note
return if object_uri.nil?

RedisLock.acquire(lock_options) do |_lock|
delete_later!(object_uri)
end

@status = Status.find_by(uri: object_uri, account: @account)
@status ||= Status.find_by(uri: @object['atomUri'], account: @account) if @object.is_a?(Hash) && @object['atomUri'].present?

delete_later!(object_uri)

return if @status.nil?

if @status.public_visibility? || @status.unlisted_visibility?
Expand Down Expand Up @@ -68,4 +70,8 @@ def delete_now!
def payload
@payload ||= Oj.dump(@json)
end

def lock_options
{ redis: Redis.current, key: "create:#{object_uri}" }
end
end

0 comments on commit a192382

Please sign in to comment.