Eric Wong [Mon, 8 Feb 2021 09:05:09 +0000 (23:05 -1000)]
lei q: improve remote mboxrd UX + MUA
For early MUA spawners using lock-free outputs, we we need to
on the startq pipe to silence progress reporting. For
--augment users, we can start the MUA even earlier by
creating Maildirs in the pre-augment phase.
To improve progress reporting for non-MUA (or late-MUA)
spawners, we'll no longer blindly append "--compressed" to the
curl(1) command when POST-ing for the gzipped mboxrd.
Furthermore, we'll overload stringify ('""') in LeiCurl to
ensure the empty -d '' string shows up properly.
v2: fix startq waiting with --threads
mset_progress is never shown with early MUA spawning,
The plan is to still show progress when augmenting and
deduping. This fixes all local search cases.
A leftover debug bit is dropped, too
Eric Wong [Mon, 8 Feb 2021 09:11:03 +0000 (09:11 +0000)]
INSTALL: depend on Text::ParseWords
It's been distributed with Perl since 1994, and we use it for
both -imapd and lei. It's split out as a separate package in
CentOS 7.x, so we'll depend on it to avoid surprising users
of RPM-based distros.
Eric Wong [Sun, 7 Feb 2021 08:52:00 +0000 (08:52 +0000)]
imap: avoid unnecessary on-stack delete
None of the Content-Type attributes are long-lived
(and unlikely to be memory intensive). While these
callsites won't trigger $DB::args segfaults via
confess or longmess, it'll make future code audits
easier.
cf. commit 0795b0906cc81f40
("ds: guard against stack-not-refcounted quirk of Perl 5")
Eric Wong [Sun, 7 Feb 2021 08:51:56 +0000 (08:51 +0000)]
lei: replace --thread with --threads
Nobody is expected to use long options, but for consistency
with mairix(1), we'll use the pluralized option throughout
(including existing PublicInbox::{Search,SearchView}).
Eric Wong [Sun, 7 Feb 2021 08:51:55 +0000 (08:51 +0000)]
lei: remove --mua-cmd alias for --mua
While "mua-cmd" may be more accurate, nobody is expected
to type 4 extra characters. It's a needless ambiguity
with no precedence or prior art to follow.
Eric Wong [Sun, 7 Feb 2021 08:51:47 +0000 (08:51 +0000)]
ipc: do not die inside wq_worker child process
die() in a child zips up the stack into the parent, which is
undesirable behavior. We're going to exit anyways, just warn
and let exit(1) happen due to $@ being set.
Eric Wong [Sun, 7 Feb 2021 08:51:46 +0000 (08:51 +0000)]
spawn_pp: die more consistently in child
The default $SIG{__DIE__} inside a forked child doesn't actually
do what we want it to do. We don't want it to zip up the stack
the parent used, but instead want to exit the child process
after warning.
Eric Wong [Sun, 7 Feb 2021 08:51:44 +0000 (08:51 +0000)]
spawn: pi_fork_exec: support "pgid"
We'll be using this to allow the "git clone" process hierarchy
to be killed via Ctrl-C. This also fixes a long-standing bug
in error reporting for the Inline::C version, because we're
actually testing for errors, now!
n.b. strlen(3) is officially async-signal-safe as of
POSIX.1-2016, but I can't think of a reason any previous
implementation prior to that wouldn't be.
Eric Wong [Sun, 7 Feb 2021 08:51:43 +0000 (08:51 +0000)]
spawn: pi_fork_exec: restore parent sigmask in child
We continue to unblock SIGCHLD unconditionally, but also
any signals not blocked by the parent (wq_worker).
This will allow Ctrl-C (SIGINT) to stop "git clone" and allow
git-clone cleanup to be performed and other long-running
processes when pi_fork_exec supports setpgid(2). This won't
affect existing daemons on systems with signalfd(2) or
EVFILT_SIGNAL at all, since those run with signals blocked
anyways.
Eric Wong [Sat, 6 Feb 2021 12:18:43 +0000 (12:18 +0000)]
lei_curl: replace -K/--config with --curl-config
Seeing --config in the command-line for lei may mislead users
into thinking we support config file overrides that way. Rename
the option to --curl-config and drop the short switch for now.
Eric Wong [Sat, 6 Feb 2021 12:18:41 +0000 (12:18 +0000)]
lei help: split out into separate file
We'll reword and improve formatting with non-breaking spaces
("\xa0") which is only replaced with SP after wrapping.
Some terminology is shortened (e.g. "URL_OR_PATHNAME" => "LOCATION")
to improve formatting.
This also enables completion for -h/--help and lets us
prioritize favored switch names while attempting to
satisfy users relying on muscle memory from other tools.
Eric Wong [Sat, 6 Feb 2021 12:18:40 +0000 (12:18 +0000)]
lei: add-external --mirror support
This can be useful for users who want to clone and
mirror an existing public-inbox. This doesn't have
update support, yet, so users will need to run
"git fetch && public-inbox-index" for now.
Eric Wong [Sat, 6 Feb 2021 12:18:39 +0000 (12:18 +0000)]
script/lei: avoid waitpid(-1, ...) to keep tests fast
We only spawn one process to be reaped at the moment. tests
will run the contents of script/* in the same process if
possible, so any test scripts which spawn -httpd or other
read-only can cause us to stall with waitpid(-1, ...)
Eric Wong [Sat, 6 Feb 2021 12:18:38 +0000 (12:18 +0000)]
treewide: replace confess with croak
The PublicInbox::Eml (and previously Email::MIME) use of confess
was the primary (or only) culprit behind the lei2mail segfaults
fixed by commit 0795b0906cc81f40.
("ds: guard against stack-not-refcounted quirk of Perl 5").
We never care about a backtrace when dealing with Eml objects
anyways, so it was just a worthless waste of CPU cycles.
We can also drop confess in a few other places. Since we only
use Perl and Inline::C, users will never be without source
and can replace s/croak/Carp::confess/ on a per-callsite basis
to help report problems.
It's also possible to use PERL5OPT=-MCarp=verbose in the
environment though still potentially risky.
Eric Wong [Sat, 6 Feb 2021 12:18:34 +0000 (12:18 +0000)]
tests: add test_lei wrapper, split out t/lei-import.t
This will make it easier to maintain and test lei going forward,
we need to be testing against existing read-only daemons. We'll
also save ourselves some boilerplate by exporting all the
Test::More methods directly in TestCommon
We'll start using this by splitting out the latest "lei import"
tests into its own file.
Eric Wong [Sat, 6 Feb 2021 12:18:33 +0000 (12:18 +0000)]
lei_query: trim curl options
Get rid of short options which will or may conflict with
some of our own. We may switch over to "git -c http.*"
options since we need to run "git clone" and "git fetch"
anyways.
Since "keywords" is a bit long, support "kw" as a shortcut since
there's no conflict and "kw:" will be our search prefix for
looking up messages by keyword.
Eric Wong [Thu, 4 Feb 2021 09:59:29 +0000 (00:59 -0900)]
lei_xsearch: drop unused imports
Reaping is handled by the parent PublicInbox::IPC, and we
have no business using PublicInbox::Import since LeiXSearch
won't write to git directly (it will write via LeiStore).
Eric Wong [Thu, 4 Feb 2021 09:59:27 +0000 (00:59 -0900)]
lei q: eliminate $not_done temporary git dir hack
Another step towards simplifying lei internals.
None of our current uses of ->wq_do involve FD passing, and the
plan is only rely on FD passing between lei-daemon and lei(1).
Internally, it ought to be possible for lei-daemon internal bits
to be ordered properly to not need FD passing.
Eric Wong [Thu, 4 Feb 2021 09:59:23 +0000 (00:59 -0900)]
lei q: reorder internals to reduce FD passing
While FD passing is critical for script/lei <=> lei-daemon,
lei-daemon doesn't need to use it internally if FDs are
created in the proper order before forking.
Eric Wong [Wed, 3 Feb 2021 21:51:44 +0000 (15:51 -0600)]
tests: guard against missing DBD::SQLite
The features we use for SharedKV could probably be implemented
with GDBM_File or SDBM_File, but that doesn't seem worth it at
the moment since we depend on SQLite elsewhere.
The new IPC stuff doesn't work without Storable or Sereal.
Storable is part of the standard library since Perl 5.8, so
we'll put a hard dependency on it for distros that package
it separately.
Data::Dumper is also part of the standard library, and
PublicInbox::MboxReader uses it, and it's frequently useful
during development.
We'll also trim down INSTALL for standard library modules so
it's hopefully less daunting for new users.
Development dependencies are noted in HACKING, now.
Email::MIME is only used for maintainer tests, so it's only
documented in HACKING.
Eric Wong [Wed, 3 Feb 2021 21:51:41 +0000 (15:51 -0600)]
HACKING: use "just-ahead-of-time" to describe Inline::C
Inline::C works during module load time, so "just-ahead-of-time"
is a better description of it than "just-in-time". I don't
think "JAOT" is a well-known enough acronym, so it's worth
spelling it out.
Eric Wong [Wed, 3 Feb 2021 08:11:42 +0000 (22:11 -1000)]
lei: use sleep(1) loop for infinite sleep
Perl may internally race and miss signals due to a lack of
self-pipe / eventfd / signalfd / EVFILT_SIGNAL usage. While our
event loop paths avoid these problems by using signalfd or
EVFILT_SIGNAL, thse sleep() calls are not within the event loop.
Eric Wong [Wed, 3 Feb 2021 08:11:41 +0000 (22:11 -1000)]
lei add-external: completion for existing URL basenames
Given the presence of one external on a certain host or prefix
path, it's logical other inboxes would share a common prefix.
For bash users, attempt to complete that using the "-o nospace"
option of bash
Eric Wong [Wed, 3 Feb 2021 08:11:38 +0000 (22:11 -1000)]
lei q: -I/--exclude/--only support globs and basenames
We can do basename matching when it's unambiguous. Since '*?[]'
characters are rare in URLs and pathnames, we'll do glob
matching by default to support a (curl-inspired) --globoff/-g
option to disable globbing.
IO::Uncompress::Gunzip seems to be losing $? when closing
PublicInbox::ProcessPipe. To workaround this, do a synchronous
waitpid ourselves to force proper $? reporting update tests to
use the new --only feature for testing invalid URLs.
This improves internal code consistency by having {pkt_op}
parse the same ASCII-only protocol script/lei understands.
We no longer pass {sock} to worker processes at all,
further reducing FD pressure on per-user limits.
Eric Wong [Tue, 2 Feb 2021 11:47:02 +0000 (11:47 +0000)]
lei q: support --jobs [SEARCHERS],[WRITERS]
This comma-delimited parameter allows controlling the number or
lei_xsearch and lei2mail worker processes. With the change
to make IPC wq_* work use the event loop, it's now safe to
run fewer worker processes for searching with no risk of
deadlocks.
MAX_PER_HOST isn't configurable yet for remote hosts,
and maybe it shouldn't be due to potential for abuse.
Eric Wong [Tue, 2 Feb 2021 11:47:01 +0000 (11:47 +0000)]
lei q: tidy up progress reporting
We won't be reporting progress when output is going to stdout
since it can clutter up the terminal unless stderr != stdout,
which probably isn't worth checking.
We'll also use a more agnostic mset_progress which may
make it easier to support worker-less invocations.
Eric Wong [Tue, 2 Feb 2021 11:46:58 +0000 (11:46 +0000)]
lei_xsearch: ensure curl.err and tail(1) cleanup happens
We can safely rely on exit(0) here when interacting with curl(1)
and git(1), unlike query workers which hit Xapian directly,
where some badness happens when hit with a signal while
retrieving an mset.
Eric Wong [Tue, 2 Feb 2021 11:46:55 +0000 (11:46 +0000)]
lei q: do not leave temporary files after oneshot exit
Avoid on-stack shortcuts which may prevent destructors from
firing since we're not inside the event loop. We'll also tidy
up the unlink mechanism in LeiOverview while we're at it.
Eric Wong [Tue, 2 Feb 2021 11:46:50 +0000 (11:46 +0000)]
lei q: support --only, --include and --exclude
-I is short for --include since it's standard for C compilers
(along with Perl and Ruby). There are no single-character
shortcuts for --exclude or --only, since I don't expect
--exclude to be used very often and --only is already short (and
will support shell completion).
Eric Wong [Tue, 2 Feb 2021 11:46:49 +0000 (11:46 +0000)]
lei q: emit progress and counting via PktOp
Sometimes it can be confusing for "lei q" to finish writing to a
Maildir|mbox and not know if it did anything. So show some
per-external progress and stats.
These can be disabled via the new --quiet/-q switch.
We differ slightly from mairix(1) here, as we use stderr
instead of stdout for reporting totals (and we support
parallel queries from various sources).
Eric Wong [Mon, 1 Feb 2021 08:28:33 +0000 (22:28 -1000)]
doc: note optional BSD::Resource use
We've actually been capable of using this since 2019(*) in our
spawn code for PSGI limiters. And it's been used since 2016 in
our tests. It's a dependency of SpamAssassin, and Danga::Socket
used it, too.
Eric Wong [Mon, 1 Feb 2021 08:28:32 +0000 (22:28 -1000)]
lei: avoid ETOOMANYREFS, cleanup imports
As with PublicInbox::IPC, we'll attempt to bump RLIMIT_NOFILE
and transparently workaround ETOOMANYREFS. If that fails,
we'll give the user a hint to bump RLIMIT_NOFILE since
ETOOMANYREFS is an uncommon error which users may be unfamiliar
with.
Eric Wong [Mon, 1 Feb 2021 08:28:31 +0000 (22:28 -1000)]
ds: next_tick: avoid $_ in top-level loop iterator
$_ at the top of a potentially deep stack below may cause
surprising behavior as I experienced with ExtSearchIdx. In the
future, we'll limit our $_ usage to easily-auditable bits (e.g.
map, grep, and small for loops)
Eric Wong [Mon, 1 Feb 2021 08:28:30 +0000 (22:28 -1000)]
ds: guard against stack-not-refcounted quirk of Perl 5
The Perl 5 stack is weakly-referenced for performance reasons.
This means it's possible for items in the stack to be freed
while executing further down the stack.
In lei (and perhaps public-facing read-only daemons in the
future), we'll fork and call PublicInbox::DS->Reset in the child
process. This causes %DescriptorMap to be clobbered, allowing
the $DescriptorMap{$fd} arg to be freed inside the child
process.
When Carp::confess or Carp::longmess is called to generate a
backtrace, it may access the @DB::args array. This array access
is not protected by reference counting and is known to cause
segfaults and other weird errors.
While the caller of an unnecessary Carp::confess may be
eliminated in a future commit, we can't guarantee our
dependencies will be free of @DB::args access attempts
in the future.
So guard against this Perl 5 quirmk by defensively bumping the
refcount of any object we call ->event_step on.
Eric Wong [Mon, 1 Feb 2021 08:28:27 +0000 (22:28 -1000)]
lei_to_mail: reduce spew on Maildir removal
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.
Eric Wong [Mon, 1 Feb 2021 08:28:25 +0000 (22:28 -1000)]
lei: increase initial timeout
PublicInbox::Listener unconditionally sets O_NONBLOCK upon
accept(), so we need a larger timeout under heavy load since
there's no "dataready" accept filter on the listener.
With O_NONBLOCK already set, we don't have to set it at
->event_step_init
Eric Wong [Mon, 1 Feb 2021 08:28:24 +0000 (22:28 -1000)]
sharedkv: lock and explicitly disconnect {dbh}
It may be possible for updates or changes to be uncommitted
until disconnect, so we'll use flock() as we do elsewhere
to avoid the polling retry behavior of SQLite.
We also need to clear CachedKids before disconnecting to
to avoid warnings like:
->disconnect invalidates 1 active statement handle
(either destroy statement handles or call finish on
them before disconnecting)
Eric Wong [Mon, 1 Feb 2021 08:28:20 +0000 (22:28 -1000)]
lei: keep $lei around until workers are reaped
This prevents SharedKV->DESTROY in lei-daemon from triggering
before DB handles are closed in lei2mail processes. The
{each_smsg_not_done} pipe was not sufficient in this case:
that gets closed at the end of the last git_to_mail callback
invocation.