[Twisted-Python] Socket error 22, Invalid Argument in twisted/internet/tcp.py

Werner Thie wthie at thiengineering.ch
Wed Oct 8 12:57:51 EDT 2008

As I wrote in post 
I'm still observing this socket error 22 problem but was not able to dig 
into it any further until now.

I found the following post from the python mailing list, which seems 
related to my problem:


Attchached to this post is a small script, which I extended somehow and 
tried out on various platforms:

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

#nothing is listening on port 9999
   s.connect(('', 9999))
except socket.error, msg:
   print msg
   print s.getsockname()

Win32 XP & VISTA:
(10061, 'Connection refused')
('', 2071)  << socket# seems to be random

Linux Fedora 9:
(111, 'Connection refused')
('', 47521)  << socket# counting up when script run again

Mac OSX 10.5:
(61, 'Connection refused')
('', 49169)  << socket# counting up when script run again

FreeBSD 7.0:
(61, 'Connection refused')
('', 0)  << socket# remains 0 when script run again

OpenBSD 4.3:
(61, 'Connection refused')
Traceback (most recent call last):
   File "socket22error.py", line 8, in <module>
     print s.getsockname()
   File "<string>", line 1, in getsockname
socket.error: (22, 'Invalid argument')

All the OS's seem to agree more or less how this particular situation 
should be handled except OpenBSD.

I agree that this is an Python/OS related question, but what happens 
with the reactor when such an error is thrown?

Is it harmless or does the system loose resources, wait for timeouts?

Is a try/except with a normalized answer in the embracing function in 
twisted/internet/tcp.py a possible cure?

I ask because I experience an unexplainable 'hickup' behavior where the 
odd request which usually triggers an instantaneous response takes up to 
30secs until a response is seen on the client in a local and clean net 

Thxs, Werner

- Yes, I know trial.
- Yes, I am able to set up a build slave for OpenBSD and in fact did so.
- Yes, I requested the creds from exarkun at divmod.com for connecting this 
buildslave on 03.July 2008 17:37 but got no answer
- Yes, I'm willing to recreate the bot which was in the meantime used in 
another project
- No, I don't know if there exists an automated test case for the 
problem given above

More information about the Twisted-Python mailing list