View Single Post
Old 03-03-2011, 09:38 PM  
blackmonsters
Making PHP work
 
blackmonsters's Avatar
 
Industry Role:
Join Date: Nov 2002
Location: 🌎🌅🌈🌇
Posts: 20,753
Quote:
Originally Posted by CCBill Paul View Post
Please contact me. I would like to look into this for you. Thanks.
I can see a possible issue that may cause the problem.

In Perl, file locking works better if you get a lock on another file instead
of the real file.

example :

open(AA, ">lock$filename");
flock(AA,2) or die;

open(BB, ">$filename");
print BB $something;
close(BB);

flock(AA,8) or die;


* Instead of "die" you can write an error routine(maybe sleep 1 then try again)

The above is better than this below :

open(BB, ">$filename");
flock(BB,2);
print BB $something;
flock(BB,8);
close(BB);


This causes a problem when two surfers hit the script at the same time because
one of the surfers will get "file locked". BUTTTTT!!! Perl will not wait for the "unlock"
and prints nothing to the file.




I would say that the CCBill script probably gets a "file already locked" but does not
die or perform error routine or wait. The script continues on but cannot write to
the file.

The script opens the passwd file as "read/write" and this is the only reason that
the entire passwrd file is not erased when you have this problem.

That's why in my first example I get a lock on a different file named "lock$filename";
that way I don't delete the file when it is opened to write but the lock failed.




In CCBill script maybe a change like this will stop that :

sub lock($) {
no strict "vars";
my $fh = shift;
flock($fh, $LOCK_EX);
}


Change above to this :


sub lock($) {
no strict "vars";
my $fh = shift;
flock($fh, $LOCK_EX) or &pleasewait($);
}

sub pleasewait($) {
sleep(1);
no strict "vars";
my $fh = shift;
flock($fh, $LOCK_EX) or &pleasewait;
}


Disclaimer : If all hell breaks loose then I was not here.



Somebody test this, I have other stuff to do right now.
blackmonsters is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote