Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #103510 > unrolled thread

tcp networking question (CLOSE_WAIT)

Started byRay <rui.vapps@gmail.com>
First post2016-02-25 09:55 -0800
Last post2016-02-25 10:44 -0800
Articles 6 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  tcp networking question (CLOSE_WAIT) Ray <rui.vapps@gmail.com> - 2016-02-25 09:55 -0800
    Re: tcp networking question (CLOSE_WAIT) "Martin A. Brown" <martin@linux-ip.net> - 2016-02-25 10:17 -0800
      Re: tcp networking question (CLOSE_WAIT) Ray <rui.vapps@gmail.com> - 2016-02-25 10:25 -0800
        Re: tcp networking question (CLOSE_WAIT) "Martin A. Brown" <martin@linux-ip.net> - 2016-02-25 10:56 -0800
          Re: tcp networking question (CLOSE_WAIT) Ray <rui.vapps@gmail.com> - 2016-02-25 10:59 -0800
    Re: tcp networking question (CLOSE_WAIT) Ray <rui.vapps@gmail.com> - 2016-02-25 10:44 -0800

#103510 — tcp networking question (CLOSE_WAIT)

FromRay <rui.vapps@gmail.com>
Date2016-02-25 09:55 -0800
Subjecttcp networking question (CLOSE_WAIT)
Message-ID<7ff15c67-102e-4b02-a2c8-87b24f1b8351@googlegroups.com>
hi,

I'm new to python networking. I am waiting TCP server/client app by using python built-in SocketServer. My problem is if client get killed, then the tcp port will never get released, in CLOSE_WAIT

maybe I didn't do the handler right? or anyway I can catch the client get killed?

I wrote following simple code to explain the issue I have, server listen on 127.0.0.1:1234
client need to send multi-messages (I am sending 2 in this test) if client get killed (I just do kill client_pid) then if I check lsof, I will see the new allocated tcp do not get released 

########server.py########

#!/usr/bin/python
import threading
import SocketServer
import time

class TCPServerRequestHandler(SocketServer.BaseRequestHandler):

    def handle(self):
        while True:
            data=self.request.recv(1024)
            if not data:
                time.sleep(0.1)
                continue
            print data
            self.request.send(data)
            if data=='end':
                break

def main():
    server = TCPServer(('127.0.0.1', 1234), TCPServerRequestHandler)
    ip, port = server.server_address
    server_thread = threading.Thread(target=server.serve_forever)
    server_thread.daemon = True
    server_thread.start()
    time.sleep(600)
    server.shutdown()
    server.server_close()
if __name__=='__main__':
    main()

########client.py########
import socket
import time

def client():
    sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect(('127.0.0.1', 1234))
    sock.sendall('hello')
    response=sock.recv(1024)
    print "Received:", response
    time.sleep(60)
    sock.sendall('end')
    response=sock.recv(1024)
    print "Received:", response
    sock.close()

if __name__=='__main__':
    client()


when I run it, client has 60 seconds sleep, so I just kill the process from OS
kill 93948

then I'm running lsof to find my TCP/UDP 
lsof -n -p $(ps -ef|grep serve[r]|awk '{print $2}')|egrep '(TCP|UDP)'|awk '{print $NF}'

TCP 127.0.0.1:search-agent (LISTEN)
TCP 127.0.0.1:search-agent->127.0.0.1:59411 (CLOSE_WAIT)

I will see the CLOSE_WAIT, this is where the client get killed.


any help would be great!

Thanks a lot!
 

[toc] | [next] | [standalone]


#103513

From"Martin A. Brown" <martin@linux-ip.net>
Date2016-02-25 10:17 -0800
Message-ID<mailman.133.1456424259.20994.python-list@python.org>
In reply to#103510
>I'm new to python networking. I am waiting TCP server/client app by 
>using python built-in SocketServer. My problem is if client get 
>killed, then the tcp port will never get released, in CLOSE_WAIT

