From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id DCB1D1FD64 for ; Thu, 21 Oct 2021 21:10:33 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 14/15] lei_mail_sync: mv_src: use transaction, check UNIQUE Date: Thu, 21 Oct 2021 21:10:31 +0000 Message-Id: <20211021211032.22666-15-e@80x24.org> In-Reply-To: <20211021211032.22666-1-e@80x24.org> References: <20211021211032.22666-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: We need a transaction across two SQL statements so readers (which don't use flock) will see the result as atomic. This may help against some occasional test failures I'm seeing from t/lei-auto-watch.t and t/lei-watch.t, or make the problem more apparent. --- lib/PublicInbox/LeiMailSync.pm | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/PublicInbox/LeiMailSync.pm b/lib/PublicInbox/LeiMailSync.pm index e70cb5de2b6b..124eb96948c5 100644 --- a/lib/PublicInbox/LeiMailSync.pm +++ b/lib/PublicInbox/LeiMailSync.pm @@ -182,16 +182,20 @@ sub mv_src { my ($self, $folder, $oidbin, $id, $newbn) = @_; my $lk = $self->lock_for_scope; my $fid = $self->{fmap}->{$folder} //= fid_for($self, $folder, 1); + $self->{dbh}->begin_work; my $sth = $self->{dbh}->prepare_cached(<<''); UPDATE blob2name SET name = ? WHERE fid = ? AND oidbin = ? AND name = ? - my $nr = $sth->execute($newbn, $fid, $oidbin, $$id); - if ($nr == 0) { # may race with a clear_src, ensure new value exists + # eval since unique constraint may fail due to race + my $nr = eval { $sth->execute($newbn, $fid, $oidbin, $$id) }; + if (!defined($nr) || $nr == 0) { # $nr may be `0E0' + # may race with a clear_src, ensure new value exists $sth = $self->{dbh}->prepare_cached(<<''); INSERT OR IGNORE INTO blob2name (oidbin, fid, name) VALUES (?, ?, ?) $sth->execute($oidbin, $fid, $newbn); } + $self->{dbh}->commit; } # read-only, iterates every oidbin + UID or name for a given folder