> # autodie in use
> Why ?
> It seems to me that death on failure is not something you want here.
THIS WAS THE PROBLEM! I have some autodie homework to do, but just putting "no autodie" in a block with "close $client" stops double-clicks killing the server.
Thank you Rob! | [reply] |
print doesn't always write to the underlying file handle immediately. What you print often ends up in a buffer that gets written to the underlying file handle on a later print, or when the handle is closed. This is an optimization called "buffering".
What's happening here is that the actual printing is being delayed until you call close, but the browser has already closed the socket by that time (to abort the first request before making the second request). Your code is therefore attempting to write to a closed socket, which normally results in a SIGPIPE signal killing your process, but you must be on Windows or have that signal ignored, because it results in close failing instead. autodie converts this into an exception.
All of that is perfectly fine! The problem is in your handling of that error, or lack thereof. Your server code should handle all exceptions that occur while handling a request. As such, your code should look like this:
use autodie;
my $server = IO::Socket::INET->new(...);
while (1) {
my $client = $server->accept()
or die("Can't accept connections: $!\n");
if (!eval {
...
close($client);
return 1; # No exception
}) {
warn("Error servicing request: $@");
}
}
| [reply] [d/l] [select] |
Thank you ikegami for explaining the situation. I was following Perl Cookbook examples that don't check $server->accept or $client->close:
while (my $client = $server->accept) {
...
$client->close;
undef $client;
}
Time to read: 16.15. Installing a Signal Handler
Thanks again! | [reply] |