about summary refs log tree commit diff
diff options
context:
space:
mode:
authorThibG <thib@sitedethib.com>2019-01-16 15:42:00 +0100
committerEugen Rochko <eugen@zeonfederated.com>2019-01-16 15:42:00 +0100
commita4f07bad9529a6bebb6a0292c4ef0bfc6e29c4d2 (patch)
tree51b2b84810af1e1ae98b9b21586e4619c9a8f103
parent5363c724fc33e441cd4be22ba0e757b1ac4fd401 (diff)
Reduce chances of race conditions when processing deleted toots (#9815)
* Reduce chances of race conditions when processing deleted toots

* Prevent race condition when processing deleted toots
-rw-r--r--app/lib/activitypub/activity/create.rb4
-rw-r--r--app/lib/activitypub/activity/delete.rb10
2 files changed, 11 insertions, 3 deletions
diff --git a/app/lib/activitypub/activity/create.rb b/app/lib/activitypub/activity/create.rb
index 2b238bc88..f4cf7ceb5 100644
--- a/app/lib/activitypub/activity/create.rb
+++ b/app/lib/activitypub/activity/create.rb
@@ -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?
diff --git a/app/lib/activitypub/activity/delete.rb b/app/lib/activitypub/activity/delete.rb
index 8270fed1b..ca3cf387e 100644
--- a/app/lib/activitypub/activity/delete.rb
+++ b/app/lib/activitypub/activity/delete.rb
@@ -21,11 +21,13 @@ class ActivityPub::Activity::Delete < ActivityPub::Activity
   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?
@@ -68,4 +70,8 @@ class ActivityPub::Activity::Delete < ActivityPub::Activity
   def payload
     @payload ||= Oj.dump(@json)
   end
+
+  def lock_options
+    { redis: Redis.current, key: "create:#{object_uri}" }
+  end
 end