ewhitt has asked for the wisdom of the Perl Monks concerning the following question:

I came across this script I have been using for handling multiple socket connections. I am new to IO::Socket in general. When a client disconnects, the server ends with a "Bus Error". I am doing this on OSX 10.5.1. Any ideas?
#!/usr/bin/perl use strict; use warnings; use IO::Socket; use threads; system("clear"); print "Server Listening on tcp/7890\n"; my $lsn = new IO::Socket::INET( Listen => '10', LocalPort => '7890', Proto => 'tcp', ReUse => 1, ) or die $!; sub do_stuff { my $sock = shift; print "Client connected\n"; while( my $input = <$sock> ) { chomp( $input ); print "$input\n"; } print "Client disconnected\n"; } threads->create( \&do_stuff, $lsn->accept, )->detach while 1;

Replies are listed 'Best First'.
Re: Bus Error from threading IO::Socket
by snoopy (Curate) on Aug 12, 2008 at 02:35 UTC
    I also get the bus error on OSX 10.5.1. Runs fine for me on Debian.

    I notice that the bus error goes away when you don't detach the thread, ie, works fine when I change the last line to:

    threads->create( \&do_stuff, $lsn->accept, ) while 1;

    Detached threads seem to be a contributing factor.

Re: Bus Error from threading IO::Socket
by jethro (Monsignor) on Aug 12, 2008 at 02:10 UTC
    Just tested the script on my linux box and it worked flawlessly. Is perl included with Mac OS or did you install it?
Re: Bus Error from threading IO::Socket
by Perlbotics (Archbishop) on Aug 12, 2008 at 10:55 UTC
    When the client disconnects, the process usually receives a SIGPIPE signal. That might also contribute to your problem when the corresponding (unaccessible?) handler is called. You might want to install a signal handler to further investigate this? Do you see the "Client disconnected" message? Please add $|=1; early to do_stuff so buffering will not swallow this message.
    Another Idea: SIGBUS is usually caused by a misaligned memory access. Thus, a change of your dynamically loaded libraries might have caused that (since Perl was compiled). Did you modify LD_LIBRARY_PATH lately? ldd your_perl_executable together with perl -V | egrep thread might shed some light on this issue.
Re: Bus Error from threading IO::Socket
by BrowserUk (Patriarch) on Aug 12, 2008 at 17:09 UTC

    Personally, I'd re-cast your main loop as:

    while( 1 ) { my $c = $lsn->accept; print "Client connected: $c"; threads->create( \&do_stuff, $c )->detach; undef $c; }

    Whether that will make the slightest difference on your platform/versions I cannot test, but I have a hunch it might.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Bus Error from threading IO::Socket
by zentara (Cardinal) on Aug 12, 2008 at 16:43 UTC
    Detached threads seem to be a contributing factor.

    A detached thread must return, and I'm guessing on your platform, the socket isn't closing itself automatically. Maybe try something like this:

    sub do_stuff { my $sock = shift; print "Client connected\n"; while( my $input = <$sock> ) { chomp( $input ); print "$input\n"; } print "Client disconnected\n"; $sock->close; #or close($sock); return; }

    I'm not really a human, but I play one on earth Remember How Lucky You Are