I did not thoroughly review your code (other than to see that you 
are not using SO_REUSEADDR).  This is the most likely problem.

Suggestion:

  man 7 socket

Look for SO_REUSEADDR.  Then, apply what you have learned to your 
code.

-Martin

-- 
Martin A. Brown
http://linux-ip.net/

[toc] | [prev] | [next] | [standalone]


#103514

FromRay <rui.vapps@gmail.com>
Date2016-02-25 10:25 -0800
Message-ID<86a8ad2a-3185-4ff3-abe0-7345411fa0c2@googlegroups.com>
In reply to#103513
On Thursday, February 25, 2016 at 1:18:05 PM UTC-5, Martin A. Brown wrote:
> >I'm new to python networking. I am waiting TCP server/client app by 
> >using python built-in SocketServer. My problem is if client get 
> >killed, then the tcp port will never get released, in CLOSE_WAIT
> 
> I did not thoroughly review your code (other than to see that you 
> are not using SO_REUSEADDR).  This is the most likely problem.
> 
> Suggestion:
> 
>   man 7 socket
> 
> Look for SO_REUSEADDR.  Then, apply what you have learned to your 
> code.
> 
> -Martin
> 
> -- 
> Martin A. Brown
> http://linux-ip.net/

it's not I can't bind the address, my problem is: server is long run. if client die without "disconnect" then server will leak one socket. 

by using the built-in thread socket server. the extra tcp port are opened by built-in class itself.
if the handler() is finish correctly (the line with break) then this socket will get cleaned up. but if client dies, then I am never get out from that True loop. so the socket will keep in close_wait 

[toc] | [prev] | [next] | [standalone]


#103516

From"Martin A. Brown" <martin@linux-ip.net>
Date2016-02-25 10:56 -0800
Message-ID<mailman.134.1456426564.20994.python-list@python.org>
In reply to#103514
Hello again Ray,

>> >I'm new to python networking. I am waiting TCP server/client app by 
>> >using python built-in SocketServer. My problem is if client get 
>> >killed, then the tcp port will never get released, in CLOSE_WAIT
>> 
>> I did not thoroughly review your code (other than to see that you 
>> are not using SO_REUSEADDR).  This is the most likely problem.
>>
>> Suggestion:
>> 
>>   man 7 socket
>> 
>> Look for SO_REUSEADDR.  Then, apply what you have learned to your 
>> code.
>
>it's not I can't bind the address, my problem is: server is long 
>run. if client die without "disconnect" then server will leak one 
>socket.

Sorry for my trigger-happy, and incorrect reply.

After so many years, I should know better than to reply without 
completely processing questions.  Apologies.

>by using the built-in thread socket server. the extra tcp port are 
>opened by built-in class itself. if the handler() is finish 
>correctly (the line with break) then this socket will get cleaned 
>up. but if client dies, then I am never get out from that True 
>loop. so the socket will keep in close_wait
>
>I fond the issue. it's my own stupid issue.
>i did "continue" if no data received.
>just break from it then it will be fine

Well, I'm glad you found the issue.

Best of luck,

-Martin

-- 
Martin A. Brown
http://linux-ip.net/

[toc] | [prev] | [next] | [standalone]


#103517

