http://qs321.pair.com?node_id=190052

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

I have an annoying socket related problem. After a lot of trial and error I have found a work-around to the problem and my code is now functional.

On the other hand, I don't understand why my work-around works, neither why it is required. This is where your help and explanations will be much appreciated.

Scenario:

I have a 'server' application that opens a socket. It then loops around an accept() on the socket. Whenever a client connects, the server reads and (for testing purposes only) discards anything read. It never writes anything to the socket. This part works fine.

I have a test client that does nothing but connect to the server socket and then closes the socket. It writes nothing to the socket before closing it.

This creates a major memory leak in the client application.

If I let the client write some arbitrary text to the socket, the problem remains. But, if I let the client read from the socket, the memory leak goes away (?!?!). There was nothing there to read. The client immediately gets a logical end-of-file, but it will not leak memory.

Question:

How can this be?

Code:

This is a code snippet that reproduces the problem:
#!/usr/bin/perl -w require 5.005; use strict; use IO::Socket; for(1..100000) { open_sock(); } exit; sub open_sock { my $answer; my $sock=IO::Socket::INET->new(PeerAddr=>'10.118.32.31', PeerPort=>1955, Proto=>'tcp', Timeout=>20)|| die("Failed to open socket.\n"); # Writing to the socket makes no difference. #print $sock "x"; # Reading from the socket closes the memory leak. # Nothing gets printed on STDOUT from the print statement, # i.e. there was nothing to read. Right? while(defined($answer=<$sock>)) { print("Answer: $answer\n"); } close($sock); }

Environment:



Everything went worng, just as foreseen.

Replies are listed 'Best First'.
Re: Memory leak with socket
by Abigail-II (Bishop) on Aug 14, 2002 at 13:19 UTC
    It could be a bug. Try it with 5.8.0, which has been released some weeks ago. And yes, I know you don't want to upgrade the production server, but you do have a test and a development area, don't you?

    If you don't have a memory leak on 5.8.0, the bug was fixed. If it's still there, please use the "perlbug" command to report a bug.

    Abigail

      I have only one test & dev environment. Need to keep it in sync with the prod system.

      OTOH, I will try this at home on my Linux box.


      Everything went worng, just as foreseen.

        I have only one test & dev environment. Need to keep it in sync with the prod system.
        So? I've more than 190 versions of Perl on my laptop. Perl doesn't refuse to run if more than one version of it is installed. Besides, how would you ever test if a new release doesn't break anything? Upgrade the production system at the same time as the test system? If you can't use your test system to test whether a bug that's hitting is solved in a new version of Perl, your test environment is not good enough.
        I will try this at home on my Linux box.
        It could very well be that the Linux version doesn't have a problem, where the windows version has. Perhaps the leak is in the kernel.

        Abigail

Re: Memory leak with socket
by dws (Chancellor) on Aug 14, 2002 at 17:33 UTC
    We had a memory leak in a Perl application server process, and suspected the socket code. The leak went away (or was reduced to the barely noticable point) when we upgraded to 5.6

    Our workaround while dealing with the leak was to restart the server process every N thousand requests. Ugly, but it kept thing chugging along.