Long write + client disconnect = unhandled exception in callback #36

Open
dallaylaen opened this Issue Jun 26, 2013 · 0 comments

1 participant

@dallaylaen

Hello,

This is a duplicate of issue in RT https://rt.cpan.org/Public/Bug/Display.html?id=78177

I've been testing Twiggy with long-lasting queries and found out that it can be crashed/confused (depending on OS and event loop engine used) by suddenly closing connection on the client side.

Here's a sample PSGI app that reliably reproduces this situation. It outputs 12345 in 5 seconds:

#!/usr/bin/perl -w

use strict;
use AnyEvent;

my $app = sub {
    my $env = shift;
    return sub {
        my $responder = shift;
        my $writer = $responder->([200, [ 'Content-Type' => 'text/plain' ]]);

        # set up timer 
        my $tm;
        my $stage;
        $tm = AnyEvent->timer( after => 1, interval => 1, cb => sub {
            if (++$stage<=5) { # print 1..5 
                $writer->write($stage);
            } else { # stop here 
                $writer->close;
                $tm = undef;
            };
        });
    };
};

After running twiggy 12345.psgi, the event can be triggered by this one-liner:

perl -we 'alarm 1; use LWP::UserAgent; my $ag = LWP::UserAgent->new; $ag->get("http://127.0.0.1:5000/");

Here's error message from my Linux box:

EV: error in callback (ignoring): AnyEvent::Handle uncaught error: Broken pipe at lib/Twiggy/Server.pm line 601

It looks like the problem can be solved by setting on_error on Twiggy::Writer's handle. However, I'm not sure where to set it (in constructor maybe?) so I'm not proposing a patch right away.

@ISim ISim pushed a commit to ISim/Twiggy that referenced this issue Apr 16, 2015
Ivan Simonik Issue #36 - Twiggy died when client close connection
* installing on_error handler
* ignoring $writer->write() when handler is closed
* added test  t/anyevent_closed_streaming_async.t
70abba0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment