You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Taras Yurij Vasylovitch <yu...@softsky.com.ua> on 2005/09/22 15:11:35 UTC

handler object: data sometimes resetting

Hello all. 
I have a problem in writing mutlithread-safe application on mod_perl. 
I have a file main.pm which is my object implementation:

package admin::main;
1;
package svntrac::admin;

use ..........
..............

print STDERR "I am require\n";
$Apache2::PerlSections::Save = 1;
$handlers::svntrac = svntrac::admin->new(
#config here
);


sub new
{
	my $class = shift;
	my $self = {config => {@_}};
	bless $self, $class;
	print STDERR "SVN DAV + Trac ADMIN: Starting up...\n";
###
# Some additional object initialization
###
	$self->{'edited'} = 0;
	$self->{'creation'} = time();
	return $self;
}


sub list : method
{
	my $self = shift;
	my $r = shift;
# What do we have in our flag??
	warn "Edited:".$self->{'edited'};
	$self->{'edited'} += 1;
	$r->content_type ('text/plain');
	$r->print ("HELLO!!!\n");
	$r->print ("Locking TestRepo\n");
	$r->print ('Writing in file during 5secs +-1minute '.
		time()."\n");

	sleep 5; # Doing something......
	$r->print('End of writing'.time()."\n");
	return Apache2::Const::OK;
};

__END__

Including it from Apache2:
PerlRequire				/home/yura/mod_perl/trac/admin/main.pm
# Creating instance of the object

<Location /projects/tracadmin>
	SetHandler				modperl
	PerlResponseHandler			$handlers::svntrac->list
</Location>


Looks good, isn't it?

And now i'll try to open this page (localhost/projects/tracadmin) in the
same time in 3 browser windows. After many times of "Refresh" hitting i
have such log:
Edited:0 at /home/yura/mod_perl/trac/admin/main.pm line 393. <-- good..
Edited:0 at /home/yura/mod_perl/trac/admin/main.pm line 393. <-- what's
that?
Edited:0 at /home/yura/mod_perl/trac/admin/main.pm line 393. <-- and
that???
Edited:1 at /home/yura/mod_perl/trac/admin/main.pm line 393. <-- ok,
going right...
Edited:2 at /home/yura/mod_perl/trac/admin/main.pm line 393.
Edited:3 at /home/yura/mod_perl/trac/admin/main.pm line 393.
Edited:4 at /home/yura/mod_perl/trac/admin/main.pm line 393.
Edited:5 at /home/yura/mod_perl/trac/admin/main.pm line 393.
Edited:6 at /home/yura/mod_perl/trac/admin/main.pm line 393.
Edited:7 at /home/yura/mod_perl/trac/admin/main.pm line 393.
Edited:0 at /home/yura/mod_perl/trac/admin/main.pm line 393. <-- hey,
why counter is 0 again???
Edited:1 at /home/yura/mod_perl/trac/admin/main.pm line 393.
Edited:2 at /home/yura/mod_perl/trac/admin/main.pm line 393.
Edited:3 at /home/yura/mod_perl/trac/admin/main.pm line 393.
Edited:4 at /home/yura/mod_perl/trac/admin/main.pm line 393.
Edited:5 at /home/yura/mod_perl/trac/admin/main.pm line 393.
Edited:6 at /home/yura/mod_perl/trac/admin/main.pm line 393.
Edited:7 at /home/yura/mod_perl/trac/admin/main.pm line 393.
Edited:8 at /home/yura/mod_perl/trac/admin/main.pm line 393.
Edited:9 at /home/yura/mod_perl/trac/admin/main.pm line 393.
Edited:10 at /home/yura/mod_perl/trac/admin/main.pm line 393.
Edited:11 at /home/yura/mod_perl/trac/admin/main.pm line 393.
Edited:12 at /home/yura/mod_perl/trac/admin/main.pm line 393. 

'edited' field it's just an example, a lot of variables somewhy gets their
initial values, but constructor is called only once, on Apache start...
Also i've been checking 'creation' field and address of this object(by
including in list() such strings: 
warn "Created: ".$self->{'creation'}; 
warn "Obj ref: ".$self;
), and it always prints out the same creation time and address, so it's
the same object... I can't imagine, why 'edited' field is falling to 0,
can you?

My Apache2 and mod_perl version:
$ ./httpd2_mod_perl -v
Server version: Apache/2.0.54
Server built:   Sep 20 2005 17:44:36

mod_perl: 2.0.1

Thanks for answer.


Re: handler object: data sometimes resetting

Posted by Damyan Ivanov <di...@creditreform.bg>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Taras Yurij Vasylovitch wrote:
> And now i'll try to open this page (localhost/projects/tracadmin) in the
> same time in 3 browser windows. After many times of "Refresh" hitting i
> have such log:
> Edited:0 at /home/yura/mod_perl/trac/admin/main.pm line 393. <-- good..
> Edited:0 at /home/yura/mod_perl/trac/admin/main.pm line 393. <-- what's
> that?
> Edited:0 at /home/yura/mod_perl/trac/admin/main.pm line 393. <-- and
> that???
> Edited:1 at /home/yura/mod_perl/trac/admin/main.pm line 393. <-- ok,
> going right...
> Edited:2 at /home/yura/mod_perl/trac/admin/main.pm line 393.
> Edited:3 at /home/yura/mod_perl/trac/admin/main.pm line 393.

A wild guess: Can this be indication that you're using prefork MPM? In this
case it would be normal - each process has its own instance of Edited and
starts incrementing it from zero.

Adding $$ to the debug print above could help diagnosing this.

Whether Edited is shared between threads in threaded MPMs is beyond my knowledge.

dam
- --
Damyan Ivanov          0x9725F63B          Creditreform Bulgaria
divanov@creditreform.bg              http://www.creditreform.bg/
phone: +359(2)928-2611, 929-3993            fax: +359(2)920-0994
mob. +359(88)856-6067  ICQ 3028500  dam@jabber.minus273.org/Gaim
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFDOPArHqjlqpcl9jsRAu1gAJ0UBzCGYi4fOcbYGb7hNXej3erh7ACeNvgw
zCHPZjx8tfmdwNnnVfunTlc=
=2MLW
-----END PGP SIGNATURE-----

RE: handler object: data sometimes resetting

Posted by Frank Maas <fr...@cheiron-it.nl>.
> Thanks, Frank, thanks, Damyan, for your answers.
> 
> I've add testing info to my script, as you recommend:
> 
> Editing: [2612]8068957.60529976$self current=0
> Editing: [2613]8068957.60529976$self current=0
> Editing: [2614]8068957.60529976$self current=0
> Editing: [2614]8068957.60529976$self current=1
> Editing: [2614]8068957.60529976$self current=2
> 
> So, you were right, it's different processes. So, i have more
> questions about it: 
> 
> 1) Why the address of object is the same, but it has different
> values? I can't imagine, how this could be.. Where can i find more
> info about it? 

The address that is returned is an address in the memory space of 
the running perl interpreter. If you start two interpreters with the
same process, then there is a very good chance that the address is
the same. The address is not an absolute address pointing towards a
shared memory location!

> 2) How can i avoid such problem (i mean not
> reconfigure apache to use 1 process, just write correct Perl script
> to store data between processes). I want, as example, "edited" field
> to be increasing on each request regardless of the process which
> handle this request. 

What you are looking for is sharing data between processes. Using
Google or the archives of this mailinglist 
(http://marc.theaimsgroup.com/?l=apache-modperl&r=1&w=2#apache-modperl)
should give you enough information. I've seen proposed solutions with 
memcached, IPC, a special session, some kind of shared memory, a database 
table and more.

Good luck!

Frank

RE: handler object: data sometimes resetting

Posted by Taras Yurij Vasylovitch <yu...@softsky.com.ua>.
Thanks, Frank, thanks, Damyan, for your answers.

I've add testing info to my script, as you recommend:

Editing: [2612]8068957.60529976$self current=0
Editing: [2613]8068957.60529976$self current=0
Editing: [2614]8068957.60529976$self current=0
Editing: [2614]8068957.60529976$self current=1
Editing: [2614]8068957.60529976$self current=2

So, you were right, it's different processes. So, i have more questions
about it:

1) Why the address of object is the same, but it has different values? I
can't imagine, how this could be.. Where can i find more info about it?
2) How can i avoid such problem (i mean not reconfigure apache to use 1
process, just write correct Perl script to store data between
processes). I want, as example, "edited" field to be increasing on each
request regardless of the process which handle this request. 
Thanks for all information about this theme.


RE: handler object: data sometimes resetting

Posted by Frank Maas <fr...@cheiron-it.nl>.
Hi,

I think you've just come accross the fact that in a normal setup there is
more than one httpd process (and thus more than one perl interpreter) that
is serving your requests. As each process starts at the same time and each
perl interpreter has its own memory space, the fact that both creation
time and object address are the same is not so strange.

If you alter the code you supplied as below, you probably see the differences.

Regards,

Frank

[..snip..]

> 	print STDERR "SVN DAV + Trac ADMIN: Starting up...\n"; ###
> # Some additional object initialization
> ###
> 	$self->{'edited'} = 0;
> 	$self->{'creation'} = time();

add
  $self->{'check_id'} = rand(999999);
  warn "Created: [$$] ".$self->{'check_id'}."/$self at ".$self->{'creation'}."\n";

> 	return $self;

[..snip..]

> # What do we have in our flag??
> 	warn "Edited:".$self->{'edited'};

change into
  warn "Editing: [$$]".$self->{'check_id'}."/$self current=".$self->{'edited'}."\n";

> 	$self->{'edited'} += 1;

[..snip..]