You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by David Wheeler <da...@wheeler.net> on 2002/12/30 20:59:36 UTC

Modules Executed Twice

Hi All,

I'm developing a new module for mod_perl 1.27, and I'm noticing that  
some code is getting executed twice when the Apache server starts up.

Here's a simple example. Say I have two modules in separate files:

package DoubleTest;
use strict;
my $seen;
sub test_seen {
     $seen++;
     warn "Seen: $seen\n"
}
1;

package TestSeen;
use strict;
use DoubleTest;
DoubleTest::test_seen();
1;

Then I have an httpd.conf with these two lines:

PerlModule DoubleTest
PerlModule TestSeen

When I start up Apache, I see "Seen: 1" print to the terminal, and then  
I see "Seen: 2" in the error log. For some reason, TestSeen is getting  
executed twice!

I did a quick search on perl.apache.org, and found this item:

    
http://perl.apache.org/docs/1.0/guide/ 
config.html#Apache_Restarts_Twice_On_Start

However, this seems to indicate that, first, modules will be executed  
twice on restart but not on start, and second, that it doesn't affect  
PerlModule directives. What I'm seeing above doesn't seem to bear this  
out.

Thanks to that item in the guide, I did figure out how to circumvent  
the problem by checking $Apache::Server::Starting in  
DoubleTest::test_seen():

sub test_seen {
     return if $Apache::Server::Starting;
     $seen++;
     warn "Seen: $seen\n"
}

So I'm fine with this workaround, but not sure why it's necessary. I  
could also change TestSeen.pm to only call test_seen() in a BEGIN block  
or something (since the modules seems to be compiled only once, but run  
twice), but since the module I'm actually writing is the equivalent of  
DoubleTest with TestSeen as the client, I'd rather not impose that on  
the users of my module.

Explanations and other suggested approaches to handling this problem  
will be most welcome.

TIA,

David

-- 
David Wheeler                                     AIM: dwTheory
david@wheeler.net                                 ICQ: 15726394
http://david.wheeler.net/                      Yahoo!: dew7e
                                                Jabber: Theory@jabber.org


Re: Modules Executed Twice

Posted by Stas Bekman <st...@stason.org>.
David Wheeler wrote:
> On Monday, December 30, 2002, at 02:45  PM, Stas Bekman wrote:
> 
>> David Wheeler wrote:
>>
>>> Hi All,
>>> I'm developing a new module for mod_perl 1.27, and I'm noticing that  
>>> some code is getting executed twice when the Apache server starts up.
>>
>>
>> It was supposed to be fixed in 1.25_01:
>>
>> =item 1.25_01 - July 6, 2001
>> ...
>> fix double-loading bug of Perl{Require,Module}s at startup time
> 
> 
> Hrm, interesting. I wonder what the problem is?
> 
>> Indeed, that's the workaround that was added originaly to cure the 
>> above problem. Perhaps you can play with httpd_conf and see why it 
>> doesn't work for you. I know that several people have reported that 
>> they still had this problem since 1.26 was released.
> 
> 
> I have a better workaround, now. I found that I could make the problem 
> go away by reversing the order in which the modules are loaded in 
> httpd.conf:
> 
> PerlModule TestSeen
> PerlModule DoubleTest
> 
> Not exactly sure why that makes a difference, though.

Because $seen wasn't reset yet on reload.

> Is httpd_conf() documented somewhere? I can't see what's supposed to be 
> passed to it.

It's an internal thing. You can add debug prints and Carp::cluck, to see 
who calls it with what args. Hopefully someone will be able to track the 
problem down and finally cure it for good.

__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com


Re: Modules Executed Twice

Posted by David Wheeler <da...@wheeler.net>.
On Monday, December 30, 2002, at 02:45  PM, Stas Bekman wrote:

> David Wheeler wrote:
>> Hi All,
>> I'm developing a new module for mod_perl 1.27, and I'm noticing that  
>> some code is getting executed twice when the Apache server starts up.
>
> It was supposed to be fixed in 1.25_01:
>
> =item 1.25_01 - July 6, 2001
> ...
> fix double-loading bug of Perl{Require,Module}s at startup time

