Bug in socket module?

Koen Vossen koen_vossen at pandora.be
Tue Mar 30 17:36:39 EST 2004


Hi, 

I came across some exceptional behaviour (pun intended) when I was
programming a small chat server. I've simplified my original code to the
following minimal version that still exhibits the same problem:

<code>
#!/usr/bin/env python

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(('', 1234))
s.settimeout(10)

try:
    print "The Meaning of Life."
    data, address = s.recvfrom(1024)
except KeyboardInterrupt:
    pass
</code>

When I start this little snippet, it just sits waiting for the socket to
return some data, as expected. However, when I press CTRL-C before the
timeout occurs it crashes on a KeyboardInterrupt exception, despite the
fact that I have set up a handler to catch the latter.

<output>
koen at s031845:~/workdir> ./weird.py
The Meaning of Life.
Traceback (most recent call last):
  File "./weird.py", line 11, in ?
    data, address = s.recvfrom(1024)
KeyboardInterrupt
</output>

When I omit the settimeout method call, or when I replace recvfrom by
raw_input, the program behaves as expected.

I'm using the following version of python:
Python 2.3+ (#1, Jan  7 2004, 09:17:35)
[GCC 3.3.1 (SuSE Linux)] on linux2
I've found that on Windows the same behaviour occurs.

Am I onto something here?

Koen Vossen
-- 
Replace the underscore by a dot to obtain my email address.
Public PGP key available @ pgp.mit.edu



More information about the Python-list mailing list