Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Perl threads memory leak with socket and solutions I tried...

by ilxl (Initiate)
on Feb 10, 2010 at 01:35 UTC ( [id://822328]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks, This problem has been my headache for two weeks. Can anyone help me? Thanks a lot!
use threads; use SOCKETSVR::Server; my $port = 5188; my $d = new SOCKETSVR::Server LocalPort => $port; print "Socket Server started. port=$port\r\n"; while (my $conn = $d->accept) { # $conn is socket client handler, which is GLOBtype threads->new(\&start_process, $conn)->detach(); undef($conn); } sub start_process { my $conn=shift; if(my $r = $conn->recv_data) { #blabla.... } $conn->close; return; }

This program will create a thread for any socket client connection. But I found the memory is increasing rapidly, and the program will crash after have run for about 3 days.

In the past weeks, I tried a lot of solutions for this problem:

1. I updated Perl from v5.8.8 to v5.10.1, and threads from v1.0.7 to v1.7.5 ----It didn't work.

2. I used Thread::Pool module to avoid massive threads open and close, which might have caused memory leak. ---It prompted error: Can't store GLOB items at lib/Storable.pm. That's because $conn above is GLOB value

3. I tried to transfer fileno($conn) between threads and re-construct socket client by  open my $conn, "+<&=$fno"; ---It didn't work. fileno($conn) always returns 9 and socket client can't be re-opened.

4. I used some other threadpool module ---They didn't work for $conn cannot be shared. it's not a simple data structure.

So after so many tries, I still cannot make it. Can any Monk help me? Thanks!

Replies are listed 'Best First'.
Re: Perl threads memory leak with socket and solutions I tried...
by BrowserUk (Patriarch) on Feb 10, 2010 at 02:01 UTC

    Where does SOCKETSVR::Server come from?

    And what does it do that this doesn't?

    my $server = IO::Socket::INET->new( LocalHost => 'localhost', LocalPort => '5188', Proto => 'tcp', Listen => 5, Reuse => 1, ) or die "Couldn't create listening socket";

    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.
      SOCKETSVR::Server is basically IO::Socket::INET. I modified it a little.
        I modified it a little.

        In a way that causes memory leaks?


        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: Perl threads memory leak with socket and solutions I tried...
by jettero (Monsignor) on Feb 10, 2010 at 02:01 UTC
    You'll never get this to work right. Consider use forks or building some long running worker threads. Ultimately you'll never really be happy with threads in Perl (just a guess). Some people use them. Another option is Coro. Might be better.

    -Paul

      Thanks. I tried forks. When I start the program, it will create 2 processes. One of the process, the child process, still has memory leak problem, although the memory increases very slowly.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://822328]
Approved by jettero
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-04-25 09:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found