]> Sergey Matveev's repositories - public-inbox.git/commitdiff
httpd/async: prevent circular reference
authorEric Wong <e@80x24.org>
Fri, 27 May 2016 05:59:14 +0000 (05:59 +0000)
committerEric Wong <e@80x24.org>
Fri, 27 May 2016 06:01:52 +0000 (06:01 +0000)
We must avoid circular references which can cause leaks in
long-running processes.  This callback is dangerous since
it may never be called to properly terminate everything.

lib/PublicInbox/HTTPD/Async.pm

index 47ba27d2e42979a2594aa23829ee0e02582be092..b00e63701495a73635874e702e16addb62985d6b 100644 (file)
@@ -10,6 +10,7 @@ use strict;
 use warnings;
 use base qw(Danga::Socket);
 use fields qw(cb cleanup);
+use Scalar::Util qw(weaken);
 require PublicInbox::EvCleanup;
 
 sub new {
@@ -25,11 +26,12 @@ sub new {
 
 sub async_pass {
        my ($self, $io, $fh, $bref) = @_;
-       my $restart_read = sub { $self->watch_read(1) };
        # In case the client HTTP connection ($io) dies, it
        # will automatically close this ($self) object.
        $io->{forward} = $self;
        $fh->write($$bref);
+       weaken($self);
+       my $restart_read = sub { $self->watch_read(1) };
        $self->{cb} = sub {
                my $r = sysread($self->{sock}, $$bref, 8192);
                if ($r) {