in reply to [solved] plack: close filehandle responsibility

$image->filehandle('<', ':raw') is opening the file. And it is in your code. So, your code needs to close it.

Not elegant (nor tested), but the following should work:

my $ih = $image->filehandle('<', ':raw'); my $ret = $self->res->set_content_type('image/x-icon') ->render_binary($ih); close $ih; return $ret;

Update: Rethinking this, the handle created by $image->filehandle and passed to render_binary() is being stored in some object. So, when that object gets destroyed, the handle should be automatically closed.

Replies are listed 'Best First'.
Re^2: plack: close filehandle responsibility
by Anonymous Monk on May 07, 2015 at 01:51 UTC

    Yes, that would close the filehandle before the file was sent, so no file would be sent

    See Re^2: plack: close filehandle responsibility (everybody), while https://metacpan.org/pod/Plack::Response#body doesn't document that it closes $io the handle it accepts as argument, it does do it

    Try it out, fireup this plack program, and start taskmanager, and watch the handles count not increase with each browser request you make

    #!/usr/bin/perl -- use strict; use warnings; use Plack::Builder; use Path::Tiny qw/ path /; my $app = sub { return [ 200, [ 'Content-Type' => 'text/plain; charset=UTF-8', ], path( __FILE__ )->openr_raw, ]; }; my $finalapp = builder { enable 'StackTrace'; $app; }; if( $0 eq __FILE__ ){ require Plack::Runner; my $runner = Plack::Runner->new; $runner->parse_options( qw' --host 127.0.0.1 --port 5000 ' ); $runner->run( $finalapp ); ## perl this.psgi } else { $finalapp; ## plackup -l localhost:5000 this.psgi } ## lwp-request -Ed http://127.0.0.1:5000/

    If I close the handle openr_raw returns before Plack reads it without slurping, there will probably be a plack error

    update: I just tested it, no error, but no file is sent either as expected

      I don't see where your code (in your response to my post) is calling render_binary().

        RonW: I don't see where your code (in your response to my post) is calling render_binary().

        And then ? Why do you think this matters?