]> Sergey Matveev's repositories - public-inbox.git/commitdiff
lei_to_mail: reduce spew on Maildir removal
authorEric Wong <e@80x24.org>
Mon, 1 Feb 2021 08:28:27 +0000 (22:28 -1000)
committerEric Wong <e@80x24.org>
Mon, 1 Feb 2021 11:38:20 +0000 (11:38 +0000)
At most, we'll only warn once per worker when a Maildir
disappears from under us.  We'll also use the '!' OpPipe
to note the exceptional condition, and use '|' to SIGPIPE
so it'll be a bit easier for hackers to remember.

lib/PublicInbox/LEI.pm
lib/PublicInbox/LeiToMail.pm
lib/PublicInbox/LeiXSearch.pm

index e2f22a7564a4366e46a65a3a0af82833b57f5785..17ad18b9cf09aa304b211f5bc966e6cb548454de 100644 (file)
@@ -305,7 +305,8 @@ sub qerr ($;@) { $_[0]->{opt}->{quiet} or err(shift, @_) }
 
 sub fail ($$;$) {
        my ($self, $buf, $exit_code) = @_;
-       err($self, $buf);
+       err($self, $buf) if defined $buf;
+       syswrite($self->{op_pipe}, '!') if $self->{op_pipe}; # fail_handler
        x_it($self, ($exit_code // 1) << 8);
        undef;
 }
@@ -365,11 +366,10 @@ sub io_restore ($$) {
        }
 }
 
-# triggers sigpipe_handler
-sub note_sigpipe {
+sub note_sigpipe { # triggers sigpipe_handler
        my ($self, $fd) = @_;
        close(delete($self->{$fd})); # explicit close silences Perl warning
-       syswrite($self->{op_pipe}, '!') if $self->{op_pipe};
+       syswrite($self->{op_pipe}, '|') if $self->{op_pipe};
        x_it($self, 13);
 }
 
index 01e7cec52e25d2500558dd5eb08c5d2f854e6775..c6c5f84b7a08b0a0a16a89ee3ce32d002de0fb20 100644 (file)
@@ -296,14 +296,14 @@ sub _buf2maildir {
        my $kw = $smsg->{kw} // [];
        my $sfx = join('', sort(map { $kw2char{$_} // () } @$kw));
        my $rand = ''; # chosen by die roll :P
-       my ($tmp, $fh, $final);
+       my ($tmp, $fh, $final, $ok);
        my $common = $smsg->{blob} // _rand;
        if (defined(my $pct = $smsg->{pct})) { $common .= "=$pct" }
        do {
                $tmp = $dst.'tmp/'.$rand.$common;
-       } while (!sysopen($fh, $tmp, O_CREAT|O_EXCL|O_WRONLY) &&
+       } while (!($ok = sysopen($fh, $tmp, O_CREAT|O_EXCL|O_WRONLY)) &&
                $! == EEXIST && ($rand = _rand.','));
-       if (print $fh $$buf and close($fh)) {
+       if ($ok && print $fh $$buf and close($fh)) {
                # ignore new/ and write only to cur/, otherwise MUAs
                # with R/W access to the Maildir will end up doing
                # a mass rename which can take a while with thousands
@@ -316,9 +316,10 @@ sub _buf2maildir {
                        ($rand = _rand.','));
                unlink($tmp) or warn "W: failed to unlink $tmp: $!\n";
        } else {
-               my $err = $!;
+               my $err = "Error writing $smsg->{blob} to $dst: $!\n";
+               $_[0] = undef; # clobber dst
                unlink($tmp);
-               die "Error writing $smsg->{blob} to $dst: $err";
+               die $err;
        }
 }
 
@@ -329,6 +330,7 @@ sub _maildir_write_cb ($$) {
        my $dst = $lei->{ovv}->{dst};
        sub { # for git_to_mail
                my ($buf, $smsg, $eml) = @_;
+               $dst // return $lei->fail; # dst may be undef-ed in last run
                $buf //= \($eml->as_string);
                return _buf2maildir($dst, $buf, $smsg) if !$dedupe;
                $eml //= PublicInbox::Eml->new($$buf); # copy buf
index 4d390ee4661444d403ad3f45fdcbb7b32868a3fb..f630e79a9b1cb48687e5e1c8f2fbb3d3e615180b 100644 (file)
@@ -356,14 +356,17 @@ sub query_prepare { # called by wq_do
        syswrite($lei->{op_pipe}, '.') == 1 or die "do_post_augment trigger: $!"
 }
 
-sub sigpipe_handler { # handles SIGPIPE from l2m/lxs workers
-       my ($lei) = @_;
-       my $lxs = delete $lei->{lxs};
-       if ($lxs && $lxs->wq_kill_old) { # is this the daemon?
-               $lxs->wq_wait_old($lei);
+sub fail_handler ($;$$) {
+       my ($lei, $code, $io) = @_;
+       if (my $lxs = delete $lei->{lxs}) {
+               $lxs->wq_wait_old($lei) if $lxs->wq_kill_old; # lei-daemon
        }
-       close(delete $lei->{1}) if $lei->{1};
-       $lei->x_it(13);
+       close($io) if $io; # needed to avoid warnings on SIGPIPE
+       $lei->x_it($code // (1 >> 8));
+}
+
+sub sigpipe_handler { # handles SIGPIPE from l2m/lxs workers
+       fail_handler($_[0], 13, delete $_[0]->{1});
 }
 
 sub do_query {
@@ -384,7 +387,8 @@ sub do_query {
        $lei->event_step_init; # wait for shutdowns
        my $done_op = {
                '' => [ \&query_done, $lei ],
-               '!' => [ \&sigpipe_handler, $lei ]
+               '|' => [ \&sigpipe_handler, $lei ],
+               '!' => [ \&fail_handler, $lei ]
        };
        my $in_loop = exists $lei->{sock};
        $done = PublicInbox::OpPipe->new($done, $done_op, $in_loop);