Asyncore select statement problem

J

JamesHoward

I have a problem with python's asyncore module throwing a bad file
descriptor error. The code might be difficult to copy here, but the
problem is essentially:

The server wants to sever the connection of an open Asyncore socket.
Calling the socket.close() nor the socket.shutdown(2) calls seem to
work. The only way I can close the connection without creating the
error below is to have the client close the connection.

I have the asyncore.loop() as the last line of a thread that is spawned
within the applications "mainframe.py" or gui thread. It doesn't seem
to me like this would make a difference, but I am unfamiliar with the
specifics of how the asyncore module works.

Any thoughts people have would be greatly appreciated. If needed I may
be able to create a small version of the problem to post for people to
see.

Thanks,
Jim Howard


Exception in thread Thread-1:
Traceback (most recent call last):
File
"/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/threading.py",
line 460, in __bootstrap
self.run()
File
"/Users/jwhoward2/Documents/Projects/LJServer/LJDeviceServer/DeviceServer.py",
line 23, in run
asyncore.loop()
File
"/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/asyncore.py",
line 191, in loop
poll_fun(timeout, map)
File
"/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/asyncore.py",
line 121, in poll
r, w, e = select.select(r, w, e, timeout)
error: (9, 'Bad file descriptor')
 
G

Gabriel Genellina

I have a problem with python's asyncore module throwing a bad file
descriptor error. The code might be difficult to copy here, but the
problem is essentially:

The server wants to sever the connection of an open Asyncore socket.
Calling the socket.close() nor the socket.shutdown(2) calls seem to
work. The only way I can close the connection without creating the
error below is to have the client close the connection.

You have to use the dispatcher's close() method, else, the asyncore map
won't be updated, keeping a reference to the closed socket.
 
J

JamesHoward

Thank you for the responses. I have learned considerably more about
how Asyncore works because of it.

The problem that I see is that Asyncore's poll function does not seem
to be thread safe. From what I can tell, I am calling
dispatcher.close() properly and the dispatchers are removed from
asyncore's global map (all except the server itself). However, it
seems like the error happens when the poll function gets the file
descriptors to run select on and then the thread ticks and removes them
from the global map. After this the select call is made, but the file
descriptors are not valid anymore.

I guess I have two questions as a result. First, is this a problem
that anyone else has had and second is there a fix for it? I have
tried looking for Asyncore thread safe topics in Google, but without
much luck. If needed I think making the poll function atomic in the
asyncore module might fix this problem, but I wanted to see what other
people thought first.

Thanks again for the help,
Jim Howard
 
G

Gabriel Genellina

Thank you for the responses. I have learned considerably more about
how Asyncore works because of it.

The problem that I see is that Asyncore's poll function does not seem
to be thread safe. From what I can tell, I am calling
dispatcher.close() properly and the dispatchers are removed from
asyncore's global map (all except the server itself). However, it
seems like the error happens when the poll function gets the file
descriptors to run select on and then the thread ticks and removes them
from the global map. After this the select call is made, but the file
descriptors are not valid anymore.

I guess I have two questions as a result. First, is this a problem
that anyone else has had and second is there a fix for it? I have
tried looking for Asyncore thread safe topics in Google, but without
much luck. If needed I think making the poll function atomic in the
asyncore module might fix this problem, but I wanted to see what other
people thought first.

Usually asyncore is used with only one thread - that's one of the
reasons of using it btw. You can spawn other working threads, but
they don't usually interact with the networking stuff directly.


--
Gabriel Genellina
Softlab SRL






__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas
 
J

JamesHoward

Again, thank you for your help. With digging through the Asyncore.py
source, I was able to find the poll2 function which is called when the
function asyncore.loop(use_poll = True) is enabled.

This function does not use a select call, but a poll call to do its
looping. It works well for the problem of threads closing devices at
unknown times. The reason for this is that the select statement is
called on a series of file descriptors that should not be changed. If
the file descriptors become invalid, select throws and exception and
the asyncore loop haults. The use_poll flag sets the asyncore module
to use a poll instead of a select statement.

Within the poll method, there are better ways of dealing with file
descriptors and they seem to be able to discern if a file descriptor
becomes disconnected with the POLLHUP flag.

I am still unsure as to why the select function is used instead of the
poll function, but using poll appears to have solved my problem.

Thanks for all the help,
Jim Howard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top