about summary refs log tree commit homepage
path: root/lib
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2021-01-03 11:24:51 +0000
committerEric Wong <e@80x24.org>2021-01-03 18:24:09 +0000
commit8ee251fcd420533c7b69f40c18f3536bcc30abaf (patch)
tree5de03f71a81452330b8fc0e308fe8bec77e2946c /lib
parentb156cb4ef176b41131154a6d7632aed92b143fa0 (diff)
downloadpublic-inbox-8ee251fcd420533c7b69f40c18f3536bcc30abaf.tar.gz
The daemon needs to flush stdout before disconnecting or killing
clients, otherwise they may reread empty data on redirected
outputs.  We also don't want to unbuffer stdout too early in
case we have lots of small chunks of data to output.

The received ($self->{2}) will always have autoflush, matching normal
STDERR behavior.
Diffstat (limited to 'lib')
-rw-r--r--lib/PublicInbox/LEI.pm6
1 files changed, 6 insertions, 0 deletions
diff --git a/lib/PublicInbox/LEI.pm b/lib/PublicInbox/LEI.pm
index 3ad5e01a..6f21da35 100644
--- a/lib/PublicInbox/LEI.pm
+++ b/lib/PublicInbox/LEI.pm
@@ -236,6 +236,7 @@ my %CONFIG_KEYS = (
 
 sub x_it ($$) { # pronounced "exit"
         my ($self, $code) = @_;
+        $self->{1}->autoflush(1); # make sure client sees stdout before exit
         if (my $sig = ($code & 127)) {
                 kill($sig, $self->{pid} // $$);
         } else {
@@ -635,6 +636,7 @@ sub accept_dispatch { # Listener {post_accept} callback
                 say $sock "timed out waiting to recv FDs";
                 return;
         }
+        $self->{2}->autoflush(1); # keep stdout buffered until x_it|DESTROY
         # $ARGV_STR = join("]\0[", @ARGV);
         # $ENV_STR = join('', map { "$_=$ENV{$_}\0" } keys %ENV);
         # $line = "$$\0\0>$ARGV_STR\0\0>$ENV_STR\0\0";
@@ -773,4 +775,8 @@ sub oneshot {
         }, __PACKAGE__), @ARGV);
 }
 
+# ensures stdout hits the FS before sock disconnects so a client
+# can immediately reread it
+sub DESTROY { $_[0]->{1}->autoflush(1) }
+
 1;