472,984 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,984 software developers and data experts.

Bug in socket module?

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@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
Jul 18 '05 #1
1 1530
At some point, Koen Vossen <ko*********@pandora.be> wrote:
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@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?


I think that's a bug. I looked at this and opened a bug report for you on
SourceForge (http://python.org/sf/926423). It's got my explanation of
what I think is going wrong.

There's a work around. This should work:

try:
try:
data, address = s.recvfrom(1024)
except KeyboardInterrupt:
print 'not triggered'
except KeyboardInterrupt:
print 'this should trigger'

So, put your error-handling code in the outermost try..except block.

--
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)physics(dot)mcmaster(dot)ca
Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jacob Lee | last post by:
I'm getting a rather bizarre error while using the socket module. If I start out disconnected from the net and then connect while the program or interpreter session is open, I do not always gain...
1
by: bobano | last post by:
Hi everyone, I am writing a POP3 Client program in Perl. You connect to a POP3 Server and have a running conversation with the mail server using commands from the RFC 1939 Post Office Protocol....
9
by: Irmen de Jong | last post by:
Hi, Recently I was bitten by an apparent bug in the BSD socket layer on Open VMS. Specifically, it appears that VMS defines MSG_WAITALL in socket.h but does not implement it (it is not in the...
2
by: John Nagle | last post by:
Here's a strange little bug. "socket.getaddrinfo" blows up if given a bad domain name containing ".." in Unicode. The same string in ASCII produces the correct "gaierror" exception. Actually,...
10
by: John Nagle | last post by:
Here are three network-related exceptions. These were caught by "except" with no exception type, because none of the more specific exceptions matched. This is what a traceback produced: 1....
14
by: ahlongxp | last post by:
Hi, everyone, I'm implementing a simple client/server protocol. Now I've got a situation: client will send server command,header paires and optionally body. server checks headers and decides...
2
by: Mirko Vogt | last post by:
Hey, it seems that the socket-module behaves differently on unix / windows when a timeout is set. Here an example: # test.py import socket...
0
by: Gabriel Genellina | last post by:
En Wed, 09 Jul 2008 15:02:56 -0300, Mirko Vogt <lists@nanl.deescribi�: Which Python version? Which Windows version? I've tried 2.3.4, 2.4.4, 2.5.1 and 3.0a4, all on WinXP SP2, and in all...
0
by: Mirko Vogt | last post by:
Gabriel Genellina wrote: Hey, this is strange. Linux: $ python --version Python 2.5.2 $ Windows:
0
by: Jean-Paul Calderone | last post by:
On Sat, 23 Aug 2008 02:25:17 +0800, Leo Jay <python.leojay@gmail.comwrote: No - it's just what I said. create_socket creates one socket and passes it to read_socket and write_socket. ...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.