This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: control-c is being sent to child thread rather than main
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 2.6
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: BreamoreBoy, ajaksu2, mwh, sheshi
Priority: normal Keywords:

Created on 2003-11-21 19:46 by sheshi, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (4)
msg60424 - (view) Author: sheshi sankineni (sheshi) Date: 2003-11-21 19:46
Hi All,

We just migrated from Python-2.2.1 to Python-2.3.1 and 
noticed a behavior change with respect to control-c + 
threads  and importing readline module. We have 
developed a CLI (command line interface) to our 
hardware using Python (communicating with CORBA). 

I am providing you with the code (below) which 
demonstrates the changed behavior in Python-2.3.1 
with "import readline" & without "import readline" on Linux 
Redhat 7.2. The same program works fine on Python-
2.2.1 with or without importing readline and doing 
control-c ALWAYS trapped by the main program.

Any help on this issue will be appreciated

thanks

-sheshi


cli.py
-------

import os
import signal
import sys
import cliEngine

def handleKeyboardInterrupt(signalNumber, frame) :
    print 'MAIN THREAD Control-C'

def handleSigUsr1(signalNumber, frame) :
    pass

# The main function
if __name__ == '__main__':

    try :
        # Register a signal handler for ctrl-C, control-z
        signal.signal(signal.SIGINT, 
handleKeyboardInterrupt)
        signal.signal(signal.SIGUSR1, handleSigUsr1)
        signal.signal(signal.SIGTSTP, signal.SIG_IGN)

        cliEngine.engine = cliEngine.CliEngine()
        cliEngine.engine.setPid(os.getpid())

        cliEngine.engine.start()

        while cliEngine.engine.cliEngineRunning :
            signal.pause()

        print 'Exiting the main thread'

    finally :
        pass


cliEngine.py
-----------------

import os
import signal
import sys
import threading
import readline

engine = None

class CliEngine(threading.Thread) :
    # Static members of the class
    cliEngineRunning = 0
    mainThreadPid = 0

    def __init__(self) :
        threading.Thread.__init__(self)
        self.cliEngineRunning = 1
        engine = self

    def setPid(self, mainPid) :
        self.mainThreadPid = mainPid


    def run(self) :
        print 'calling CliEngine run'
        self.cliEngineRunning = 1
        self.runEngine()

    def runEngine(self) :

        while self.cliEngineRunning :
            try :
                line = raw_input('# ')

                if line == "logout" :
                    self.cliEngineRunning = 0

            except KeyboardInterrupt :
                print 'CHILD THREAD Control-C 
(KeyboardInterrupt)'

            except EOFError :
                # ctrl-d to logout from CLI
                self.cliEngineRunning = 0

            except :
                print 'Unknown Exception'
                self.cliEngineRunning = 0

        print 'Returning from cliEngine.runEngine()'

If I remove "import readline" in the above file, control-c 
is always trapped by the  main thread (cli.py) as shown 
by the output below

>>python cli.py

calling CliEngine run
# MAIN THREAD Control-C
# MAIN THREAD Control-C

# logout
Returning from cliEngine.runEngine()
Exiting the main thread

If in the above Python file (cliEngine.py), If I 
include "import readline", control-c will be trapped by 
the  child thread (CliEngine) instead of the main thread, 
as shown by the output below and the program 
terminates with segmentation fault

>>python cli.py

calling CliEngine run
# CHILD THREAD Control-C (KeyboardInterrupt)
# CHILD THREAD Control-C (KeyboardInterrupt)
# 
# logout
Returning from cliEngine.runEngine()

Segmentation fault
msg60425 - (view) Author: Michael Hudson (mwh) (Python committer) Date: 2005-04-07 09:14
Logged In: YES 
user_id=6656

Is this still an issue with Python 2.4?
msg81880 - (view) Author: Daniel Diniz (ajaksu2) * (Python triager) Date: 2009-02-13 04:59
Closing proposed.

Cannot reproduce as of r69550:

calling CliEngine run
#
# MAIN THREAD Control-C
MAIN THREAD Control-C
[...]
Returning from cliEngine.runEngine()
MAIN THREAD Control-C
Exiting the main thread

On Ubuntu 8.04 (2.6,24) with libreadline 5.2.
msg114299 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2010-08-18 23:29
Closed as no reply to msg81880.
History
Date User Action Args
2022-04-11 14:56:01adminsetgithub: 39581
2013-02-24 01:10:54r.david.murraysetstatus: open -> closed
resolution: out of date
stage: test needed -> resolved
2010-08-18 23:29:51BreamoreBoysetnosy: + BreamoreBoy
messages: + msg114299
2009-02-13 04:59:53ajaksu2settype: behavior
stage: test needed
messages: + msg81880
nosy: + ajaksu2
versions: + Python 2.6, - Python 2.3
2003-11-21 19:46:25sheshicreate