Signal SIGINT ignored during socket.accept

James Harris james.harris.1 at gmail.com
Thu Sep 10 22:12:40 CEST 2015


"Chris Angelico" <rosuav at gmail.com> wrote in message 
news:mailman.337.1441913195.8327.python-list at python.org...
> On Fri, Sep 11, 2015 at 5:11 AM, James Harris 
> <james.harris.1 at gmail.com> wrote:

...

>> However, on Windows the recognition of Control-C does not happen 
>> until after
>> something connects to the socket.

...

> This is a known problem on Windows.

...

> It's entirely possible that a blocking socket-accept call will
> continue to block. There is one rather silly option, and that's to use
> select() to effectively poll for Ctrl-C... or, possibly better, have a
> separate program that shuts down your server (by connecting to it,
> which thus breaks the stranglehold).

Thanks for your help, Chris. Using select() is a very good option. I 
tried first without a timeout and even then this version of Windows does 
not recognise Control-C until after the select() call returns (which 
needs similar prompting as with the accept() call. However, select() 
with a timeout allows the code to work both on Windows and Linux. 
Hooray!

For anyone else who is looking for this the earlier test code was 
changed to

port = 8880
import select
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setblocking(0)
s.bind(("", port))
s.listen(1)
while 1:
  ready = select.select((s,), (), (), 0.5)
  #print '(ready %s)' % repr(ready)
  if (ready[0]):
    try:
      endpoint = s.accept()
    except socket.error, details:
      print 'Ignoring socket error:', repr(details)
      continue
    print '(endpoint %s)' % repr(endpoint)
    if endpoint:
      break

James



More information about the Python-list mailing list