From 3195c481157e45bc9cc37c28be22ebf1b663c208 Mon Sep 17 00:00:00 2001 From: Yorhel Date: Tue, 9 Aug 2022 10:49:29 +0200 Subject: Avoid a deadlock when image vote & DB edit happen at the same time Happened about once day. Problem is that both a DB edit and a related image vote want to update the same rows in the users and images tables, but do so in different order. This workaround seems to do the trick. --- lib/VNWeb/Images/Vote.pm | 3 +++ 1 file changed, 3 insertions(+) (limited to 'lib') diff --git a/lib/VNWeb/Images/Vote.pm b/lib/VNWeb/Images/Vote.pm index 5999fb3c..8831c0af 100644 --- a/lib/VNWeb/Images/Vote.pm +++ b/lib/VNWeb/Images/Vote.pm @@ -71,6 +71,9 @@ elm_api ImageVote => undef, { return elm_Unauth if !can_vote; return elm_CSRF if !validate_token $data->{votes}; + # Lock the users table early to prevent deadlock with a concurrent DB edit that attempts to update c_changes. + tuwf->dbExeci('SELECT c_imgvotes FROM users WHERE id =', \auth->uid, 'FOR UPDATE'); + # Find out if any of these images are being overruled enrich_merge id => sub { sql 'SELECT id, bool_or(ignore) AS overruled FROM image_votes WHERE id IN', $_, 'GROUP BY id' }, $data->{votes}; enrich_merge id => sql('SELECT id, NOT ignore AS my_overrule FROM image_votes WHERE uid =', \auth->uid, 'AND id IN'), -- cgit v1.2.3