From cbd9045c6b887bdfad710b31317180f69de164a1 Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 23 Aug 2018 15:10:55 +1000 Subject: [PATCH] FIX: avoid race condition creating posts in some cases the distributed mutex was not protecting us and transactions would fail --- lib/post_creator.rb | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/post_creator.rb b/lib/post_creator.rb index 9d870736ddd..80f890ddba4 100644 --- a/lib/post_creator.rb +++ b/lib/post_creator.rb @@ -325,13 +325,17 @@ class PostCreator end def transaction(&blk) - Post.transaction do - if new_topic? + if new_topic? + Post.transaction do blk.call - else - # we need to ensure post_number is monotonically increasing with no gaps - # so we serialize creation to avoid needing rollbacks - DistributedMutex.synchronize("topic_id_#{@opts[:topic_id]}", &blk) + end + else + # we need to ensure post_number is monotonically increasing with no gaps + # so we serialize creation to avoid needing rollbacks + DistributedMutex.synchronize("topic_id_#{@opts[:topic_id]}") do + Post.transaction do + blk.call + end end end end