Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Problem with Multi- threaded Server

Reply
Thread Tools

Problem with Multi- threaded Server

 
 
janicehwang1325@yahoo.com
Guest
Posts: n/a
 
      06-23-2006
hi,

I post the program before, however, based on the response, i simplify
my server code in the following. I still got error when the clients on
the same LAN trying to connect to the server it gives me segmentation
fault error. What is the cause? For your information, the prefork
server runs perfectly well without any errors. However, due to
specification, i need to design my server handling multiple clients
using threads.


--------------------------------------------------------------------------------------------------------------------------------------------
use threads;

#!/usr/bin/perl

use POSIX ":sys_wait_h";
use IO::Socket::SSL;
use IO::Handle;
use Time::Local;
use DBI;

my ($sock, $s, $v_mode);

unless (@ARGV == 1) { die "usage: perl $0 <Port Number>" };
($port) = @ARGV;

# Check to make sure that we were not accidentally run in the wrong
# directory:
unless (-d "certs") {
if (-d "../certs") {
chdir "..";
} else {
die "No CA Certs\n";
}
}

if(!($sock = IO::Socket::SSL->new( Listen => 20,
LocalPort => $port,
Proto => 'tcp',
ReuseAddr => 1,
SSL_verify_mode => 0x01,
SSL_use_cert => 1,
SSL_passwd_cb => sub {return
"haha"},
SSL_key_file =>
'certs/server_key.pem',
SSL_cert_file =>
'certs/server_certs.pem',
)) ) {
warn "unable to create socket: ", &IO::Socket::SSL::errstr, "\n";
exit(0);

}
warn "SSL socket created: $sock.\n";
warn "waiting for next connection......\n";

sub handle_connection{
$s = shift;
$output = shift || $s;
$exit = 0;

#$s->autoflush(1);

my ($peer_cert, $subject_name, $issuer_name, $date, $str);

if( ! $s ) {
warn "error: ", $s->errstr, "\n";
$exit = 1;
break;
}

chomp($_);

while(<$s>){
if( ref($s) eq "IO::Socket::SSL" && $s->connected()) {
$subject_name =
$s->peer_certificate("subject");
$issuer_name =
$s->peer_certificate("issuer");
print "from client > $_";
if(/equal/i){
#print $s "No new coming log...\n";
last;
}
}
else{
$exit = 1;
}
last if ($exit == 1);
}
print "Thread exited lor..\n";
}

while(1){
while(($s = $sock->accept())) {
#$s->autoflush(1);
print "new thread here .. \n";
#$thread = threads->create(\&handle_connection, $s);
#$thread->detach;
async(\&handle_connection, $s)->detach;
}
}

 
Reply With Quote
 
 
 
 
Ted Zlatanov
Guest
Posts: n/a
 
      06-23-2006
On 23 Jun 2006, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> I post the program before, however, based on the response, i simplify
> my server code in the following. I still got error when the clients on
> the same LAN trying to connect to the server it gives me segmentation
> fault error. What is the cause? For your information, the prefork
> server runs perfectly well without any errors. However, due to
> specification, i need to design my server handling multiple clients
> using threads.


Hello,

thanks for posting complete code. Now you know:

- the DBI+SSL server works without threads

Next, find out if:

- the threaded server works without SSL
- the threaded server works without DBI
- the threaded server works without SSL and DBI (trivial case, but needed)

I would say at this point it's very likely you're hitting a bug, so
find out where it is... Then you can try to avoid the bug and you can
tell the developers about it.

Ted
 
Reply With Quote
 
 
 
 
janicehwang1325@yahoo.com
Guest
Posts: n/a
 
      06-26-2006
Thanks for the debuggin guidelines. Here are some results for my
testing:

1. Threaded server works fine without SSL.
2. DBI doesn't cause a problem for threaded server (either enable or
disable DBI does not cause the problem with the condition WITHOUT SSL)
3. Threaded server fails when use with SSL.
4. Threaded server works without SSL and DBI.

