about summary refs log tree commit homepage
path: root/lib/PublicInbox/LeiStore.pm
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2021-04-29 19:49:57 +0000
committerEric Wong <e@80x24.org>2021-04-30 06:41:37 +0000
commit6c7b884068318011af6f8382a023befa815643c3 (patch)
tree349c2e1939d00ee285a3fae9cf1ccc735c10ed4e /lib/PublicInbox/LeiStore.pm
parentdc82a4a2af4c8c2e34cdbd56e07f097dff5c9a78 (diff)
downloadpublic-inbox-6c7b884068318011af6f8382a023befa815643c3.tar.gz
Prior to this change, it was possible for oneshot lei processes
to race on epoch creation/rollover.  lei-daemon normally
prevents the problem by funnelling all writes to a single
socket, but oneshot lei has no such protection.
Diffstat (limited to 'lib/PublicInbox/LeiStore.pm')
-rw-r--r--lib/PublicInbox/LeiStore.pm9
1 files changed, 6 insertions, 3 deletions
diff --git a/lib/PublicInbox/LeiStore.pm b/lib/PublicInbox/LeiStore.pm
index fcc9224d..8af740fd 100644
--- a/lib/PublicInbox/LeiStore.pm
+++ b/lib/PublicInbox/LeiStore.pm
@@ -88,6 +88,7 @@ sub importer {
                 $self->checkpoint;
                 $max = $self->git_epoch_max + 1;
         }
+        my (undef, $tl) = eidx_init($self); # acquire lock
         my $pfx = $self->git_pfx;
         $max //= $self->git_epoch_max;
         while (1) {
@@ -97,7 +98,9 @@ sub importer {
                 my $git = PublicInbox::Git->new($latest);
                 if (!$old) {
                         $git->qx(qw(config core.sharedRepository 0600));
-                        $self->done; # force eidx_init on next round
+                        $self->done; # unlock
+                        # re-acquire lock, update alternates for new epoch
+                        (undef, $tl) = eidx_init($self);
                 }
                 my $packed_bytes = $git->packed_bytes;
                 my $unpacked_bytes = $packed_bytes / $self->packing_factor;
@@ -130,7 +133,7 @@ sub eidx_init {
         my $tl = wantarray && $self->{-err_wr} ?
                         PublicInbox::OnDestroy->new($$, \&_tail_err, $self) :
                         undef;
-        $eidx->idx_init({-private => 1});
+        $eidx->idx_init({-private => 1}); # acquires lock
         wantarray ? ($eidx, $tl) : $eidx;
 }
 
@@ -204,7 +207,7 @@ sub set_sync_info ($$$) {
 sub add_eml {
         my ($self, $eml, $vmd, $xoids) = @_;
         my $im = $self->importer; # may create new epoch
-        my ($eidx, $tl) = eidx_init($self); # updates/writes alternates file
+        my ($eidx, $tl) = eidx_init($self);
         my $oidx = $eidx->{oidx}; # PublicInbox::Import::add checks this
         my $smsg = bless { -oidx => $oidx }, 'PublicInbox::Smsg';
         my $im_mark = $im->add($eml, undef, $smsg);