Is the webserver running under your control? Some ISPs put a time limit on running cgi-scripts. If your script takes too long on a large amount of content, it may be killed off by the server (SIGTERM....). HTH, although it is not really a debugging hint...