I tried to debug by printing line after each process. I found out that,
when the second client(same host as the first client) trying to connect
to the server, the server is actually spawning a new thread to handle
it. The error occurs is after the server receiving the data from the
second client. Segmentation fault occurs either after receiving first
few lines of the data and after server spawns a new thread.

Nevertheless, I found out that the server actually detach the threads
after spawning the first thread. I have no idea what is actually going
on. Please advice.



Ted Zlatanov wrote:
> On 23 Jun 2006, (E-Mail Removed) wrote:
>
> > I post the program before, however, based on the response, i simplify
> > my server code in the following. I still got error when the clients on
> > the same LAN trying to connect to the server it gives me segmentation
> > fault error. What is the cause? For your information, the prefork
> > server runs perfectly well without any errors. However, due to
> > specification, i need to design my server handling multiple clients
> > using threads.

>
> Hello,
>
> thanks for posting complete code. Now you know:
>
> - the DBI+SSL server works without threads
>
> Next, find out if:
>
> - the threaded server works without SSL
> - the threaded server works without DBI
> - the threaded server works without SSL and DBI (trivial case, but needed)
>
> I would say at this point it's very likely you're hitting a bug, so
> find out where it is... Then you can try to avoid the bug and you can
> tell the developers about it.
>
> Ted


 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      06-26-2006
"(E-Mail Removed)" <(E-Mail Removed)> wrote:
> Thanks for the debuggin guidelines. Here are some results for my
> testing:
>
> 1. Threaded server works fine without SSL.
> 2. DBI doesn't cause a problem for threaded server (either enable or
> disable DBI does not cause the problem with the condition WITHOUT SSL)
> 3. Threaded server fails when use with SSL



Have you read and understood the parts about close and SSL_no_shutdown in
the IO::Socket::SSL docs?

I can't get SSL to install, so I can't test it, but I suspect this is
likely the problem.


Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
Ted Zlatanov
Guest
Posts: n/a
 
      06-26-2006
On 26 Jun 2006, (E-Mail Removed) wrote:

"(E-Mail Removed)" <(E-Mail Removed)> wrote:
>> Thanks for the debuggin guidelines. Here are some results for my
>> testing:
>>
>> 1. Threaded server works fine without SSL.
>> 2. DBI doesn't cause a problem for threaded server (either enable or
>> disable DBI does not cause the problem with the condition WITHOUT SSL)
>> 3. Threaded server fails when use with SSL

>
>
> Have you read and understood the parts about close and SSL_no_shutdown in
> the IO::Socket::SSL docs?
>
> I can't get SSL to install, so I can't test it, but I suspect this is
> likely the problem.


I'm glad we've established the problem is just in the SSL layer.

An additional suggestion: after you looked at the docs, if you still
can't figure the problem out, contact the IO::Socket::SSL
maintainers. Until the problem is resolved, you could turn off SSL so
your server can be used, if that's prudent depending on your
particular security situation.

Ted
 
Reply With Quote
 
janicehwang1325@yahoo.com
Guest
Posts: n/a
 
      06-27-2006
Ya, I miss that part and I amend my program by putting the close with
SSL_no_shutdown. However, this only help when the client program ends
the connection and the server program would not hang. It does not
actually solve the segmentation fault problem. I did send my problem to
the maintainers of IO::Socket::SSL. While waiting for their reply, will
keep debuggin. Thank you very much for your information. I appreciates
a lot. Anyhow, any progressions on the program will be shared.

Ted Zlatanov wrote:
> On 26 Jun 2006, (E-Mail Removed) wrote:
>
> "(E-Mail Removed)" <(E-Mail Removed)> wrote:
> >> Thanks for the debuggin guidelines. Here are some results for my
> >> testing:
> >>
> >> 1. Threaded server works fine without SSL.
> >> 2. DBI doesn't cause a problem for threaded server (either enable or
> >> disable DBI does not cause the problem with the condition WITHOUT SSL)
> >> 3. Threaded server fails when use with SSL

> >
> >
> > Have you read and understood the parts about close and SSL_no_shutdown in
> > the IO::Socket::SSL docs?
> >
> > I can't get SSL to install, so I can't test it, but I suspect this is
> > likely the problem.

>
> I'm glad we've established the problem is just in the SSL layer.
>
> An additional suggestion: after you looked at the docs, if you still
> can't figure the problem out, contact the IO::Socket::SSL
> maintainers. Until the problem is resolved, you could turn off SSL so
> your server can be used, if that's prudent depending on your
> particular security situation.
>
> Ted


 
Reply With Quote
 
janicehwang1325@yahoo.com
Guest
Posts: n/a
 
      06-27-2006
I found another funny error in the code. When I test the socket whether
it is detached or not, by $thread->detach() or die "can't detach"; it
prompts that "A thread exited while 2 threads were running." I test it
with the IO::Socket::INET, same error occurs. I wonder this is the
cause of the problem or just another problem as I can run the program
with IO::Socket::INET perfectly without the checking on detach.


(E-Mail Removed) wrote:
> Ya, I miss that part and I amend my program by putting the close with
> SSL_no_shutdown. However, this only help when the client program ends
> the connection and the server program would not hang. It does not
> actually solve the segmentation fault problem. I did send my problem to
> the maintainers of IO::Socket::SSL. While waiting for their reply, will
> keep debuggin. Thank you very much for your information. I appreciates
> a lot. Anyhow, any progressions on the program will be shared.
>
> Ted Zlatanov wrote:
> > On 26 Jun 2006, (E-Mail Removed) wrote:
> >
> > "(E-Mail Removed)" <(E-Mail Removed)> wrote:
> > >> Thanks for the debuggin guidelines. Here are some results for my
> > >> testing:
> > >>
> > >> 1. Threaded server works fine without SSL.
> > >> 2. DBI doesn't cause a problem for threaded server (either enable or
> > >> disable DBI does not cause the problem with the condition WITHOUT SSL)
> > >> 3. Threaded server fails when use with SSL
> > >
> > >
> > > Have you read and understood the parts about close and SSL_no_shutdown in
> > > the IO::Socket::SSL docs?
> > >
> > > I can't get SSL to install, so I can't test it, but I suspect this is
> > > likely the problem.

> >
> > I'm glad we've established the problem is just in the SSL layer.
> >
> > An additional suggestion: after you looked at the docs, if you still
> > can't figure the problem out, contact the IO::Socket::SSL
> > maintainers. Until the problem is resolved, you could turn off SSL so
> > your server can be used, if that's prudent depending on your
> > particular security situation.
> >
> > Ted


 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      06-27-2006
"(E-Mail Removed)" <(E-Mail Removed)> wrote:
> Ya, I miss that part and I amend my program by putting the close with
> SSL_no_shutdown. However, this only help when the client program ends
> the connection and the server program would not hang. It does not
> actually solve the segmentation fault problem.


The SSLeay module seems to be fundamentally unsafe for threads. I tried a
few things to hack it with CLONED, etc. but haven't been able to. The mere
existence of a thread is enough to trigger the problem, the thread doesn't
need to do anything with the socket at all. By changing the "close"
parameters, I could make it segfault either ealier (upon the initial close)
or later (half way through the 2nd accept) but couldn't get rid of it
altogether.


__SERVER__
use threads;
use POSIX ":sys_wait_h";
use IO::Socket::SSL qw(debug4);
use IO::Handle;
use strict;

my ($sock);

unless (@ARGV == 1) { die "usage: perl $0 <Port Number>" };
my ($port) = @ARGV;

