[ python-Bugs-846817 ] control-c is being sent to child thread rather than main

SourceForge.net noreply at sourceforge.net
Fri Nov 21 14:46:25 EST 2003


Bugs item #846817, was opened at 2003-11-21 19:46
Message generated for change (Tracker Item Submitted) made by Item Submitter
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=846817&group_id=5470

Category: Threads
Group: Python 2.3
Status: Open
Resolution: None
Priority: 5
Submitted By: sheshi sankineni (sheshi)
Assigned to: Nobody/Anonymous (nobody)
Summary: control-c is being sent to child thread rather than main

Initial Comment:
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

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=846817&group_id=5470



More information about the Python-bugs-list mailing list