New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
asyncore does not react properly on close() #55087
Comments
I am trying to add a simple timer to each created socket and destroy it class client(asyncore.dispatcher):
def __init__(self,host):
...
self.timeout = time.time() + 5
def readable(self):
if time.time() >= self.timeout:
self.close()
return True When running that code, it raises an exception: asyncore.loop(timeout=0.8)
File "/usr/lib/python2.6/asyncore.py", line 211, in loop
poll_fun(timeout)
File "/usr/lib/python2.6/asyncore.py", line 144, in poll
raise
File "/usr/lib/python2.6/asyncore.py", line 141, in poll
r, w, e = select.select(r, w, e, timeout)
select.error: (9, 'Bad file descriptor') Although del_channel is executed properly and the socket is removed from the map, the poll function is not updated with that info and continues to keep the socket into the r,w,e. |
Problem is you have to return False right after close(). |
Sorry, I forgot to mention - I have already tried to return False, but there was no difference. def readable(self):
if time.time() >= self.timeout:
self.close()
return False
else:
return True |
What if you return False also in writable method? |
Precisely, I traced down the problem by putting a simple "breakpoint" def poll(timeout=0.0, map=None):
if map is None:
map = socket_map
if map:
r = []; w = []; e = []
for fd, obj in map.items():
is_r = obj.readable()
print "Readable??? -->" , is_r
is_w = obj.writable()
if is_r:
r.append(fd)
if is_w:
w.append(fd)
if is_r or is_w:
e.append(fd)
if [] == r == w == e:
time.sleep(timeout)
return
try: And here it comes: [5] [5] [5] Traceback (most recent call last):
File "./dlms_client.py", line 136, in <module>
asyncore.loop(timeout=0.8)
File "/usr/lib/python2.6/asyncore.py", line 213, in loop
poll_fun(timeout)
File "/usr/lib/python2.6/asyncore.py", line 146, in poll
raise
File "/usr/lib/python2.6/asyncore.py", line 143, in poll
r, w, e = select.select(r, w, e, timeout)
select.error: (9, 'Bad file descriptor') So, in order this to work, on first sight all r,w,e must not point to |
What I noticed in tracing through the code is that it's getting stuck in a loop because it depends on grabbing asyncore.socket_map if map is null when passed into asyncore.loop. I got around this by appending: asyncore.loop(0.1, map=[], count=1) After my close(). I believe this bypasses the grab of socket_map from asyncore.socket_map and allows the while loop in asyncore.loop to exit cleanly. |
For more clarity, I am passing in a list because it will evaluate as False in the while loop on 209 and 213. The fix is to add len(map) to the while loops on those lines. |
Teodor or Gavin: is the (mis)behavior the same in 3.3? Giampaolo: has the OP identified a fixable misbehavior relative to the documented behavior, making this a valid behavior issue? |
I'm not sure what the OP and Gavin are complaining about in their last messages. Could you guys be more clear and/or provide a code sample which reproduces the problem? |
I am re-closing as there is no defined bug to fix, and 2.7 is closed to enhancements. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: