user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 2/3] http: avoid circular reference for getline responses
Date: Fri, 27 May 2016 06:17:37 +0000	[thread overview]
Message-ID: <20160527061738.21132-3-e@80x24.org> (raw)
In-Reply-To: <20160527061738.21132-1-e@80x24.org>

Lightly tested, this seems to work when mass-aborting
responses.  Will still need to automate the testing...
---
 lib/PublicInbox/HTTP.pm | 45 ++++++++++++++++++++++++++++-----------------
 1 file changed, 28 insertions(+), 17 deletions(-)

diff --git a/lib/PublicInbox/HTTP.pm b/lib/PublicInbox/HTTP.pm
index 6aae5c8..0454f60 100644
--- a/lib/PublicInbox/HTTP.pm
+++ b/lib/PublicInbox/HTTP.pm
@@ -11,11 +11,12 @@ package PublicInbox::HTTP;
 use strict;
 use warnings;
 use base qw(Danga::Socket);
-use fields qw(httpd env rbuf input_left remote_addr remote_port forward);
+use fields qw(httpd env rbuf input_left remote_addr remote_port forward pull);
 use Fcntl qw(:seek);
 use Plack::HTTPParser qw(parse_http_request); # XS or pure Perl
 use HTTP::Status qw(status_message);
 use HTTP::Date qw(time2str);
+use Scalar::Util qw(weaken);
 use IO::File;
 use constant {
 	CHUNK_START => -1,   # [a-f0-9]+\r\n
@@ -255,6 +256,28 @@ sub response_done ($$) {
 	}
 }
 
+sub getline_response {
+	my ($self, $body, $write, $close) = @_;
+	$self->{forward} = $body;
+	weaken($self);
+	my $pull = $self->{pull} = sub {
+		local $/ = \8192;
+		my $forward = $self->{forward};
+		while ($forward && defined(my $buf = $forward->getline)) {
+			$write->($buf);
+			last if $self->{closed};
+			if ($self->{write_buf_size}) {
+				$self->write($self->{pull});
+				return;
+			}
+		}
+		$self->{forward} = $self->{pull} = undef;
+		$forward->close if $forward; # avoid recursion
+		$close->();
+	};
+	$pull->();
+}
+
 sub response_write {
 	my ($self, $env, $res) = @_;
 	my $alive = response_header_write($self, $env, $res);
@@ -266,21 +289,7 @@ sub response_write {
 			$write->($_) foreach @$body;
 			$close->();
 		} else {
-			my $pull;
-			$pull = sub {
-				local $/ = \8192;
-				while (defined(my $buf = $body->getline)) {
-					$write->($buf);
-					if ($self->{write_buf_size}) {
-						$self->write($pull);
-						return;
-					}
-				}
-				$pull = undef;
-				$body->close();
-				$close->();
-			};
-			$pull->();
+			getline_response($self, $body, $write, $close);
 		}
 	} else {
 		# this is returned to the calling application:
@@ -445,8 +454,10 @@ sub event_err { $_[0]->close }
 sub close {
 	my $self = shift;
 	my $forward = $self->{forward};
+	my $env = $self->{env};
+	delete $env->{'psgix.io'} if $env; # prevent circular referernces
+	$self->{pull} = $self->{forward} = $self->{env} = undef;
 	$forward->close if $forward;
-	$self->{forward} = $self->{env} = undef;
 	$self->SUPER::close(@_);
 }
 

  parent reply	other threads:[~2016-05-27  6:17 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-05-27  6:17 [PATCH 0/3] http: another round EPIPE fixes Eric Wong
2016-05-27  6:17 ` [PATCH 1/3] httpd/async: prevent circular reference Eric Wong
2016-05-27  7:32   ` [PATCH 4/3] httpd/async: do not needlessly weaken Eric Wong
2016-05-27  6:17 ` Eric Wong [this message]
2016-05-27  6:17 ` [PATCH 3/3] git-http-backend: fix aborts for generic PSGI clone Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20160527061738.21132-3-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).