Hrm, interesting. I wonder what the problem is?

> Indeed, that's the workaround that was added originaly to cure the 
> above problem. Perhaps you can play with httpd_conf and see why it 
> doesn't work for you. I know that several people have reported that 
> they still had this problem since 1.26 was released.

I have a better workaround, now. I found that I could make the problem 
go away by reversing the order in which the modules are loaded in 
httpd.conf:

PerlModule TestSeen
PerlModule DoubleTest

Not exactly sure why that makes a difference, though.

Is httpd_conf() documented somewhere? I can't see what's supposed to be 
passed to it.

Thanks,

David

-- 
David Wheeler                                     AIM: dwTheory
david@wheeler.net                                 ICQ: 15726394
http://david.wheeler.net/                      Yahoo!: dew7e
                                                Jabber: Theory@jabber.org


Re: Modules Executed Twice

Posted by Stas Bekman <st...@stason.org>.
David Wheeler wrote:
> Hi All,
> 
> I'm developing a new module for mod_perl 1.27, and I'm noticing that  
> some code is getting executed twice when the Apache server starts up.

It was supposed to be fixed in 1.25_01:

=item 1.25_01 - July 6, 2001
...
fix double-loading bug of Perl{Require,Module}s at startup time

Here is the diff:

Index: Apache/Apache.pm
===================================================================
RCS file: /home/cvs/modperl/Apache/Apache.pm,v
retrieving revision 1.62
retrieving revision 1.63
diff -u -r1.62 -r1.63
--- Apache/Apache.pm    29 Jan 2001 16:07:08 -0000      1.62
+++ Apache/Apache.pm    26 Apr 2001 05:30:45 -0000      1.63
@@ -30,7 +30,10 @@

  sub httpd_conf {
      shift;
-    push @Apache::ReadConfig::PerlConfig,
+    no strict 'refs';
+    #use a symbolic reference so %Apache::ReadConfig::
+    #is empty at compile time
+    push @{"Apache::ReadConfig::PerlConfig"},
        map "$_\n", @_;
  }

[...]
> sub test_seen {
>     return if $Apache::Server::Starting;
>     $seen++;
>     warn "Seen: $seen\n"
> }

Indeed, that's the workaround that was added originaly to cure the above 
problem. Perhaps you can play with httpd_conf and see why it doesn't 
work for you. I know that several people have reported that they still 
had this problem since 1.26 was released.

__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com


Re: Modules Executed Twice

Posted by darren chamberlain <dl...@users.sourceforge.net>.
* Perrin Harkins <pe...@elem.com> [2002-12-30 19:07]:
> > Explanations and other suggested approaches to handling this problem
> > will be most welcome.
> 
> My suggestion in the past has been to PerlRequire a startup.pl that
> does a use on your modules, instead of pulling them in with
> PerlModule.

I believe that if you have custom directives, you need to PerlModule
your module -- a simple 'use Foo;' line within a PerlRequire'd script
is not sufficient.

(darren)

-- 
Do you realize how many holes there could be if people would
just take the time to take the dirt out of them?

Re: Modules Executed Twice

Posted by David Wheeler <da...@wheeler.net>.
On Monday, December 30, 2002, at 04:09  PM, Perrin Harkins wrote:

> My suggestion in the past has been to PerlRequire a startup.pl that 
> does a
> use on your modules, instead of pulling them in with PerlModule.
> Of course, if you turn PerlFreshRestart on then this is the intended
> behavior.

That's exactly what I do, too. But I want to give users the flexibility 
to do both.

Thanks,

David

-- 
David Wheeler                                     AIM: dwTheory
david@wheeler.net                                 ICQ: 15726394
http://david.wheeler.net/                      Yahoo!: dew7e
                                                Jabber: Theory@jabber.org


Re: Modules Executed Twice

Posted by Perrin Harkins <pe...@elem.com>.
> Explanations and other suggested approaches to handling this problem
> will be most welcome.

My suggestion in the past has been to PerlRequire a startup.pl that does a
use on your modules, instead of pulling them in with PerlModule.
Of course, if you turn PerlFreshRestart on then this is the intended
behavior.
- Perrin