Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Plack::Handler::Apache2: preserve a copy of %ENV for cleanup handlers

For reasons unknown we'll *sometimes* have an emptied out %ENV by the
time we run the cleanup phase, I can reproduce this by requesting any
URL in my application a few times.

With this patch applied to Plack::Handler::Apache2:

    @@ -90,9 +92,12 @@ sub call_app {
             die "Bad response $res";
         }

    +    print STDERR "Have done everything, have the env = " . Data::Dumper::Dumper(\%ENV);
    +
         if (@{ $env->{'psgix.cleanup.handlers'} }) {
             $r->push_handlers(
                 PerlCleanupHandler => sub {
    +                print STDERR "Running the cleanup handler, have the env = " . Data::Dumper::Dumper(\%ENV);
                     for my $cleanup_handler (@{ $env->{'psgix.cleanup.handlers'} }) {
                         $cleanup_handler->($env);
                     }

I'll always get the full %ENV when I dump it in "Have done
everything", but by the time I get to running the cleanup handlers
it's sometimes been completely emptied.

Work around this by preserving %ENV and restoring it if it gets lost,
this helps a lot with migrating existing Apache applications which
look at %ENV, and since we normally set %ENV with the "subprocess_env"
cal it's really confusing to only provide it some of the time.
  • Loading branch information...
commit 0b1bc01a5321c6d47786e479490f9fdead7e91b3 1 parent 5eba0ce
@avar authored
Showing with 29 additions and 6 deletions.
  1. +29 −6 lib/Plack/Handler/Apache2.pm
View
35 lib/Plack/Handler/Apache2.pm
@@ -91,14 +91,37 @@ sub call_app {
}
if (@{ $env->{'psgix.cleanup.handlers'} }) {
+ my %ENV_copy_for_PerlCleanupHandler = %ENV;
+ # This is referred to in the comment below
$r->push_handlers(
PerlCleanupHandler => sub {
- for my $cleanup_handler (@{ $env->{'psgix.cleanup.handlers'} }) {
- $cleanup_handler->($env);
- }
-
- if ($env->{'psgix.harakiri.commit'}) {
- $r->child_terminate;
+ my $do_cleanup_handlers = sub {
+ for my $cleanup_handler (@{ $env->{'psgix.cleanup.handlers'} }) {
+ $cleanup_handler->($env);
+ }
+
+ if ($env->{'psgix.harakiri.commit'}) {
+ $r->child_terminate;
+ }
+
+ return;
+ };
+
+ if (keys %ENV) {
+ $do_cleanup_handlers->();
+ } else {
+ # Sometimes Apache will seemingly empty out %ENV,
+ # i.e. if we were to dump out %ENV when we assign to
+ # %ENV_copy_for_PerlCleanupHandler above we'd get a
+ # populated %ENV, but it'll be empty by the time we
+ # get here.
+ #
+ # I haven't tracked down *why* this happens, so this
+ # might be cargo-culting around something, but this
+ # workaround works for me and isn't too expensive.
+ local %ENV = %ENV_copy_for_PerlCleanupHandler;
+
+ $do_cleanup_handlers->();
}
},
);
Please sign in to comment.
Something went wrong with that request. Please try again.