Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7406347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:32:37+00:00 2026-05-29T05:32:37+00:00

I have a server script that I need to be able to shutdown cleanly.

  • 0

I have a server script that I need to be able to shutdown cleanly. While testing the usual try..except statements I realized that Ctrl-C didn’t work the usual way. Normally I’d wrap long running tasks like this

try:
    ...
except KeyboardInterrupt:
    #close the script cleanly here

so the task could be shutdown cleanly on Ctrl-C. I have never ran into any problems with this before, but somehow when I hit Ctrl-C when this particular script is running the script just exits without catching the Ctrl-C.

The initial version was implemented using Process from multiprocessing. I rewrote the script using Thread from threading, but same issue there. I have used threading many times before, but I am new to the multiprocessing library. Either way, I have never experienced this Ctrl-C behavior before.

Normally I have always implemented sentinels etc to close down Queues and Thread instances in an orderly fashion, but this script just exits without any response.

Last, I tried overriding signal.SIGINT as well like this

def handler(signal, frame):
    print 'Ctrl+C'

signal.signal(signal.SIGINT, handler)
...

Here Ctrl+C was actually caught, but the handler doesn’t execute, it never prints anything.

Besides the threading / multiprocessing aspect, parts of the script contains C++ SWIG objects. I don’t know if that has anything to do with it. I am running Python 2.7.2 on OS X Lion.

So, a few questions:

  1. What’s going on here?
  2. How can I debug this?
  3. What do I need to learn in order to understand the root cause?

PLEASE NOTE: The internals of the script is proprietary so I can’t give code examples. I am however very willing to receive pointers so I could debug this myself. I am experienced enough to be able to figure it out if someone could point me in the right direction.

EDIT: I started commenting out imports etc to see what caused the weird behavior, and I narrowed it down to an import of a C++ SWIG library. Any ideas why importing a C++ SWIG library ‘steals’ Ctrl-C? I am not the author of the guilty library however and my SWIG experience is limited so don’t really know where to start…

EDIT 2: I just tried the same script on a windows machine, and in Windows 7 the Ctrl-C is caught as expected. I’m not really going to bother with the OS X part, the script will be run in an Windows environment anyway.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T05:32:38+00:00Added an answer on May 29, 2026 at 5:32 am

    This might have to do with the way Python manages threads, signals and C calls.

    In short – Ctrl-C cannot interrupt C calls, since the implementation requires that a python thread will handle the signal, and not just any thread, but the main thread (often blocked, waiting for other threads).

    In fact, long operations can block everything.

    Consider this:

    >>> nums = xrange(100000000)
    >>> -1 in nums
    False (after  ~ 6.6 seconds)
    >>>
    

    Now, Try hitting Ctrl-C (uninterruptible!)

    >>> nums = xrange(100000000)
    >>> -1 in nums
    ^C^C^C   (nothing happens, long pause)
    ...
    KeyboardInterrupt
    >>>
    

    The reason Ctrl-C doesn’t work with threaded programs is that the main thread is often blocked on an uninterruptible thread-join or lock (e.g, any ‘wait’, ‘join’ or just a plain empty ‘main’ thread, which in the background causes python to ‘join’ on any spawned threads).

    Try to insert a simple

    while True:
        time.sleep(1)
    

    in your main thread.

    If you have a long running C function, do signal handling in C-level (May the Force be with you!).

    This is largely based on David Beazley’s video on the subject.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a server script that I need to pass data to from the
I have a script that connects to SQL Server 2005 default instance. But I'm
I have a script that retrieves objects from a remote server through an Ajax
I have a script that works fine on my test server (using IIS6). The
I have a script that listens to a jabber server and responds accordingly. Though
I have a python script that is a http-server: http://paste2.org/p/89701 , when benchmarking it
I have to maintain a server-side script written in JScript (NOT Javascript) that needs
I have a simple php script on a server that's using fsockopen to connect
I have a PHP script on a server that generates the XML data on
I have a PHP script (running on a Linux server) that ouputs the names

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.