homepage Welcome to WebmasterWorld Guest from 54.211.213.10
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

    
My CGI hangs for no apparent reason
Dabrowski




msg:3722194
 4:48 pm on Aug 13, 2008 (gmt 0)

I have a script, pretty simple, just a few if this do that, then output a page.

For some reason it hangs. No errors. When I check the server's processes, it's just sitting there, 0% CPU time.

So, I stick a die in after the initalisation bits, the use lines, getting/setting session vars etc...

ok fine, program dies normally.

Move the die down the file after each section, works every time. Move it to the end, the page is displayed properly. Get rid of it again, script still works fine.

I come back another day and it's broken again.

This is really pi*sing me off. Anybody else suffered from this mystery refusal to execute?

 

perl_diver




msg:3722291
 6:42 pm on Aug 13, 2008 (gmt 0)

I have had similar problems in the past and it was always something to do with the server the script was on or my internet connection.

rocknbil




msg:3722300
 6:52 pm on Aug 13, 2008 (gmt 0)

Yeah I've had that hundreds of times. This may be trivial and might not help, but then . . . .

My "mystery hangs" started to go away (or become identifiable) when I moved from "die" to my own error routine. FatalsToBrowser should work too.

sub error {
my ($err);
$err = shift(@_);
print "content-type: text/html\n\n";
print "ERROR: $err";
exit 0;
}

There are two important things here. If some function in the program is failing and prints to STDOUT before the content-type is output, we assume it SHOULD cause a 500. It doesn't always, and I've never figured out precisely why. If further actions rely on getting a value from this function and don't, it complicates your debugging.

The other is the exit 0 in the error sub. Sometimes a process will fail and without the hook, continue running, might even overwrite what you're printing out in the error sub, leading you to look in all the wrong places.

This approach just makes it easy to hook . . EVERYTHING, and style your hooks so you know exactly where to look.

open (MAIL "$mailprog") or &error("Cannot open mail $!");

open (FILE "$file") or &error("Cannot open file $!");

$res = `some_command`;
$res = system("some_command");

if ($res > 0) { &error("cannot execute command $!"); }
Sometimes "some_command" will only return 0 on error . . .

if ($somevariable_that_is_supposed_to_have_value == 0) { &error("Var has no value in this location"); }

Dabrowski




msg:3722917
 1:43 pm on Aug 14, 2008 (gmt 0)

something to do with the server

Did you ever manage to narrow it down to a particular software/hardware?

n I moved from "die" to my own error routine

I'm not using die either, or any other premature exit routine.

I added the die to see if it would run half the script, then removed it and the whole script then runs.

This script is on the end of a form post, and during the tests I hadn't changed the input to the script either, just hit refresh on my browser.

I've had this before and I changed the form to GET and that worked, I have no idea why. I am using CGI::Simple, could it be doing something wrong with the form data and blocking on STDIN?

I've come back to it today and it's broken again. Grrrr.

rocknbil




msg:3723213
 7:09 pm on Aug 14, 2008 (gmt 0)

Did you ever manage to narrow it down to a particular software/hardware?

Sometimes, yes, sometimes no. Usually when I do find a problem it turns out to be Something That I Did. :-P Things like calling an external program incorrectly, or not providing correct switches, etc.

I've had this before and I changed the form to GET and that worked,

This is actually one of the things that demonstrated weirdness, but in your case probably not, because you're using a module to read/parse (right?) I had a REALLY small read/parse routine that was SUPPOSED to accept post, but did not, but it wouldn't 500 either. Just the white page. It was a very long time ago, can't post a specific, but hey, it's a thought. Try switching from CGI::Simple to the standard CGI module, you shouldn't have to change much.

perl_diver




msg:3723270
 8:21 pm on Aug 14, 2008 (gmt 0)

I was never able to determine the problem.

Dabrowski




msg:3723281
 8:34 pm on Aug 14, 2008 (gmt 0)

because you're using a module to read/parse (right?)

Right, I'm using CGI's param to retrieve the post data. Initially I did the same as you, I wrote my own little routine, just a couple of lines to parse the data but occasionally failed because of reading from STDIN. I had the same symptoms that you had, a white screen and hung script.

Try switching from CGI::Simple to the standard CGI module

Yeah, did that today. As I'm only using the basic functions param, redirect and header, my code needed no modification apart from the module name. Didn't seem to make any immediate difference.

I added:
BEGIN { print "content-type: text/html;\n\n"; }

The script then worked, I took it out again and it still worked. D'oh!

An interesting thing I noticed today, if I refresh the page the script hangs. If I go back and click on the submit button, it works. As long as it stays that way for now it's acceptable as my client (or any user) will most likely never notice.

Still would like to get to the bottom of this though.

[edited by: Dabrowski at 8:36 pm (utc) on Aug. 14, 2008]

[edited by: phranque at 11:13 pm (utc) on Aug. 14, 2008]
[edit reason] formatting [/edit]

perl_diver




msg:3723543
 4:41 am on Aug 15, 2008 (gmt 0)

I've come back to it today and it's broken again. Grrrr.

See, there is no way to debug something like that. It could anywhere from an overloaded shared server to the host making changes to the setup.

Dabrowski




msg:3723813
 1:53 pm on Aug 15, 2008 (gmt 0)

It could anywhere from an overloaded shared server to the host making changes to the setup.

I AM the host! I didn't change anything!

And it's not overloaded, my development server is really just used for me, and clients to see my progress. Less than 100 hits per day, and that's when I'm on a big project.

perl_diver




msg:3723930
 4:57 pm on Aug 15, 2008 (gmt 0)

Well, maybe a ghost in the machine? ;-)

Dabrowski




msg:3726589
 8:04 pm on Aug 19, 2008 (gmt 0)

perl_diver, you mentioned something about CGI::Carp in another thread, I've never used it.

You said it was designed to capture CGI errors, could it be used to debug this if it is a case of the script erroring somewhere and not dying properly?

phranque




msg:3726725
 12:49 am on Aug 20, 2008 (gmt 0)

you can do this to send all erors and warnings nicely formatted to the error log:
use CGI::Carp

if you want to see fatal errors in the browser also:
use CGI::Carp qw(fatalsToBrowser)

but it doesn't seem you are getting a fatal error.

are you making any system calls?

Dabrowski




msg:3728286
 6:55 pm on Aug 21, 2008 (gmt 0)

but it doesn't seem you are getting a fatal error.

are you making any system calls?

No, not making any system calls.

rocknbil mentioned something about a process he had failing but not terminating, that could be what is happening here. It wouldn't generate a 500 in the browser unless the process had ended.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved