pools: lock pool when adding/removing posts (fixes #3091).

Adding a post id to a pool's post_ids string is non-atomic, hence we
must lock the pool to avoid a race condition.

Adding a pool to a post's pool_string is likewise non-atomic, hence we
must lock the post as well.
This commit is contained in:
evazion
2017-05-26 16:22:03 -05:00
parent f38810bd1c
commit 31b58e17b1
2 changed files with 24 additions and 16 deletions

View File

@@ -246,9 +246,11 @@ class Pool < ActiveRecord::Base
return if contains?(post.id)
return if is_deleted?
update_attributes(:post_ids => add_number_to_string(post.id, post_ids), :post_count => post_count + 1)
post.add_pool!(self, true)
clear_post_id_array
with_lock do
update_attributes(:post_ids => add_number_to_string(post.id, post_ids), :post_count => post_count + 1)
post.add_pool!(self, true)
clear_post_id_array
end
end
def remove!(post)
@@ -256,9 +258,11 @@ class Pool < ActiveRecord::Base
return unless CurrentUser.user.can_remove_from_pools?
return if is_deleted?
update_attributes(:post_ids => remove_number_from_string(post.id, post_ids), :post_count => post_count - 1)
post.remove_pool!(self, true)
clear_post_id_array
with_lock do
update_attributes(:post_ids => remove_number_from_string(post.id, post_ids), :post_count => post_count - 1)
post.remove_pool!(self, true)
clear_post_id_array
end
end
def add_number_to_string(number, string)