Socket Troubles

C

Chris Spencer

I've written a simple class to manage P2P socket connections. However,
whenever I try to receive data, the socket raises an exception with the
error message (11, 'Resource temporarily unavailable').

My code's fairly straight-forward, with much of it right out of the
Python docs, so I'm not sure what I'm doing wrong. You can see it all at
http://deadbeefbabe.org/paste/1525/0

Any help is immensely appreciated.

Thanks,
Chris
 
C

Chris Spencer

Chris said:
I've written a simple class to manage P2P socket connections. However,
whenever I try to receive data, the socket raises an exception with the
error message (11, 'Resource temporarily unavailable').

My code's fairly straight-forward, with much of it right out of the
Python docs, so I'm not sure what I'm doing wrong. You can see it all at
http://deadbeefbabe.org/paste/1525/0

Any help is immensely appreciated.

Thanks,
Chris

One more thing. The code I posted is also a complete demo of my problem.
Run the script in two different terminals simultaneously, like
script.py 8000 8001
and
script.py 8001 8000

They should then try talking to each other, reproducing the problem.

Chris
 
P

Peter Hansen

Chris said:
I've written a simple class to manage P2P socket connections. However,
whenever I try to receive data, the socket raises an exception with the
error message (11, 'Resource temporarily unavailable').

I would assume (without looking at your code) that this is equivalent to
the Windows error "WSAEWOULDBLOCK" (10035) for which Microsoft's docs
say this:

Resource temporarily unavailable.
This error is returned from operations on nonblocking sockets that
cannot be completed immediately, for example recv when no data is queued
to be read from the socket. It is a nonfatal error, and the operation
should be retried later. It is normal for WSAEWOULDBLOCK to be reported
as the result from calling connect on a nonblocking SOCK_STREAM socket,
since some time must elapse for the connection to be established.


Does that help? If not, at least provide information about what
platform you're running on, and preferably post the code, if you can
reduce it to only a few lines which still reproduces the problem. Many
people don't like to spend time downloading code from who knows where
and attempting to run it on their own machine, but could find the
problem with a quick inspection of your code if posted here (but not if
it's hundreds of lines!).

-Peter
 
M

Michael Sparks

Chris said:
My code's ... at http://deadbeefbabe.org/paste/1525/0 ....
I've written a simple class to manage P2P socket connections. However,
whenever I try to receive data, the socket raises an exception with the
error message (11, 'Resource temporarily unavailable').

At one point in your code you do this:
self._socket.setblocking(0)

This says "if we can't recieve or send data without blocking, fail rather
than succeed". One of the failure modes is to return error 11. This is
infact normally defined as:
#define EAGAIN 11 /* Try again */

What this means is "sorry, I couldn't do this without blocking right now,
try again very shortly!".

Looking at your code it continually loops (in BaseConnectionHandler_recv)
receiving data - whether or not there's any data to receive:
def _recv(self):
while self.running:
try:
data = self._socket.recv(4096)
if not len(data):
time.sleep(0.1)
continue
except Exception, e:
log('Recieve failed for handler',self.address,'because of',e)

break

Since you never actually check to see if the socket is ready to give you
data, and you've set it non-blocking, seeing lots of EAGAIN errors is
pretty much what you'd expect to see. (Simply sleeping is not sufficient!)

I suppose the short answer though really is this: you set the socket
non-blocking, you should therefore expect to see failures telling you
to try again, and follow their advice! :)

Regards,


Michael.
 
D

Dennis Lee Bieber

I would assume (without looking at your code) that this is equivalent to
the Windows error "WSAEWOULDBLOCK" (10035) for which Microsoft's docs
say this:
In my experience, winsock error codes map directly to common socket
error numbers -> 10035 is 35... 11 would then be 10011.

However, a scan of the relevant .H files shows that (100)11 is NOT
one of the winsock reported variations.
--
 
C

Chris Spencer

Michael said:
Chris Spencer wrote:

At one point in your code you do this:
self._socket.setblocking(0)

This says "if we can't recieve or send data without blocking, fail rather
than succeed". One of the failure modes is to return error 11. This is
infact normally defined as:
#define EAGAIN 11 /* Try again */

What this means is "sorry, I couldn't do this without blocking right now,
try again very shortly!".

Looking at your code it continually loops (in BaseConnectionHandler_recv)
receiving data - whether or not there's any data to receive:
def _recv(self):
while self.running:
try:
data = self._socket.recv(4096)
if not len(data):
time.sleep(0.1)
continue
except Exception, e:
log('Recieve failed for handler',self.address,'because of',e)

break

Since you never actually check to see if the socket is ready to give you
data, and you've set it non-blocking, seeing lots of EAGAIN errors is
pretty much what you'd expect to see. (Simply sleeping is not sufficient!)

I suppose the short answer though really is this: you set the socket
non-blocking, you should therefore expect to see failures telling you
to try again, and follow their advice! :)

You're quite right. I fixed this by using select(). However, I was still
having problems with open() blocking the main thread. Then I realized a
slight problem:
t = threading.Thread(target=self._connection_handler(h))

I changed this to:
t = threading.Thread(target=self._connection_handler, args=(h,))
and now it appears to be working correctly.

Thanks for your help! I truly appreciate it.

Chris
 

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
474,264
Messages
2,571,315
Members
47,996
Latest member
LaurenFola

Latest Threads

Top