From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.2 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id A7F261F461 for ; Tue, 17 Oct 2023 10:11:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1697537467; bh=Mfj4Xw4FtG2CWkUPuvMkGfM5G/gJ6MGnc+PHal9v4ls=; h=From:To:Subject:Date:In-Reply-To:References:From; b=JMkXpXjgCHTBRiuhbil2K12oZ1UKrXoMsk4hox3bTWQNJQKIj1RH82Xncymaw0jS6 EvAFErkkwT4rv4qgVGJXPFPb5XnU91v7ALg5g05f7jpVoTw8vDlhihgSKlk/2mD79b C/pdp2Vw67ggnEOYN2D+U8SOPwzCBjIebaZQoYps= From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 2/3] input_pipe: improve error handling Date: Tue, 17 Oct 2023 10:11:05 +0000 Message-Id: <20231017101106.582556-3-e@80x24.org> In-Reply-To: <20231017101106.582556-1-e@80x24.org> References: <20231017101106.582556-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: Ensure the callback is always guarded by `eval' to catch exceptions and to force a ->close (EPOLL_CTL_DEL). We also don't want to blindly set O_NONBLOCK on TTYs since their O_NONBLOCK semantics aren't well-defined by POSIX. We can also drop EPOLLET (edge-triggered) use to reduce the need to make ->requeue calls on our end. --- lib/PublicInbox/InputPipe.pm | 48 ++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/lib/PublicInbox/InputPipe.pm b/lib/PublicInbox/InputPipe.pm index 60a9f01f..39aefab2 100644 --- a/lib/PublicInbox/InputPipe.pm +++ b/lib/PublicInbox/InputPipe.pm @@ -1,35 +1,51 @@ # Copyright (C) all contributors # License: AGPL-3.0+ -# for reading pipes and sockets off the DS event loop +# for reading pipes, sockets, and TTYs off the DS event loop package PublicInbox::InputPipe; use v5.12; use parent qw(PublicInbox::DS); -use PublicInbox::Syscall qw(EPOLLIN EPOLLET); +use PublicInbox::Syscall qw(EPOLLIN); sub consume { my ($in, $cb, @args) = @_; my $self = bless { cb => $cb, args => \@args }, __PACKAGE__; - eval { $self->SUPER::new($in, EPOLLIN|EPOLLET) }; - return $self->requeue if $@; # regular file - $in->blocking(0); # pipe or socket + eval { $self->SUPER::new($in, EPOLLIN) }; + if ($@) { # regular file (but not w/ select|IO::Poll backends) + $self->{-need_rq} = 1; + $self->requeue; + } elsif (-p $in || -s _) { # O_NONBLOCK for sockets and pipes + $in->blocking(0); + } # TODO: tty +} + +sub close { + my ($self) = @_; + $self->{-need_rq} ? delete($self->{sock}) : $self->SUPER::close } sub event_step { my ($self) = @_; my $r = sysread($self->{sock} // return, my $rbuf, 65536); - if ($r) { - $self->{cb}->(@{$self->{args}}, $rbuf); - return $self->requeue; # may be regular file or pipe - } - if (defined($r)) { # EOF - $self->{cb}->(@{$self->{args}}, ''); - } elsif ($!{EAGAIN}) { - return; - } else { # another error - $self->{cb}->(@{$self->{args}}, undef) + eval { + if ($r) { + $self->{cb}->(@{$self->{args}}, $rbuf); + $self->requeue if $self->{-need_rq}; + } elsif (defined($r)) { # EOF + $self->{cb}->(@{$self->{args}}, ''); + $self->close + } elsif ($!{EAGAIN}) { # rely on EPOLLIN + } elsif ($!{EINTR}) { # rely on EPOLLIN for sockets/pipes/tty + $self->requeue if $self->{-need_rq}; + } else { # another error + $self->{cb}->(@{$self->{args}}, undef); + $self->close; + } + }; + if ($@) { + warn "E: $@"; + $self->close; } - $self->{sock}->blocking ? delete($self->{sock}) : $self->close } 1;