Subject: | IO::select blocks a socket in multithreaded server application |
I have used days to get my simple httpd daemon experiment to run.
Finally I hacked the siplest way to get it working. This piece of code
explains the problem I have faced:
# Simple threaded http server application
use 5.010;
use strict;
use warnings;
use threads;
use IO::Select;
use HTTP::Daemon;
my $daemon = HTTP::Daemon->new(LocalPort => 8080, ReuseAddr => 1) || die;
my $select = IO::Select->new($daemon);
say 'Please, go to ', $daemon->url;
for (;;)
{
#This line is needed to get any answers from this server
threads->yield() until ($select->can_read(0));
my $connection = $daemon->accept;
my $thread = threads->create(\&service_thread, $connection);
$thread->detach;
}
sub service_thread
{
my ($connection) = @_;
while (my $request = $connection->get_request)
{
$connection->send_error(400); #400 Bad Request
}
}
As you can see, I have add a yield loop for select to get this server
application working. In Linux perl version this works without the select
line at all! In Windows the accept call blocks service thread socket and
also the select call blocks the tread socket if used without zero timeout.
This is perl, v5.10.1 (*) built for MSWin32-x86-multi-thread
Microsoft Windows XP [versio 5.1.2600]