]> git.ipfire.org Git - thirdparty/public-inbox.git/log
thirdparty/public-inbox.git
20 months agotest_common: fix excessive wait for GNU tail inotify
Eric Wong [Mon, 20 Nov 2023 07:39:09 +0000 (07:39 +0000)] 
test_common: fix excessive wait for GNU tail inotify

We want to use the filenames tail will watch, not the number of
args passed to the `tail_f' subroutine.

Fixes: 9231d2e7b93f (tests: map CLOFORK->FD_CLOEXEC temporarily for `tail -f')
20 months agoextindex: warn and hint about --gc on bad ibx_id
Eric Wong [Thu, 16 Nov 2023 11:00:20 +0000 (11:00 +0000)] 
extindex: warn and hint about --gc on bad ibx_id

Stale entries from newsgroup name changes (including adding
a `publicinbox.<name>.newsgroup' entry when none existed
before) can wreak havoc during a --reindex.  So give the
hint to users about running -extindex with --gc to clean
up stale entries.

20 months agolei q|up|convert: common finish_output to detect errors
Eric Wong [Wed, 15 Nov 2023 09:21:45 +0000 (09:21 +0000)] 
lei q|up|convert: common finish_output to detect errors

We need to consistently check the exit code of pigz|gzip|xz|bzip2
when writing to compressed mboxes (or bad storage).

20 months agolei: avoid extra fork for v2 outputs
Eric Wong [Wed, 15 Nov 2023 09:21:44 +0000 (09:21 +0000)] 
lei: avoid extra fork for v2 outputs

We've always forced LeiToMail to only have one process for v2
outputs anyways since v2 has its own sharding and IPC.  Thus we
can use the single LeiToMail process directly to avoid extra IPC
overhead.

20 months agolei convert: fix repeat and idempotent v2 output
Eric Wong [Wed, 15 Nov 2023 09:21:43 +0000 (09:21 +0000)] 
lei convert: fix repeat and idempotent v2 output

We should be able to treat v2 outputs just like any other mail
format, with the exception that content dedupe is always
enforced by the v2 format.

This allows users hosting v2 public-inboxes to catch up broken
synchronization from alternate archives such as the mbox
archives hosted by https://lists.gnu.org/

Link: https://public-inbox.org/meta/20231114-hypersonic-papaya-starling-e1cfc8@nitro/
20 months agolei: fix idempotent STDERR redirect in workers
Eric Wong [Wed, 15 Nov 2023 09:21:42 +0000 (09:21 +0000)] 
lei: fix idempotent STDERR redirect in workers

This is needed to support forking from already-forked lei workers
and $lei->{2} is already STDERR.

Fixes: e015c3742f91 (lei: use autodie where appropriate, 2023-10-17)
20 months agoxap_helper_cxx: accept leading spaces from pkg-config
Eric Wong [Wed, 15 Nov 2023 08:24:22 +0000 (08:24 +0000)] 
xap_helper_cxx: accept leading spaces from pkg-config

Eric Wong <e@80x24.org> wrote:
> Avoid mixing autodie use in different scopes since it's likely
> to cause problems like it did in Gcf2.  While none of these
> fix known problems with test cases, it's likely worthwhile to
> avoid it anyways to avoid future surprises.

>  lib/PublicInbox/XapHelperCxx.pm | 18 ++++++++----------

That XapHelperCxx change was totally necessary for running the
C++ build on CentOS 7.x (but the test is auto-skipped on any
build failure), as is this one:

--------8<--------
Subject: [PATCH] xap_helper_cxx: accept leading spaces from pkg-config

pkg-config 0.27.1 and xapian14-core-devel (1.4.24-1.el7) on
CentOS 7.x will print a leading space when running
`pkg-config --libs --cflags xapian-core'.  This leading
space creates an empty string when `split' with /\s+/ as
a pattern.  Instead, use the documented ' ' (SP) character
to put split into "awk mode" which eats leading (and
redundant) spaces and tabs.

20 months agotreewide: more autodie safety fixes for older Perl
Eric Wong [Wed, 15 Nov 2023 04:32:39 +0000 (04:32 +0000)] 
treewide: more autodie safety fixes for older Perl

Avoid mixing autodie use in different scopes since it's likely
to cause problems like it did in Gcf2.  While none of these
fix known problems with test cases, it's likely worthwhile to
avoid it anyways to avoid future surprises.

For Process::IO, we'll add some additional tests in t/io.t
to ensure we don't get unintended exceptions for try_cat.

20 months agogcf2: fix autodie usage for older Perl
Eric Wong [Wed, 15 Nov 2023 04:32:38 +0000 (04:32 +0000)] 
gcf2: fix autodie usage for older Perl

At least on Perl v5.16.3 on CentOS 7.x, use-ing autodie within
BEGIN {} affects all subroutines in that package, too.  So just
use autodie at the top-level and rely on CORE::* and try_cat
to handle cases where autodie isn't desired.

20 months agogcf2client: add alias for PublicInbox::Git::fail
Eric Wong [Wed, 15 Nov 2023 04:32:37 +0000 (04:32 +0000)] 
gcf2client: add alias for PublicInbox::Git::fail

Ensure we can ->fail properly from other subs we can within
Gcf2Client.  This doesn't fix the test failures on CentOS 7.x,
but tries to make it easier to fix underlying problems and
report OOM errors and other things which the test suite doesn't
touch on.

20 months agods: run @post_loop_do if any user-queued events run
Eric Wong [Tue, 14 Nov 2023 22:46:57 +0000 (22:46 +0000)] 
ds: run @post_loop_do if any user-queued events run

This ensures we can notice shutdown events in one-shot scripts
like -cindex (and eventually -clone/-fetch/-compact) without
forcing another real event to fire.

20 months agocindex: fix test when missing time(1) executable
Eric Wong [Wed, 15 Nov 2023 05:55:49 +0000 (05:55 +0000)] 
cindex: fix test when missing time(1) executable

It was only there for development purposes because associate is
slow, but it causes the test to get stuck on systems where it's
not available.  So remove it and just call join(1posix).

Note: this is not the `time' builtin found in shells, this
executable shows memory and pagefault info (and more with the
`-v' switch).  Unfortunately, it's not installed on many systems
despite being widely-packaged.

Reported-by: Konstantin Ryabitsev <konstantin@linuxfoundation.org>
20 months agot/lei-import: account for more verbose error
Eric Wong [Wed, 15 Nov 2023 01:04:57 +0000 (01:04 +0000)] 
t/lei-import: account for more verbose error

Perl 5.16.3 on CentOS seems more verbose in one of the EIO
tests.  Relax the regexp so we can account for extra errors
reported by Perl.

20 months agolei: use -signal numbers for old Perl
Eric Wong [Wed, 15 Nov 2023 01:04:56 +0000 (01:04 +0000)] 
lei: use -signal numbers for old Perl

Unlike modern Perls, Perl 5.16.3 on CentOS doesn't accept
negative string signals like "-TERM" .

This only became a problem since commit b231d91f42d7
(treewide: enable warnings in all exec-ed processes)
made our code stricter by enabling more warnings.
In both cases, the kill is probably unnecessary and safe
to remove since we can rely on closing sockets to drop
processes.

20 months agoTestCommon: older strace does not have --version
Konstantin Ryabitsev [Tue, 14 Nov 2023 20:16:37 +0000 (15:16 -0500)] 
TestCommon: older strace does not have --version

The tests will check for strace >= 4.16, but version 4.24 that I have
does not accept --version, only -V. This works for both older and newer
strace, so switch to using "strace -V" for the check.

Signed-off-by: Konstantin Ryabitsev <konstantin@linuxfoundation.org>
20 months agoconfig: avoid eidx_key and newsgroup conflicts
Eric Wong [Tue, 14 Nov 2023 00:32:20 +0000 (00:32 +0000)] 
config: avoid eidx_key and newsgroup conflicts

Start lowercasing newsgroup names automatically since uppercase
names are incompatible with IMAP and POP3 and also causes
problems with both -extindex and -cindex.

We'll also warn on eidx_key and newsgroup conflicts to avoid
sometimes subtle breakage when using -extindex and -cindex.

20 months agocindex: fix missing semicolon on broken $GIT_DIR/objects
Eric Wong [Tue, 14 Nov 2023 18:58:41 +0000 (18:58 +0000)] 
cindex: fix missing semicolon on broken $GIT_DIR/objects

Noticed while working on another feature...

20 months agocindex: support --associate-aggressive shortcut
Eric Wong [Mon, 13 Nov 2023 13:15:51 +0000 (13:15 +0000)] 
cindex: support --associate-aggressive shortcut

This is shorthand to enabling --associate with the most
aggressive (and time-consuming) options available, starting from
the Unix epoch and having an unlimited window to join on.

20 months agocindex: rename associate-max => window
Eric Wong [Mon, 13 Nov 2023 13:15:50 +0000 (13:15 +0000)] 
cindex: rename associate-max => window

"window" is probably a better term since it's an inexact thing
to match on.

20 months agocindex: do not guess integer maximum for Xapian
Eric Wong [Mon, 13 Nov 2023 13:15:49 +0000 (13:15 +0000)] 
cindex: do not guess integer maximum for Xapian

We can return an array to allow the caller to omit the internal
`-m' arg entirely.  We'll also allow any non-positive values to
mean there's no limit; and we'll defer the "unlimited" case to
the XapHelper implementation.  This frees us of having to deal
with mismatches between Perl and Xapian if Xapian was compiled
with 64-bit docid support and we're stuck on a 32-bit Perl
build.

20 months agoxap_helper: better variable naming for key buffer
Eric Wong [Mon, 13 Nov 2023 13:15:48 +0000 (13:15 +0000)] 
xap_helper: better variable naming for key buffer

We'll use `kbuf' for the search object key, since we already use
the `fbuf' term in `struct fbuf'.  This also adds an extra check
for open_memstream(3) failures in case of ENOMEM.

20 months agoxap_helper: stricter and harsher error handling
Eric Wong [Mon, 13 Nov 2023 13:15:47 +0000 (13:15 +0000)] 
xap_helper: stricter and harsher error handling

We'll require an error stream for dump_ibx and dump_roots
commands; they're too important to ignore.  Instead of writing
code to provide diagnostics for errors, rely on abort(3) and the
-ggdb3 compiler flag to generate nice core dumps for gdb since
all commands sent to xap_helper are from internal users.
We'll even abort on most usage errors since they could be
bugs in split2argv or our use of getopt(3).

We'll also just exit on ENOMEM errors since it's the easiest way
to recover from those errors by starting a new process which
closes all open Xapian DB handles.

20 months agocidx_xap_helper_aux: complain about truncated inputs
Eric Wong [Mon, 13 Nov 2023 13:15:46 +0000 (13:15 +0000)] 
cidx_xap_helper_aux: complain about truncated inputs

This will help us notice bugs and system resource limitations
sooner rather than later.

20 months agoxap_helper: Perl dump_ibx respects `-m MAX'
Eric Wong [Mon, 13 Nov 2023 13:15:45 +0000 (13:15 +0000)] 
xap_helper: Perl dump_ibx respects `-m MAX'

The C++ version does, so the Perl/XS version should, too;
even if we intentionally avoid using it right now.

20 months agocindex: delay associate until prune+indexing finish
Eric Wong [Mon, 13 Nov 2023 13:15:44 +0000 (13:15 +0000)] 
cindex: delay associate until prune+indexing finish

Prune can get rid of invalid commits while indexing can add new
candidates for association, so we don't dump coderepo roots for
association until those are squared away.  However, we can dump
inbox info since we don't touch inboxes while -cindex is running.

20 months agocindex: imply --all with --associate w/o -I/--only
Eric Wong [Mon, 13 Nov 2023 13:15:43 +0000 (13:15 +0000)] 
cindex: imply --all with --associate w/o -I/--only

I just forgot to use --all with --associate and it wasn't
easily apparent what was wrong.  We'll also show some extra
progress while we're at it.

20 months agospawn: don't append to scalarrefs on stdout/stderr
Eric Wong [Mon, 13 Nov 2023 13:15:42 +0000 (13:15 +0000)] 
spawn: don't append to scalarrefs on stdout/stderr

None of our current code relies on it, and I can't imagine it's
something we'd need in the future, actually...  This keeps the
door open for relying more on Spawn in TestCommon.

20 months agotreewide: update read_all to avoid eof|close checks
Eric Wong [Mon, 13 Nov 2023 13:15:41 +0000 (13:15 +0000)] 
treewide: update read_all to avoid eof|close checks

read_all can be expanded to support FIFOs/pipes/sockets where
read-until-EOF behavior is desired.  We can also rely on
wantarray to support splitting on EOL markers, but it's
hard-coded to support only `$/ eq "\n"' since (AFAIK)
it's the only way we use the wantarray form `readline'.

20 months agoxap_client: spawn C++ xap_helper directly
Eric Wong [Mon, 13 Nov 2023 13:15:40 +0000 (13:15 +0000)] 
xap_client: spawn C++ xap_helper directly

No need to suffer through an extra dose of slow Perl load times
when we can drive the build in the big parent Perl process and
get the executable path name to pass to spawn directly.

20 months agoxap_helper_cxx: use -pipe by default in CXXFLAGS
Eric Wong [Mon, 13 Nov 2023 13:15:39 +0000 (13:15 +0000)] 
xap_helper_cxx: use -pipe by default in CXXFLAGS

-ggdb3 is already used for g++ and clang, and -pipe is supported
by clang even if it's a no-op.  So just use it to speed up g++
since it saves me 30-40ms.

We'll also get rid of the explicit `-O0' since it's the default
for both clang and g++.

20 months agoxap_helper_cxx: make the build process ccache-friendly
Eric Wong [Mon, 13 Nov 2023 13:15:38 +0000 (13:15 +0000)] 
xap_helper_cxx: make the build process ccache-friendly

We need to have stable filenames and separate compilation
from the linkage stage for ccache to hit.  So avoid the use
of a temporary directory and instead rely on a lock file to
guard against parallel builds.

20 months agoxap_helper_cxx: use write_file helper
Eric Wong [Mon, 13 Nov 2023 13:15:37 +0000 (13:15 +0000)] 
xap_helper_cxx: use write_file helper

PublicInbox::IO already gets loaded by PublicInbox::Spawn, so
there's no avoiding it even if we want fast startup time :<
But startup time for this piece will be less relevant in the
near future...

20 months agocindex: use `local' for pipes between processes
Eric Wong [Mon, 13 Nov 2023 13:15:36 +0000 (13:15 +0000)] 
cindex: use `local' for pipes between processes

We can let these pipes get auto-closed upon leaving the process
subroutine scope.

20 months agotmpfile: check `stat' errors, use autodie for unlink
Eric Wong [Mon, 13 Nov 2023 13:15:35 +0000 (13:15 +0000)] 
tmpfile: check `stat' errors, use autodie for unlink

`stat' can fail due to bugs on our end or ENOMEM, but there's
no autodie support for it.  So just die if `unlink' fails, since
the FS wouldn't be usable for tmpfiles in that state, anyways.

20 months agocindex: check `say' errors w/ close or ->flush
Eric Wong [Mon, 13 Nov 2023 13:15:34 +0000 (13:15 +0000)] 
cindex: check `say' errors w/ close or ->flush

We actually need to rely on autodie `close' to check for errors,
since error-checking with `say' is not useful due to perlio
write buffering.  We'll also stop relying on `say ... or die'
since it's needless noise.

Fixes: 19f9089343c9 (cindex: drop redundant close on regular FH)
20 months agoxap_helper: reset getopt(3) properly in workers
Eric Wong [Mon, 13 Nov 2023 05:00:39 +0000 (05:00 +0000)] 
xap_helper: reset getopt(3) properly in workers

I only noticed this while doing a full -cindex --associate with
--associate-date-range=30.years.ago and --associate-max=-1 (no
limit for Xapian) between local mirrors of lore and
git.kernel.org my glibc-based system.

Apparently, glibc requires `optind = 0' to reset getopt(3) in
our workers.  Oddly, glibc appeared to work fine prior to this
change for the defaults (--associate-date-range=1.year.ago..
and --associate-max=50000).

BSDs and musl have an `optreset' variable which appear to do
the same thing, but I don't have space on BSD VMs to test full
associations.

While we're at it, we'll also keep `opterr' enabled to improve
error reporting.

20 months agolei: don't read --stdin terminals from daemon
Eric Wong [Sun, 12 Nov 2023 13:12:33 +0000 (13:12 +0000)] 
lei: don't read --stdin terminals from daemon

We must use a foreground process to read from terminals
on stdin, otherwise weird things like lost keystrokes and
EIO can happen.  So take advantage of ->send_exec_cmd to
spawn `cat' in the same way we spawn MUAs, pagers,
`git config --edit' and `git credential' from script/lei

20 months agodoc: update README.unsubscribe
Eric Wong [Sat, 11 Nov 2023 09:04:59 +0000 (09:04 +0000)] 
doc: update README.unsubscribe

The whitelist was only used in the early days of its development
and hasn't existed for a while.  I've largely forgotten this
thing exists since it's been working well...

20 months agomda: fix and test some usage problems
Eric Wong [Sat, 11 Nov 2023 09:04:58 +0000 (09:04 +0000)] 
mda: fix and test some usage problems

-mda now honors `--help' properly and invocations missing
ORIGINAL_RECIPIENT now fail with EX_NOUSER.

Helped-by: Leah Neukirchen <leah@vuxu.org>
Link: https://public-inbox.org/meta/87msvlguqu.fsf@vuxu.org/
20 months agomda|learn|watch: support dropUniqueUnsubscribe config
Eric Wong [Sat, 11 Nov 2023 09:04:57 +0000 (09:04 +0000)] 
mda|learn|watch: support dropUniqueUnsubscribe config

List-Unsubscribe headers with unique identifiers (such as those
generated by our examples/unsubscribe.milter) should not
end up in public archives.  Add a new config knob to strip
List-Unsubscribe headers if they have the
`List-Unsubscribe-Post: List-Unsubscribe=One-Click'
header.

Unfortunately, this breaks DKIM signatures if the signature
covers either of these List-Unsubscribe* headers.  However,
breaking DKIM is the lesser evil compared to any archive reader
being able to stop archival by an independent archivist.

As much as I would like this to be the default, it probably
affects few users at the moment since very few mailing lists
use unique identifiers in List-Unsubscribe (but that number
has grown, recently).

20 months agolearn: fix redundant ham import on dual matches
Eric Wong [Sat, 11 Nov 2023 09:04:56 +0000 (09:04 +0000)] 
learn: fix redundant ham import on dual matches

When learning and injecting new messages ham, we want to avoid
wasting cycles importing the same message into an inbox twice
(once for the To/Cc match and once for the List-Id match).  Our
existing %seen hash turned out to be ineffective since
PublicInbox::Inbox refs get re-blessed to PublicInbox::InboxWritable.
So we stop letting class name influence the hash key for tracking by
using the reference address instead.  We can get the reference address
by performing an arithmetic operation (+ 0) instead of having to
pay the cost of importing Scalar::Util::refaddr.

20 months agot/lei-import: skip strace for restricted systems
Eric Wong [Fri, 10 Nov 2023 22:26:33 +0000 (22:26 +0000)] 
t/lei-import: skip strace for restricted systems

Systems with Yama can restrict ptrace(2) (the underlying syscall
used by strace(1)) and make it difficult to test error handling
via error injection.  Just skip the tests on such systems since
it's probably not worth the effort to start using prctl(2) to
enable the test on such systems.

20 months agowww: add topics_(new|active).(html|atom) endpoints
Eric Wong [Fri, 10 Nov 2023 03:09:59 +0000 (03:09 +0000)] 
www: add topics_(new|active).(html|atom) endpoints

This seems like a easy (but WWW-specific) way to get recently
created and recently active topics as suggested by Konstantin.

To do this with Xapian will require a new columns and
reindexing; and I'm not sure if the current lei handling of
search results by dumping results to a format readable by common
MUAs would work well with this.  A new TUI may be required...

Suggested-by: Konstantin Ryabitsev <konstantin@linuxfoundation.org>
Link: https://public-inbox.org/meta/20231107-skilled-cobra-of-swiftness-a6ff26@meerkat/
20 months agospawn: get rid of wantarray popen_rd/popen_wr
Eric Wong [Thu, 9 Nov 2023 10:09:46 +0000 (10:09 +0000)] 
spawn: get rid of wantarray popen_rd/popen_wr

We've updated all of our users to use Process::IO (and avoiding
tied handles) so the trade-off for using the array context
no longer exists.

20 months agolei: get rid of autoreap usage
Eric Wong [Thu, 9 Nov 2023 10:09:45 +0000 (10:09 +0000)] 
lei: get rid of autoreap usage

We can rely on Process::IO->DESTROY to close and reap
in these cases.  This is the final step in eliminating
the wantarray invocations of popen_rd (and popen_wr).

20 months agoxapcmd: get rid of scalar wantarray popen_rd
Eric Wong [Thu, 9 Nov 2023 10:09:44 +0000 (10:09 +0000)] 
xapcmd: get rid of scalar wantarray popen_rd

We can rely on Process::IO->attached_pid and work towards
simplifying popen_rd.

20 months agolei_input: always close single `eml' inputs
Eric Wong [Thu, 9 Nov 2023 10:09:43 +0000 (10:09 +0000)] 
lei_input: always close single `eml' inputs

This matches the behavior we have for multi-message mbox files
since we rely on ->close to detect errors on bad mboxes.  This
ensures we'll notice errors reading single messages from stdin.

We'll also start relying more on strace error injection to test
error handling.

20 months agoipc: simplify partial sendmsg fallback
Eric Wong [Thu, 9 Nov 2023 10:09:42 +0000 (10:09 +0000)] 
ipc: simplify partial sendmsg fallback

In the rare case sendmsg(2) isn't able to send the full amount
(due to buffers >=2GB on Linux), use print + (autodie)close
to send the remainder and retry on EINTR.  `substr' should be
able to avoid a large malloc via offsets and CoW on modern Perl.

20 months agolei_mirror: note missing local manifests are non-fatal
Eric Wong [Thu, 9 Nov 2023 10:09:41 +0000 (10:09 +0000)] 
lei_mirror: note missing local manifests are non-fatal

Sometimes seeing that warning is alarming.

20 months agonet: retry on EINTR and check for {quit} flag
Eric Wong [Thu, 9 Nov 2023 10:09:40 +0000 (10:09 +0000)] 
net: retry on EINTR and check for {quit} flag

This should allow us to detect shutdown signals in -watch
more quickly and not unnecessarily fail on inconsequential
signals such as SIGWINCH.

20 months agolei ls-mail-source: gracefully handle network failures
Eric Wong [Thu, 9 Nov 2023 10:09:39 +0000 (10:09 +0000)] 
lei ls-mail-source: gracefully handle network failures

All network connections may fail, so try to emit a helpful
error message instead of attempting to dispatch methods off
`undef'.

20 months agonet_nntp_socks: more comments around how it works
Eric Wong [Thu, 9 Nov 2023 10:09:38 +0000 (10:09 +0000)] 
net_nntp_socks: more comments around how it works

This is convoluted as hell but I can't figure out a better way
to make Net::NNTP work with SOCKS.

20 months agolei_up: use v5.12
Eric Wong [Thu, 9 Nov 2023 10:09:37 +0000 (10:09 +0000)] 
lei_up: use v5.12

No unicode_strings dependencies here, AFAIK

20 months agolei: reuse FDs atfork and close explicitly
Eric Wong [Thu, 9 Nov 2023 10:09:36 +0000 (10:09 +0000)] 
lei: reuse FDs atfork and close explicitly

We'll avoid having a redundant STDERR FD open in lei workers,
and some explicit close() on `lei up' sockets reduces the
likelyhood of inadvertantly open FDs causing processes to
linger.

20 months agolei: use cached $daemon_pid when possible
Eric Wong [Thu, 9 Nov 2023 10:09:35 +0000 (10:09 +0000)] 
lei: use cached $daemon_pid when possible

->lei_daemon_pid can only be called in the top-level daemon
process when $daemon_pid is valid, so avoid a getpid(2) syscall
in those cases.

20 months agolei_xsearch: put query in process title for debugging
Eric Wong [Thu, 9 Nov 2023 10:09:34 +0000 (10:09 +0000)] 
lei_xsearch: put query in process title for debugging

Having queries in the process titles makes it easier to diagnose
stuck queries due to IPC problems.  This was used to diagnose
commit e97a30e7624d (lei: fix SIGPIPE on large result sets to pager)).

20 months agolei: fix SIGPIPE on large result sets to pager
Eric Wong [Tue, 7 Nov 2023 13:01:47 +0000 (13:01 +0000)] 
lei: fix SIGPIPE on large result sets to pager

When dealing with large search results, we need to deal with
EPIPE not just from the pager, but also EPIPE or ECONNRESET
between lei_xsearch and lei2mail processes.

Without this fix, lei_xsearch processes could linger and get
stuck writing to dead lei2mail processes if a user aborts the
pager early during a large result set.

To ensure lei_xsearch processes don't linger around after
lei2mail workers all die, we must close $l2m->{-wq_s2} before
spawning lei_xsearch processes, since $l2m->{-wq_s2} is only
used in lei2mail workers.

For `git cat-file' processes, we also need to trigger
PublicInbox::Git->close to handle unpredictable destructor
ordering to avoid using uninitialized IO refs.  This combines
with the `git_to_mail' change to deal with process cleanup
handling from premature shutdowns.

To test all this, we can't just rely on a single message being
large, but also need to rely on the result set being large
enough to saturate the lei_xsearch -> lei2mail socket so we
rely on GIANT_INBOX_DIR once again.

20 months agolei_view_text: fix inverted condition
Eric Wong [Fri, 3 Nov 2023 20:25:07 +0000 (20:25 +0000)] 
lei_view_text: fix inverted condition

This was causing `lei q -f text' output to be uncolored on
color-capable terminals.

Fixes: d3c55d072839 (treewide: use ->close to call ProcessIO->CLOSE)
20 months agot/cindex+extsearch: use write_file, autodie, etc.
Eric Wong [Thu, 2 Nov 2023 09:35:39 +0000 (09:35 +0000)] 
t/cindex+extsearch: use write_file, autodie, etc.

write_file is a new API which makes setting up config files more
pleasant, while autodie and scalarref redirects (in tests) have
been available for a while, now.  So do what we can to reduce
the code burden we have.

20 months agomove read_all, try_cat, and poll_in to PublicInbox::IO
Eric Wong [Thu, 2 Nov 2023 09:35:38 +0000 (09:35 +0000)] 
move read_all, try_cat, and poll_in to PublicInbox::IO

The IO package seems like a better home for I/O subs than the
Git package.  We lose the 60 second read timeout for `git
cat-file --batch-*' processes since it's probably not necessary
given how reliable the code has proven and things would fall
over hard in other ways if the storage device were completely
hosed.

20 months agotreewide: use eof and close to detect readline errors
Eric Wong [Thu, 2 Nov 2023 09:35:37 +0000 (09:35 +0000)] 
treewide: use eof and close to detect readline errors

readline (<FH>) isn't wrapped by autodie, and there's no
way to know if read(2) errors truncated the readline output.
IO::Handle->error isn't reliable on Perl < v5.34.

Thus, combining the `eof' and `close' (combined with autodie) is
the only way we can detect read(2) errors (injected via strace)
when called via `readline' (aka <$fh>).  Neither using `eof'
nor `close' alone is sufficient, they must be combined to detect
errors from buffered `readline'.

20 months agotreewide: check alternates writes with eof + autodie
Eric Wong [Thu, 2 Nov 2023 09:35:36 +0000 (09:35 +0000)] 
treewide: check alternates writes with eof + autodie

We must use eof() combined with close() to detect errors
in situations involving the readline()' op since `readline'
(and most buffered I/O libraries) have weak error detection
support.

This fixes error detection for files opened for read/write
access.  The next commit will fix error detection for read-only
files.

20 months agospawn: support PerlIO layer in scalar redirects
Eric Wong [Thu, 2 Nov 2023 09:35:35 +0000 (09:35 +0000)] 
spawn: support PerlIO layer in scalar redirects

We have to deal with UTF-8 data for generating patches, so make
it easier to pass Perl utf8 data to git, diff, sdiff, etc. to
avoid "Wide character" warnings.

20 months agoio: introduce write_file helper sub
Eric Wong [Thu, 2 Nov 2023 09:35:34 +0000 (09:35 +0000)] 
io: introduce write_file helper sub

This is pretty convenient way to create files for diff
generation in both WWW and lei.  The test suite should also be
able to take advantage of it.

20 months agoreplace ProcessIO with untied PublicInbox::IO
Eric Wong [Thu, 2 Nov 2023 09:35:33 +0000 (09:35 +0000)] 
replace ProcessIO with untied PublicInbox::IO

This fixes two major problems with the use of tie for filehandles:

* no way to do fcntl, stat, etc. calls directly on the tied handle,
  forcing callers to use the `tied' perlop to access the underlying
  IO::Handle

* needing separate classes to handle blocking and non-blocking I/O

As a result, Git->cleanup_if_unlinked, InputPipe->consume,
and Qspawn->_yield_start have fewer bizzare bits and we
can call `$io->blocking(0)' directly instead of
`(tied *$io)->{fh}->blocking(0)'

Having a PublicInbox::IO class will also allow us to support
custom read buffering which allows inspecting the current state.

20 months agogit_credential: use autodie where appropriate
Eric Wong [Thu, 2 Nov 2023 09:35:32 +0000 (09:35 +0000)] 
git_credential: use autodie where appropriate

We can also rely on `say' in Perl 5.10+ to save us the trouble
of printing a newline.

20 months agomulti_git: use autodie
Eric Wong [Thu, 2 Nov 2023 09:35:31 +0000 (09:35 +0000)] 
multi_git: use autodie

Trying to move away from half my code being "or die" statements...

20 months agotreewide: use ->close to call ProcessIO->CLOSE
Eric Wong [Thu, 2 Nov 2023 09:35:30 +0000 (09:35 +0000)] 
treewide: use ->close to call ProcessIO->CLOSE

This will open the door for us to drop `tie' usage from
ProcessIO completely in favor of OO method dispatch.  While
OO method dispatches (e.g. `$fh->close') are slower than normal
subroutine calls, it hardly matters in this case since process
teardown is a fairly rare operation and we continue to use
`close($fh)' for Maildir writes.

20 months agocindex: drop redundant close on regular FH
Eric Wong [Thu, 2 Nov 2023 09:35:29 +0000 (09:35 +0000)] 
cindex: drop redundant close on regular FH

There's no need to waste optree space on close() statements for
file handles which are (effectively) read-only on their last use
and incapable of error checking in our Perl code (since they're
only read by git).

Let Perl refcounting take care of it so we have less code to
wade through when focusing on `close' statements which actually
matter.

20 months agods: don't try ->close after ->accept_SSL failure
Eric Wong [Thu, 2 Nov 2023 21:35:50 +0000 (21:35 +0000)] 
ds: don't try ->close after ->accept_SSL failure

Eric Wong <e@80x24.org> wrote:
> --- a/lib/PublicInbox/DS.pm
> +++ b/lib/PublicInbox/DS.pm
> @@ -341,8 +341,8 @@ sub greet {
>   my $ev = EPOLLIN;
>   my $wbuf;
>   if ($sock->can('accept_SSL') && !$sock->accept_SSL) {
> - return CORE::close($sock) if $! != EAGAIN;
> - $ev = PublicInbox::TLS::epollbit() or return CORE::close($sock);
> + return $sock->close if $! != EAGAIN;
> + $ev = PublicInbox::TLS::epollbit() or return $sock->close;
>   $wbuf = [ \&accept_tls_step, $self->can('do_greet')];
>   }
>   new($self, $sock, $ev | EPOLLONESHOT);

Noticed this on deploy:

-----8<-----
Subject: [PATCH] ds: don't try ->close after ->accept_SSL failure

->accept_SSL failures leaves the socket ref as a GLOB (not
IO::Handle) and unable to respond to the ->close method.
Calling close in any form isn't actually necessary at all,
so just let refcounting destroy the socket.

20 months agotreewide: use ->close method rather than CORE::close
Eric Wong [Thu, 2 Nov 2023 09:35:28 +0000 (09:35 +0000)] 
treewide: use ->close method rather than CORE::close

It's easier-to-read and should open the door for us to get rid
of `tie' for ProcessIO without performance penalties for
more frequently-used perlop calls and ability to do `stat' directly
on the object instead of the awkward `tied' thing.

20 months agods: replace FD map hash table with array
Eric Wong [Thu, 2 Nov 2023 09:35:27 +0000 (09:35 +0000)] 
ds: replace FD map hash table with array

FDs are array indices into the kernel, anyways, so we can
take advantage of space savings and speedups because the
majority of FDs a big process has is going to end up in
the array, anyways.

20 months agoxap_helper.pm: use do_fork to Reset and reseed
Eric Wong [Thu, 2 Nov 2023 09:35:26 +0000 (09:35 +0000)] 
xap_helper.pm: use do_fork to Reset and reseed

We may start using rand() in the worker someday if we need
to seed a hash function for caching.  It saves us some LoC
in the meantime.

20 months agogit: reschedule cleanup if active
Eric Wong [Wed, 1 Nov 2023 06:31:48 +0000 (06:31 +0000)] 
git: reschedule cleanup if active

This is necessary to reliably cleanup cat-file processes for
coderepos in long-lived -netd and -httpd processes if they
haven't been accessed in a while.

Followup-to: 33e99002c552 (git: cleanup un-associated coderepo processes)
20 months agods: make ->close behave like CORE::close
Eric Wong [Tue, 31 Oct 2023 20:42:55 +0000 (20:42 +0000)] 
ds: make ->close behave like CORE::close

Matching existing Perl IO semantics seems like a good idea to
reduce confusion in the future.

We'll also fix some outdated comments and update indentation
to match the rest of our code base since we're far detached from
Danga::Socket at this point.

20 months agopop3: use SSL_shutdown(3ssl) if appropriate
Eric Wong [Tue, 31 Oct 2023 20:42:54 +0000 (20:42 +0000)] 
pop3: use SSL_shutdown(3ssl) if appropriate

This allows us support SSL session caching + reuse in the future.

20 months agowatch: simplify DirIdle object cleanup
Eric Wong [Tue, 31 Oct 2023 20:42:53 +0000 (20:42 +0000)] 
watch: simplify DirIdle object cleanup

There's no need to waste time nor reach into DS internals to
map FDs to Perl objects, here.  LEI.pm has never had to deal
with integer FDs for DirIdle, either.

20 months agods: move maxevents further down the stack
Eric Wong [Tue, 31 Oct 2023 20:42:52 +0000 (20:42 +0000)] 
ds: move maxevents further down the stack

The epoll implementation is the only one which respects the
limit (kevent would, but IO::KQueue does not).  In any case,
I'm not a fan of the maxevents=1000 historical default since
it leads to fairness problems with shared non-blocking listeners
across multiple daemon workers.

20 months agods: do not defer close
Eric Wong [Tue, 31 Oct 2023 20:42:51 +0000 (20:42 +0000)] 
ds: do not defer close

We can map all integer FDs to Perl objects once ->ep_wait returns,
so there's no need to play tricks elsewhere to ensure FDs can
be mapped to objects within the same event loop iteration.

20 months agods: next_tick: shorten object lifetimes
Eric Wong [Tue, 31 Oct 2023 20:42:50 +0000 (20:42 +0000)] 
ds: next_tick: shorten object lifetimes

Drop reference counts ASAP in case it saves us some memory
sooner rather than later.  This ought to give us more predictable
resource use and ensure OnDestroy callbacks fire sooner.
There's no need to use `local' to clobber the arrayref anymore,
either.

AFAIK, this doesn't fix any known bug, but more predictability
will make it easier to debug things going forward.

20 months agoxap_helper.pm: quiet undefined die at shutdown
Eric Wong [Tue, 31 Oct 2023 20:34:51 +0000 (20:34 +0000)] 
xap_helper.pm: quiet undefined die at shutdown

Another attempt at doing what commit 35de8fdcbf290e25
(xap_helper.pm: quiet undefined warnings at shutdown, 2023-10-23)
tried to do.  It turns out perl croaks (not warn/carp) when it sees
an undefined file handle, here.

20 months agopoll+select: check EBADF + POLLNVAL errors
Eric Wong [Mon, 30 Oct 2023 18:29:40 +0000 (18:29 +0000)] 
poll+select: check EBADF + POLLNVAL errors

I hit this in via select running -cindex with some other
experimental patches.  I can't reproduce the problem, though,
but this ensure we have a chance to diagnose it if it happens
again instead of looping on select(2) => EBADF.

20 months agolei_config: import read_all properly
Eric Wong [Sun, 29 Oct 2023 16:08:06 +0000 (16:08 +0000)] 
lei_config: import read_all properly

*sigh* fixing interactive editing stuff is a pain...
Found while making other changes...

Fixes: 19b791f4894efcb6 (use read_all in more places to improve safety)
20 months agolei_mirror: fix seek/sysseek argument ordering
Eric Wong [Sun, 29 Oct 2023 05:56:32 +0000 (05:56 +0000)] 
lei_mirror: fix seek/sysseek argument ordering

It doesn't matter in practice for platforms we support, since
SEEK_SET is `0' everywhere I've seen.

20 months agoexamples/logrotate: only SIGUSR1 main process
Eric Wong [Sat, 28 Oct 2023 18:01:13 +0000 (18:01 +0000)] 
examples/logrotate: only SIGUSR1 main process

There's no need to send SIGUSR1 to auxiliary processes since
they don't know what to do with them.

20 months agoexamples/*.service: avoid `nobody' user on systemd
Eric Wong [Sat, 28 Oct 2023 18:01:12 +0000 (18:01 +0000)] 
examples/*.service: avoid `nobody' user on systemd

systemd complains about `User=nobody' since `nobody' has access
to all files which can't be mapped to a valid UID.  We'll also
switch to `Group=ssl-cert' since that ought to be able to read
TLS certificates.

20 months agospawn: use readline instead of read for scalar redirects
Eric Wong [Sat, 28 Oct 2023 09:12:43 +0000 (09:12 +0000)] 
spawn: use readline instead of read for scalar redirects

Using `-s $fh' as the length arg for `read' is incorrect for
:utf8 and other non-:raw file handles because `read' operates
in *characters*, not bytes.

21 months agolei_ale: use v5.12, autodie, and try_cat
Eric Wong [Fri, 27 Oct 2023 22:21:16 +0000 (22:21 +0000)] 
lei_ale: use v5.12, autodie, and try_cat

Just things I spotted while looking for other problems
throughout our codebase.

21 months agoxt/eml_check_limits: remove useless import
Eric Wong [Fri, 27 Oct 2023 22:21:15 +0000 (22:21 +0000)] 
xt/eml_check_limits: remove useless import

Found while hunting for popen_rd calls.

21 months agowww_altid: reduce FD pressure in qspawn queues
Eric Wong [Fri, 27 Oct 2023 22:21:14 +0000 (22:21 +0000)] 
www_altid: reduce FD pressure in qspawn queues

We can use the built-in stdin redirection functionality of
spawn() instead of creating a pipe that sits idle in the queue
on busy servers.

21 months agotreewide: use run_qx where appropriate
Eric Wong [Fri, 27 Oct 2023 22:21:13 +0000 (22:21 +0000)] 
treewide: use run_qx where appropriate

This saves us some code, and is a small step towards getting
ProcessIO working with stat, fcntl and other perlops that don't
work with tied handles.

21 months agogcf2: simplify pkg-config and Inline::C setup
Eric Wong [Fri, 27 Oct 2023 22:21:12 +0000 (22:21 +0000)] 
gcf2: simplify pkg-config and Inline::C setup

We can use run_qx and try_cat to make the build setup simpler.

21 months agogit: avoid extra stat(2) for git version
Eric Wong [Fri, 27 Oct 2023 22:21:11 +0000 (22:21 +0000)] 
git: avoid extra stat(2) for git version

No sane installer will update executable files in place due to
ETXTBSY on execve.  So save ourselves a stat(2) call by relying
on the special `CORE::stat(_)' case to reuse the cached result
from the `-x FILE' filetest in which().

21 months agogit: use run_qx to read `git --version'
Eric Wong [Fri, 27 Oct 2023 22:21:10 +0000 (22:21 +0000)] 
git: use run_qx to read `git --version'

It exists, now, so save us a few lines of code.

21 months agospawn: avoid alloca in C pi_fork_exec
Eric Wong [Fri, 27 Oct 2023 22:21:09 +0000 (22:21 +0000)] 
spawn: avoid alloca in C pi_fork_exec

We don't have thread-safety to worry about, so just leave a few
allocations at process exit at worst.  We'll also update some
comments about usage while we're at it.

21 months agospawnpp: use autodie for syscall failures
Eric Wong [Fri, 27 Oct 2023 22:21:08 +0000 (22:21 +0000)] 
spawnpp: use autodie for syscall failures

We lose a little info for fork failures, but I don't think it
matters.

21 months agospawn: croak directly in C pi_fork_exec
Eric Wong [Fri, 27 Oct 2023 22:21:07 +0000 (22:21 +0000)] 
spawn: croak directly in C pi_fork_exec

This saves us some Perl code in the wrapper, since the SpawnPP
implementation also dies directly.

21 months agolei: don't exit lei-daemon on ovv_begin failure
Eric Wong [Fri, 27 Oct 2023 01:14:35 +0000 (01:14 +0000)] 
lei: don't exit lei-daemon on ovv_begin failure

When ->ovv_begin is called in LeiXSearch->do_query in the top-level
lei-daemon process, $lei->{pkt_op_p} still exists.  We must make
sure we're exiting the correct process since lei->out can call
lei->fail and lei->fail calls lei->x_it.

As to avoiding how I caused ->ovv_begin failures to begin with,
that's for a much bigger change...

21 months agocindex: clarify fatal vs non-fatal messages
Eric Wong [Thu, 26 Oct 2023 08:20:07 +0000 (08:20 +0000)] 
cindex: clarify fatal vs non-fatal messages

cindex must be able to handle coderepos being deleted mid-run
since `public-inbox-clone --purge' may be running at the same
time.  This is a step towards handling parallel invocations
of -cindex and public-inbox-clone as gracefully as possible
by improving error messages.

21 months agogit: cleanup un-associated coderepo processes
Eric Wong [Thu, 26 Oct 2023 08:20:06 +0000 (08:20 +0000)] 
git: cleanup un-associated coderepo processes

It's possible to have many coderepos with no inbox association
that never see git->cleanup.  So instead of tying git->cleanup
to inboxes, ensure it gets armed when ->watch_async is called
(since it's only called in our -netd or -httpd servers).