+sub _wq_done_wait { # dwaitpid callback
+ my ($arg, $pid) = @_;
+ my ($wq, $lei) = @$arg;
+ $lei->child_error($?, 'auth failure') if $?
+}
+
+no warnings 'once';
+*ipc_atfork_child = \&PublicInbox::LeiInput::input_only_atfork_child;
+
+package PublicInbox::LeiUp1; # for redispatch_all
+use strict;
+use v5.10.1;
+
+sub nxt ($$$) {
+ my ($lei, $out, $op_p) = @_;
+ bless { lei => $lei, out => $out, op_p => $op_p }, __PACKAGE__;
+}
+
+sub event_step { # runs via PublicInbox::DS::requeue
+ my ($self) = @_;
+ my $lei = $self->{lei}; # the original, from lei_up
+ my $l = bless { %$lei }, ref($lei); # per-output copy
+ delete($l->{sock}) or return; # client disconnected if {sock} is gone
+ $l->{opt} = { %{$l->{opt}} }; # deep copy
+ delete $l->{opt}->{all};
+ $l->qerr("# updating $self->{out}");
+ my $o = " (output: $self->{out})"; # add to all warnings
+ my $cb = $SIG{__WARN__} // \&CORE::warn;
+ local $SIG{__WARN__} = sub {
+ my @m = @_;
+ push(@m, $o) if !@m || $m[-1] !~ s/\n\z/$o\n/;
+ $cb->(@m);
+ };
+ $l->{-up1} = $self; # for LeiUp1->DESTROY
+ delete @$l{qw(-socks -event_init_done)};
+ my ($op_c, $op_p) = PublicInbox::PktOp->pair;
+ $self->{unref_on_destroy} = $op_c->{sock}; # to cleanup $lei->{-socks}
+ $lei->pkt_ops($op_c->{ops} //= {}); # errors from $l -> script/lei
+ push @{$lei->{-socks}}, $op_c->{sock}; # script/lei signals to $l
+ $l->{sock} = $op_p->{op_p}; # receive signals from op_c->{sock}
+ $op_c = $op_p = undef;
+
+ eval { $l->dispatch('up', $self->{out}) };
+ $lei->child_error(0, $@) if $@ || $l->{failed}; # lei->fail()
+}
+
+sub DESTROY {
+ my ($self) = @_;
+ my $lei = $self->{lei}; # the original, from lei_up
+ return if $lei->{daemon_pid} != $$;
+ my $sock = delete $self->{unref_on_destroy};
+ my $s = $lei->{-socks} // [];
+ @$s = grep { $_ != $sock } @$s;
+ my $out = shift(@{$lei->{-upq}}) or return;
+ PublicInbox::DS::requeue(nxt($lei, $out, $self->{op_p}));
+}
+