if(!($sock = IO::Socket::SSL->new( Listen => 20, LocalPort => $port,
Proto => 'tcp', ReuseAddr => 1,
SSL_key_file => 'certs/newkey.pem',
SSL_cert_file => 'certs/newcert.pem',
)) ) {
warn "unable to create socket: ", &IO::Socket::SSL::errstr, "\n";
exit(0);

}

warn "SSL socket created: $sock.\n";
while(1){

while((my $s = $sock->accept())) {
warn "I accepted $s";
print "new thread here .. \n";
my $thread = threads->create(sub {sleep 5});
$thread->join;
warn "joined";
$s->close(SSL_no_shutdown => 1);
}
}


__CLIENT__
#!/usr/bin/perl

use POSIX ":sys_wait_h";
use IO::Socket::SSL;
use IO::Handle;
use strict;
use warnings;

unless (@ARGV == 1) { die "usage: perl $0 <Port Number>" };
my ($port) = @ARGV;
my $sock = IO::Socket::SSL->new("localhost:$port") or die "$! $@ ",
&IO::Socket::SSL::errstr; use Data:umper;
warn "SSL socket created: $sock.\n";
warn Dumper $sock;

print $sock "asldfjalsdjfsadlfj\n";
__END__

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
Ted Zlatanov
Guest
Posts: n/a
 
      06-27-2006
On 26 Jun 2006, (E-Mail Removed) wrote:

> Ya, I miss that part and I amend my program by putting the close with
> SSL_no_shutdown. However, this only help when the client program ends
> the connection and the server program would not hang. It does not
> actually solve the segmentation fault problem. I did send my problem to
> the maintainers of IO::Socket::SSL. While waiting for their reply, will
> keep debuggin. Thank you very much for your information. I appreciates
> a lot. Anyhow, any progressions on the program will be shared.


One last thing: you could try the `stunnel' program as your SSL
front-end, and run your server behind it. I don't know if it's
available on Windows, or if something like it is available on Windows,
but it's definitely something you should consider if SSL breaks your
otherwise good threaded server.

Ted
 
Reply With Quote
 
janicehwang1325@yahoo.com
Guest
Posts: n/a
 
      06-28-2006
For your information, I am running my program in FreeBSD(should have
mention earlier, sorry bout this) and connect to the FreeBSD using SSH.
Therefore, I don't think I can use "stunnel" program for it. One more
interesting to share with you all, I tried to run the program on
another machine using PERL 5.8.6, it works!!! However, using back on a
machine with PERL 5.8.5, it fails. I wonder it is related to the memory
thing? That's why the program having segmentation fault problem? I will
try to get more machines to test it on and share with you all.

P/S: I didn't get any reply from the maintainers of IO::Socket::SSL.

Ted Zlatanov wrote:
> On 26 Jun 2006, (E-Mail Removed) wrote:
>
> > Ya, I miss that part and I amend my program by putting the close with
> > SSL_no_shutdown. However, this only help when the client program ends
> > the connection and the server program would not hang. It does not
> > actually solve the segmentation fault problem. I did send my problem to
> > the maintainers of IO::Socket::SSL. While waiting for their reply, will
> > keep debuggin. Thank you very much for your information. I appreciates
> > a lot. Anyhow, any progressions on the program will be shared.

>
> One last thing: you could try the `stunnel' program as your SSL
> front-end, and run your server behind it. I don't know if it's
> available on Windows, or if something like it is available on Windows,
> but it's definitely something you should consider if SSL breaks your
> otherwise good threaded server.
>
> Ted


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Threaded server Giampaolo Rodola' Python 3 01-14-2008 01:53 PM
Event driven server that wastes CPU when threaded doesn't Snor Python 14 11-05-2006 01:36 PM
multi threaded server sockets thewhoracle Java 5 06-08-2005 07:51 PM
multi-threaded server, pthreads & sleep Parahat Melayev C Programming 5 03-16-2005 10:22 PM
Threaded server: what's wrong with this code? Fortepianissimo Python 3 12-25-2003 06:43 PM



Advertisments