FromRay <rui.vapps@gmail.com>
Date2016-02-25 10:59 -0800
Message-ID<fb83fa02-7e5e-442c-8d8b-1b1cbaf1d432@googlegroups.com>
In reply to#103516
On Thursday, February 25, 2016 at 1:56:21 PM UTC-5, Martin A. Brown wrote:
> Hello again Ray,
> 
> >> >I'm new to python networking. I am waiting TCP server/client app by 
> >> >using python built-in SocketServer. My problem is if client get 
> >> >killed, then the tcp port will never get released, in CLOSE_WAIT
> >> 
> >> I did not thoroughly review your code (other than to see that you 
> >> are not using SO_REUSEADDR).  This is the most likely problem.
> >>
> >> Suggestion:
> >> 
> >>   man 7 socket
> >> 
> >> Look for SO_REUSEADDR.  Then, apply what you have learned to your 
> >> code.
> >
> >it's not I can't bind the address, my problem is: server is long 
> >run. if client die without "disconnect" then server will leak one 
> >socket.
> 
> Sorry for my trigger-happy, and incorrect reply.
> 
> After so many years, I should know better than to reply without 
> completely processing questions.  Apologies.
> 
> >by using the built-in thread socket server. the extra tcp port are 
> >opened by built-in class itself. if the handler() is finish 
> >correctly (the line with break) then this socket will get cleaned 
> >up. but if client dies, then I am never get out from that True 
> >loop. so the socket will keep in close_wait
> >
> >I fond the issue. it's my own stupid issue.
> >i did "continue" if no data received.
> >just break from it then it will be fine
> 
> Well, I'm glad you found the issue.
> 
> Best of luck,
> 
> -Martin
> 
> -- 
> Martin A. Brown
> http://linux-ip.net/


Thank you very much for reply and help.

[toc] | [prev] | [next] | [standalone]


#103515

FromRay <rui.vapps@gmail.com>
Date2016-02-25 10:44 -0800
Message-ID<d27ee121-7e11-4f0a-82f9-9781683bbc70@googlegroups.com>
In reply to#103510
On Thursday, February 25, 2016 at 12:56:10 PM UTC-5, Ray wrote:
> hi,
> 
> I'm new to python networking. I am waiting TCP server/client app by using python built-in SocketServer. My problem is if client get killed, then the tcp port will never get released, in CLOSE_WAIT
> 
> maybe I didn't do the handler right? or anyway I can catch the client get killed?
> 
> I wrote following simple code to explain the issue I have, server listen on 127.0.0.1:1234
> client need to send multi-messages (I am sending 2 in this test) if client get killed (I just do kill client_pid) then if I check lsof, I will see the new allocated tcp do not get released 
> 
> ########server.py########
> 
> #!/usr/bin/python
> import threading
> import SocketServer
> import time
> 
> class TCPServerRequestHandler(SocketServer.BaseRequestHandler):
> 
>     def handle(self):
>         while True:
>             data=self.request.recv(1024)
>             if not data:
>                 time.sleep(0.1)
>                 continue
>             print data
>             self.request.send(data)
>             if data=='end':
>                 break
> 
> def main():
>     server = TCPServer(('127.0.0.1', 1234), TCPServerRequestHandler)
>     ip, port = server.server_address
>     server_thread = threading.Thread(target=server.serve_forever)
>     server_thread.daemon = True
>     server_thread.start()
>     time.sleep(600)
>     server.shutdown()
>     server.server_close()
> if __name__=='__main__':
>     main()
> 
> ########client.py########
> import socket
> import time
> 
> def client():
>     sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>     sock.connect(('127.0.0.1', 1234))
>     sock.sendall('hello')
>     response=sock.recv(1024)
>     print "Received:", response
>     time.sleep(60)
>     sock.sendall('end')
>     response=sock.recv(1024)
>     print "Received:", response
>     sock.close()
> 
> if __name__=='__main__':
>     client()
> 
> 
> when I run it, client has 60 seconds sleep, so I just kill the process from OS
> kill 93948
> 
> then I'm running lsof to find my TCP/UDP 
> lsof -n -p $(ps -ef|grep serve[r]|awk '{print $2}')|egrep '(TCP|UDP)'|awk '{print $NF}'
> 
> TCP 127.0.0.1:search-agent (LISTEN)
> TCP 127.0.0.1:search-agent->127.0.0.1:59411 (CLOSE_WAIT)
> 
> I will see the CLOSE_WAIT, this is where the client get killed.
> 
> 
> any help would be great!
> 
> Thanks a lot!


I fond the issue. it's my own stupid issue.
i did "continue" if no data received.
just break from it then it will be fine

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web