Note that there are some explanatory texts on larger screens.

plurals
  1. POSocket message hangs until thread completes in Perl
    primarykey
    data
    text
    <p>I am trying to write Perl code that does two-way communication over a Unix socket. It needs to do the following things:</p> <ol> <li>Client code sends a request over the socket</li> <li>Server code reads the request</li> <li>Server performs any actions that should happen immediately</li> <li>Server creates a new thread to do additional actions that may take a long time</li> <li>Server sends response over the socket</li> <li>Client receives response</li> <li>The new thread continues to work after the response has been sent</li> </ol> <p>I have gotten most of this to work now, but with one problem. Steps 1 - 5 work fine, but at step 6, the client is unable to read the response until AFTER the thread has exited. (even though the response was sent more-or-less immediately) Any idea what I'm doing wrong?</p> <p>I'm using Perl 5.10.1 on Ubuntu Lucid.</p> <p>Here's an example:</p> <p><strong>Client code:</strong></p> <pre><code>#!/usr/bin/perl use strict; use Socket; my $socket_name = 'catsock'; my $client_message = "Hello server, this is the client."; my $SOCK; socket($SOCK, PF_UNIX, SOCK_STREAM, 0) or die "socket: $!"; connect($SOCK, sockaddr_un($socket_name)) or die "connect: $!"; $| = 1, select $_ for select $SOCK; # turn on autoflush print $SOCK $client_message; # send the message shutdown($SOCK,1); # finished writing print "sent: $client_message\n"; my $server_message = do { local $/; &lt;$SOCK&gt; }; # get the response print "recieved: $server_message\n\n"; </code></pre> <p><strong>Server code</strong></p> <pre><code>#!/usr/bin/perl use strict; use Socket; use threads; use threads::shared; sub threadfunc { print " waiting 5 seconds in new thread...\n"; sleep(5); print " exiting thread...\n\n"; } my $server_message = "Hello client, this is the server."; my $socket_name = 'catsock'; my $SERVER; my $CLIENT; socket($SERVER, PF_UNIX, SOCK_STREAM, 0) or die "socket: $!"; unlink($socket_name); bind($SERVER, sockaddr_un($socket_name)) or die "bind: $!"; chmod(0660, $socket_name); listen($SERVER, SOMAXCONN) or die "listen: $!"; print "server started on $socket_name\n\n"; while(1) { accept($CLIENT, $SERVER); my $client_message = do { local $/; &lt;$CLIENT&gt; }; # grab entire message print "recieved: $client_message\n"; print "creating new thread...\n"; my $thr = threads-&gt;create(\&amp;threadfunc); $thr-&gt;detach(); print $CLIENT $server_message; # send the response print "sent: $server_message\n\n"; close $CLIENT; } </code></pre> <p>When I run this, the following things happen:</p> <ol> <li>Client sends "Hello server, this is the client."</li> <li>Server receives client's message</li> <li>Server creates a new thread</li> <li>Server sends "Hello client, this is the server."</li> <li>Client should receive the message now, but it does not.</li> <li>New thread on server sleeps for 5 seconds</li> <li>Thread exits</li> <li><em>Now</em> the client receives the server's message, even though it was sent 5 seconds ago. Why?</li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload