[Catalyst] Non Blocking File Streaming - write_fh

neil.lunn neil at mylunn.id.au
Fri Dec 6 07:54:50 GMT 2013


Hi all, and mostly John,

I was just browsing through the upcoming Advent code examples when I saw 
the example for this had a bug. The code will die horribly when a call 
is made to HEAD rather than get as the output filehandle gets closed. 
The die is from an uncaught exception on $self->write.

My patch to MyApp::Stream :

sub read_chunk {
   my ($self, $fh, $offset) = @_;
   my $buffer = '';
   aio_read $fh, $offset, 65536, $buffer, 0, sub {
     my $status = shift;
     die "read error[$status]: $!" unless $status >= 0;
     if($status) {
       eval {
         $self->write($buffer);
       };
       if ($@) {
         warn "Cannot write, probably a closed pipe";
       }
       $self->read_chunk($fh, ($offset + 65536));
     } else {
       $self->close;
       aio_close $fh, sub { };
     }
   }
}

So the eval block and the warn, not die as this is not being caught 
under any other exception handler. Real world, YMMV. Not elegant, but 
would probably be better for passing in context.

Also I have a full Advent style writeup of the issue Bill brought up 
recently with explainations and solutions down to making the handle DWIW 
for for transparent handing over to Catalyst. If that is wanted/needed :)

Neil


---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com




More information about the Catalyst mailing list