Subject: | Localizing $ENV{MOD_PERL} in perl-script may make $ENV{MOD_PERL} 'disappear' for subsequent requests |
Given the attached EnvIssue.pm and the following Apache configuration:
<Location /foo>
SetHandler perl-script
PerlResponseHandler EnvIssue
</Location>
When I access this location for the first time, I get output like this, which is what I would expect:
PATH:
$VAR1 = '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin';
$VAR1 = undef;
$VAR1 = '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin';
MOD_PERL:
$VAR1 = 'mod_perl/2.0.8';
$VAR1 = undef;
$VAR1 = 'mod_perl/2.0.8';
However, on the second request to the same location (in the same process), I get this:
PATH:
$VAR1 = '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin';
$VAR1 = undef;
$VAR1 = '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin';
MOD_PERL:
$VAR1 = undef;
$VAR1 = undef;
$VAR1 = undef;
So I'm doing the same for the PATH and MOD_PERL environment variables, but get different results. Adding
PerlOptions -SetupEnv
to the location helps, and I get the expected result.
I first noticed this behavior in a setup with Plack::Handler::Apache2 (used for one application) and an internal framework which uses $ENV{MOD_PERL} to detect if it is running on mod_perl. Plack::Handler::Apache2 localizes $ENV{MOD_PERL} in its load_app() method, which, when combined with this (in my opinion buggy) behavior, resulted in seemingly random crashes of our applications.
Using the above configuration and the example script, I can reproduce the behavior on Debian Wheezy (mod_perl 2.0.7, Apache 2.2.22, Perl v5.14.2), Debian Testing aka Jessie (mod_perl 2.0.8, Apache 2.4.10, Perl v5.20.0) and CentOS 5 (mod_perl 2.0.4, Apache 2.2.3, Perl v5.8.8).
Subject: | EnvIssue.pm |
package EnvIssue;
use strict;
use warnings;
use Apache2::RequestRec ();
use Apache2::RequestIO ();
use Apache2::Const -compile => qw(OK);
use Data::Dumper;
my @keys = qw(PATH MOD_PERL);
sub handler {
my $r = shift;
$r->content_type('text/plain');
mp_env();
return Apache2::Const::OK;
}
sub mp_env {
for my $key (@keys) {
print $key.":\n";
print(Dumper($ENV{$key}));
do {
local $ENV{$key};
print(Dumper($ENV{$key}));
};
print(Dumper($ENV{$key}));
}
return;
}
1;