our @EXPORT_OK = qw(now msg_more);
use warnings;
use 5.010_001;
+use Scalar::Util qw(blessed);
use PublicInbox::Syscall qw(:epoll);
+use PublicInbox::Tmpfile;
use fields ('sock', # underlying socket
'rbuf', # scalarref, usually undef
'wbuf_off', # offset into first element of wbuf to start writing at
);
-use Errno qw(EAGAIN EINVAL EEXIST);
+use Errno qw(EAGAIN EINVAL);
use Carp qw(croak confess carp);
require File::Spec;
-my $nextq = []; # queue for next_tick
-my $WaitPids = []; # list of [ pid, callback, callback_arg ]
+my $nextq; # queue for next_tick
+my $WaitPids; # list of [ pid, callback, callback_arg ]
my $reap_timer;
our (
%DescriptorMap, # fd (num) -> PublicInbox::DS object
$LoopTimeout, # timeout of event loop in milliseconds
$DoneInit, # if we've done the one-time module init yet
@Timers, # timers
+ $in_loop,
);
Reset();
=cut
sub Reset {
%DescriptorMap = ();
+ $nextq = [];
$WaitPids = [];
$reap_timer = undef;
@ToClose = ();
my $q = $nextq;
$nextq = [];
for (@$q) {
- if (ref($_) eq 'CODE') {
- $_->();
- } else {
+ # we avoid "ref" on blessed refs to workaround a Perl 5.16.3 leak:
+ # https://rt.perl.org/Public/Bug/Display.html?id=114340
+ if (blessed($_)) {
$_->event_step;
+ } else {
+ $_->();
}
}
}
sub enqueue_reap ($) { push @$nextq, \&reap_pids };
sub EpollEventLoop {
- local $SIG{CHLD} = \&enqueue_reap;
+ local $in_loop = 1;
while (1) {
my @events;
my $i;
# PerlIO::mmap or PerlIO::scalar if needed
sub tmpio ($$$) {
my ($self, $bref, $off) = @_;
- my $fh; # open(my $fh, '+>>', undef) doesn't set O_APPEND
- do {
- my $fn = File::Spec->tmpdir . '/wbuf-' . rand;
- if (sysopen($fh, $fn, O_RDWR|O_CREAT|O_EXCL|O_APPEND, 0600)) { # likely
- unlink($fn) or return drop($self, "unlink($fn) $!");
- } elsif ($! != EEXIST) { # EMFILE/ENFILE/ENOSPC/ENOMEM
- return drop($self, "open: $!");
- }
- } until (defined $fh);
+ my $fh = tmpfile('wbuf', $self->{sock}, 1) or
+ return drop($self, "tmpfile $!");
$fh->autoflush(1);
my $len = bytes::length($$bref) - $off;
$fh->write($$bref, $len, $off) or return drop($self, "write ($len): $!");
# must be called with eval, PublicInbox::DS may not be loaded (see t/qspawn.t)
sub dwaitpid ($$$) {
my ($pid, $cb, $arg) = @_;
- my $chld = $SIG{CHLD};
- if (defined($chld) && $chld eq \&enqueue_reap) {
+ if ($in_loop) {
push @$WaitPids, [ $pid, $cb, $arg ];
# We could've just missed our SIGCHLD, cover it, here: