Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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:

  • Windows NT V4.00.1381
  • ActiveState Perl V5.6.0, build 616 (Yes, it's out of date, but I can't upgrade the production server for another good while. Probably when 5.8 is released.)


Everything went worng, just as foreseen.


In reply to Memory leak with socket by Biker

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (3)
    As of 2020-07-03 22:54 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?