* [PATCH 07/12] lei: more eval guards for die on failure
2021-10-16 1:00 7% [PATCH 00/16] some yak-shaving and annoyance fixes Eric Wong
@ 2021-10-16 1:00 7% ` Eric Wong
0 siblings, 0 replies; 2+ results
From: Eric Wong @ 2021-10-16 1:00 UTC (permalink / raw)
To: meta
Relying on $lei->fail is unsustainable since there'll always
be parts of our code and dependencies which can trigger die()
and break the event loop.
---
lib/PublicInbox/LEI.pm | 6 +++---
lib/PublicInbox/LeiLcat.pm | 21 +++++++++------------
lib/PublicInbox/LeiQuery.pm | 24 +++++++++++-------------
lib/PublicInbox/LeiXSearch.pm | 9 +++++----
4 files changed, 28 insertions(+), 32 deletions(-)
diff --git a/lib/PublicInbox/LEI.pm b/lib/PublicInbox/LEI.pm
index 0cdcf4492885..511b2c1d03a7 100644
--- a/lib/PublicInbox/LEI.pm
+++ b/lib/PublicInbox/LEI.pm
@@ -556,7 +556,7 @@ sub _lei_atfork_child {
# we need to explicitly close things which are on stack
if ($persist) {
open $self->{3}, '<', '/' or die "open(/) $!";
- fchdir($self) or die;
+ fchdir($self);
close($_) for (grep(defined, delete @$self{qw(0 1 2 sock)}));
if (my $cfg = $self->{cfg}) {
delete @$cfg{qw(-lei_store -watches -lei_note_event)};
@@ -779,7 +779,7 @@ sub lazy_cb ($$$) {
sub dispatch {
my ($self, $cmd, @argv) = @_;
- fchdir($self) or return;
+ fchdir($self);
local %ENV = %{$self->{env}};
local $current_lei = $self; # for __WARN__
$self->{2}->autoflush(1); # keep stdout buffered until x_it|DESTROY
@@ -1381,7 +1381,7 @@ sub wq_done_wait { # dwaitpid callback
sub fchdir {
my ($lei) = @_;
my $dh = $lei->{3} // die 'BUG: lei->{3} (CWD) gone';
- chdir($dh) || $lei->fail("fchdir: $!");
+ chdir($dh) || die "fchdir: $!";
}
sub wq_eof { # EOF callback for main daemon
diff --git a/lib/PublicInbox/LeiLcat.pm b/lib/PublicInbox/LeiLcat.pm
index d553b18733da..191f6f244857 100644
--- a/lib/PublicInbox/LeiLcat.pm
+++ b/lib/PublicInbox/LeiLcat.pm
@@ -124,18 +124,15 @@ could not extract Message-ID from $x
sub _stdin { # PublicInbox::InputPipe::consume callback for --stdin
my ($lei) = @_; # $_[1] = $rbuf
- if (defined($_[1])) {
- $_[1] eq '' and return eval {
- $lei->fchdir or return;
- my @argv = split(/\s+/, $lei->{mset_opt}->{qstr});
- $lei->{mset_opt}->{qstr} = extract_all($lei, @argv)
- or return;
- $lei->_start_query;
- };
- $lei->{mset_opt}->{qstr} .= $_[1];
- } else {
- $lei->fail("error reading stdin: $!");
- }
+ $_[1] // return $lei->fail("error reading stdin: $!");
+ return $lei->{mset_opt}->{qstr} .= $_[1] if $_[1] ne '';
+ eval {
+ $lei->fchdir;
+ my @argv = split(/\s+/, $lei->{mset_opt}->{qstr});
+ $lei->{mset_opt}->{qstr} = extract_all($lei, @argv) or return;
+ $lei->_start_query;
+ };
+ $lei->fail($@) if $@;
}
sub lei_lcat {
diff --git a/lib/PublicInbox/LeiQuery.pm b/lib/PublicInbox/LeiQuery.pm
index c65b00ca0986..ec2ece051492 100644
--- a/lib/PublicInbox/LeiQuery.pm
+++ b/lib/PublicInbox/LeiQuery.pm
@@ -55,19 +55,17 @@ sub _start_query { # used by "lei q" and "lei up"
}
sub qstr_add { # PublicInbox::InputPipe::consume callback for --stdin
- my ($self) = @_; # $_[1] = $rbuf
- if (defined($_[1])) {
- $_[1] eq '' and return eval {
- $self->fchdir or return;
- $self->{mset_opt}->{q_raw} = $self->{mset_opt}->{qstr};
- $self->{lse}->query_approxidate($self->{lse}->git,
- $self->{mset_opt}->{qstr});
- _start_query($self);
- };
- $self->{mset_opt}->{qstr} .= $_[1];
- } else {
- $self->fail("error reading stdin: $!");
- }
+ my ($lei) = @_; # $_[1] = $rbuf
+ $_[1] // $lei->fail("error reading stdin: $!");
+ return $lei->{mset_opt}->{qstr} .= $_[1] if $_[1] ne '';
+ eval {
+ $lei->fchdir;
+ $lei->{mset_opt}->{q_raw} = $lei->{mset_opt}->{qstr};
+ $lei->{lse}->query_approxidate($lei->{lse}->git,
+ $lei->{mset_opt}->{qstr});
+ _start_query($lei);
+ };
+ $lei->fail($@) if $@;
}
sub lxs_prepare {
diff --git a/lib/PublicInbox/LeiXSearch.pm b/lib/PublicInbox/LeiXSearch.pm
index 4aa2a81c0025..8ab84b15c00b 100644
--- a/lib/PublicInbox/LeiXSearch.pm
+++ b/lib/PublicInbox/LeiXSearch.pm
@@ -460,10 +460,11 @@ sub do_post_augment {
my ($lei) = @_;
local $PublicInbox::LEI::current_lei = $lei;
my $l2m = $lei->{l2m} or return; # client disconnected
- $lei->fchdir or return;
- my $err;
- eval { $l2m->post_augment($lei) };
- $err = $@;
+ eval {
+ $lei->fchdir;
+ $l2m->post_augment($lei);
+ };
+ my $err = $@;
if ($err) {
if (my $lxs = delete $lei->{lxs}) {
$lxs->wq_kill('-TERM');
^ permalink raw reply related [relevance 7%]
* [PATCH 00/16] some yak-shaving and annoyance fixes
@ 2021-10-16 1:00 7% Eric Wong
2021-10-16 1:00 7% ` [PATCH 07/12] lei: more eval guards for die on failure Eric Wong
0 siblings, 1 reply; 2+ results
From: Eric Wong @ 2021-10-16 1:00 UTC (permalink / raw)
To: meta
Hopefully having less code will make bug-hunting easier and
and more robust in the future at handling failures and
unexpected interrupts (e.g. Ctrl-C).
There's a lot of YAGNI elimination and more to come.
Eric Wong (12):
smsg: add ->oidbin method
dir_idle: do not add watches in ->new
imapd+nntpd: drop timer-based expiration
httpd: move pipeline logic into event_step
lei: golf PATH2CFG cleanup
lei: always keep cwd fd {3} for ->fchdir
lei: more eval guards for die on failure
extindex: prune invalid alternate entries on --gc
lei_overview: die rather than lei->fail
lei_to_mail: quiet down abort messages
inbox + search: use 5.10.1 and do some golfing
httpd/async: switch to level-triggered epoll
Documentation/technical/ds.txt | 3 +-
lib/PublicInbox/DS.pm | 37 +------------------
lib/PublicInbox/Daemon.pm | 8 ++---
lib/PublicInbox/DirIdle.pm | 8 +----
lib/PublicInbox/ExtSearch.pm | 2 +-
lib/PublicInbox/ExtSearchIdx.pm | 19 +++++-----
lib/PublicInbox/HTTP.pm | 64 +++++++++------------------------
lib/PublicInbox/HTTPD/Async.pm | 16 +++------
lib/PublicInbox/IMAP.pm | 17 +++------
lib/PublicInbox/Import.pm | 2 +-
lib/PublicInbox/Inbox.pm | 11 +++---
lib/PublicInbox/LEI.pm | 29 +++++++--------
lib/PublicInbox/LeiLcat.pm | 21 +++++------
lib/PublicInbox/LeiOverview.pm | 24 ++++++-------
lib/PublicInbox/LeiQuery.pm | 24 ++++++-------
lib/PublicInbox/LeiToMail.pm | 1 +
lib/PublicInbox/LeiXSearch.pm | 9 ++---
lib/PublicInbox/MultiGit.pm | 6 +++-
lib/PublicInbox/NNTP.pm | 12 ++-----
lib/PublicInbox/OverIdx.pm | 3 +-
lib/PublicInbox/Qspawn.pm | 1 -
lib/PublicInbox/Search.pm | 7 ++--
lib/PublicInbox/Smsg.pm | 6 ++--
lib/PublicInbox/Watch.pm | 3 +-
t/dir_idle.t | 3 +-
25 files changed, 118 insertions(+), 218 deletions(-)
^ permalink raw reply [relevance 7%]
Results 1-2 of 2 | reverse | options above
-- pct% links below jump to the message on this page, permalinks otherwise --
2021-10-16 1:00 7% [PATCH 00/16] some yak-shaving and annoyance fixes Eric Wong
2021-10-16 1:00 7% ` [PATCH 07/12] lei: more eval guards for die on failure Eric Wong
Code repositories for project(s) associated with this public inbox
https://80x24.org/public-inbox.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).