[issue8831] recv and recvfrom on UDP socket do not return/throw exception after a close()

2010-05-27 Thread Alessandro Roat

New submission from Alessandro Roat :

A thread blocked on a recv or a recvfrom method on a UDP socket (waiting for a 
datagram) can not be unlocked calling a .close() from a different thread.
This is in contrast with the standard C++/C behavior, where a close() on a 
socket causes an asynchronous and immediate exception/return with error on the 
functions that are using the socket at the same time (but in another thread).
Thus, it is impossible to unlock a waiting recv/recvfrom calling a close() or a 
shutdown() if no more datagrams are coming.

--
components: IO
messages: 106596
nosy: Alessandro.Roat
priority: normal
severity: normal
status: open
title: recv and recvfrom on UDP socket do not return/throw exception after a 
close()
type: behavior
versions: Python 2.6

___
Python tracker 
<http://bugs.python.org/issue8831>
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue8831] recv and recvfrom on UDP socket do not return/throw exception after a close()

2010-05-27 Thread Alessandro Roat

Alessandro Roat  added the comment:

This is an example, test it with netcat (nc -u localhost ) on linux (ubuntu 
9.10).
Lauch it with python , a prompt will appear.
Type "start" to launch the server, test the server sending UDP packets with 
netcat, the lenght of packet will be correctly printed.
However, when you'll type "stop" the close will be invoked but the receiving 
thread wont stop and the join in the stop() wont never return and you will need 
to kill the python interpreter.


import socket
import threading
import sys
import select


class UDPServer:
def __init__(self):
self.s=None
self.t=None
def start(self,port=):
if not self.s:
self.s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.s.bind(("",port))
self.t=threading.Thread(target=self.run)
self.t.start()
def stop(self):
if self.s:
self.s.close()
self.t.join()
self.t=None
def run(self):
while True:
try:
data,addr=self.s.recvfrom(1024)
print "recv done"
if len(data)<=0:
raise
self.onPacket(addr,data)
except:
break
#chiusura socket
print "server is no more running"
self.s=None
def onPacket(self,addr,data):
print len(data)


us=UDPServer()
while True:
sys.stdout.write("UDP server> ")
cmd=sys.stdin.readline()
if cmd=="start\n":
print "starting server..."
us.start()
print "done"
elif cmd=="stop\n":
print "stopping server..."
us.stop()
print "done"
elif cmd=="quit\n":
print "Quitting ..."
us.stop()
break;

print "bye bye"

--

___
Python tracker 
<http://bugs.python.org/issue8831>
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com