コード例 #1
0
ファイル: job.py プロジェクト: IrinaMax/h2o-3
 def signal_handler(self, signum, stackframe):
     """(internal)."""
     if self._polling:
         h2o.api("POST /3/Jobs/%s/cancel" % self.job_key)
         print("Job {} was cancelled.".format(self.job_key))
     else:
         signal.default_int_handler()
コード例 #2
0
def terminate(signal, frame):
    # This assumes that all the child processes will terminate gracefully
    # on a SIGINT
    global MANAGER
    MANAGER.stop()

    # Raise SIGINT to all child processes.
    signal.default_int_handler()
コード例 #3
0
def _interrupt_handler(signum, frame):
    # if a keyboard-interrupt occurs...
    if _do_catch_interrupt:
        # push a Ctrl+C (ascii value 3) to the curses getch stack
        curses.ungetch(3)
    else:
        # use the default handler
        signal.default_int_handler(signum, frame)
コード例 #4
0
ファイル: colorizer.py プロジェクト: aclements/my-bin
 def __call__(self, signum, stack):
     if self.dieOnNext:
         if self.child:
             self.msg("Killing child %d with signal 9" % self.child)
             os.kill(-self.child, signal.SIGKILL)
         signal.default_int_handler()
     else:
         self.dieOnNext = True
         if self.child:
             self.msg("Interrupt: Waiting for child %s" % self.child)
             os.kill(-self.child, signal.SIGINT)
コード例 #5
0
ファイル: SaneThread.py プロジェクト: BackupTheBerlios/eikazo
def thrdhandler(sig, frame):
    print "caught signal", sig, frame
    for thread in threads:
        thread.abort = 1
    l = _lasthandler[sig]
    if l == signal.SIG_DFL:
        # FIXME: this leads to a misleading exception/error message.
        # Unfortnately,I don't know any simple way to invoke the "real" 
        # default signal handler
        signal.default_int_handler(sig, frame)
    elif l != signal.SIG_IGN:
        l(sig, frame)
コード例 #6
0
ファイル: process.py プロジェクト: chencoyote/RETask
    def kill_children(self, *args):
        '''
        Kill all of the children
        '''
        # check that this is the correct process, children inherit this
        # handler, if we are in a child lets just run the original handler
        if os.getpid() != self._PM_pid:
            if callable(self._sigterm_handler):
                return self._sigterm_handler(*args)
            elif self._sigterm_handler is not None:
                return signal.default_int_handler(signal.SIGTERM)(*args)
            else:
                return

        for pid, p_map in self._process_map.items():
            p_map['Process'].terminate()

        end_time = time.time() + self.wait_for_kill  # when to die

        while self._process_map and time.time() < end_time:
            for pid, p_map in self._process_map.items():
                p_map['Process'].join(0)

                # This is a race condition if a signal was passed to all children
                try:
                    del self._process_map[pid]
                except KeyError:
                    pass
        # if anyone is done after
        for pid in self._process_map:
            try:
                os.kill(signal.SIGKILL, pid)
            # in case the process has since decided to die, os.kill returns OSError
            except OSError:
                pass
コード例 #7
0
ファイル: pipeline.py プロジェクト: health1987/paleomix
 def _sigint_handler(self, signum, frame):
     """Signal handler; see signal.signal."""
     if not self._interrupted:
         self._interrupted = True
         self._logger.error("\nKeyboard interrupt detected, waiting for "
                            "current tasks to complete ... Press CTRL-C "
                            "again to force termination.\n")
     else:
         raise signal.default_int_handler(signum, frame)
コード例 #8
0
ファイル: process.py プロジェクト: dmyerscough/salt
    def kill_children(self, *args):
        '''
        Kill all of the children
        '''
        # check that this is the correct process, children inherit this
        # handler, if we are in a child lets just run the original handler
        if os.getpid() != self._pid:
            if callable(self._sigterm_handler):
                return self._sigterm_handler(*args)
            elif self._sigterm_handler is not None:
                return signal.default_int_handler(signal.SIGTERM)(*args)
            else:
                return
        if salt.utils.is_windows():
            with open(os.devnull, 'wb') as devnull:
                for pid, p_map in six.iteritems(self._process_map):
                    # On Windows, we need to explicitly terminate sub-processes
                    # because the processes don't have a sigterm handler.
                    subprocess.call(
                        ['taskkill', '/F', '/T', '/PID', str(pid)],
                        stdout=devnull, stderr=devnull
                        )
                    p_map['Process'].terminate()
        else:
            for p_map in six.itervalues(self._process_map):
                p_map['Process'].terminate()

        end_time = time.time() + self.wait_for_kill  # when to die

        while self._process_map and time.time() < end_time:
            for pid, p_map in six.iteritems(self._process_map.copy()):
                p_map['Process'].join(0)

                # This is a race condition if a signal was passed to all children
                try:
                    del self._process_map[pid]
                except KeyError:
                    pass
        # if anyone is done after
        for pid in self._process_map:
            try:
                os.kill(signal.SIGKILL, pid)
            # in case the process has since decided to die, os.kill returns OSError
            except OSError:
                pass
コード例 #9
0
ファイル: job.py プロジェクト: Kaushik512/h2o-3
 def signal_handler(self, signum, stackframe):
   if POLLING:
     H2OConnection.post(url_suffix="Jobs/" + self.job_key + "/cancel")
     print("Job {} was cancelled.".format(self.job_key))
   else:
     signal.default_int_handler()
コード例 #10
0
ファイル: _ossighelper.py プロジェクト: GNOME/pygobject
def register_sigint_fallback(callback):
    """Installs a SIGINT signal handler in case the default Python one is
    active which calls 'callback' in case the signal occurs.

    Only does something if called from the main thread.

    In case of nested context managers the signal handler will be only
    installed once and the callbacks will be called in the reverse order
    of their registration.

    The old signal handler will be restored in case no signal handler is
    registered while the context is active.
    """

    # To handle multiple levels of event loops we need to call the last
    # callback first, wait until the inner most event loop returns control
    # and only then call the next callback, and so on... until we
    # reach the outer most which manages the signal handler and raises
    # in the end

    global _callback_stack, _sigint_called

    if not is_main_thread():
        yield
        return

    if not sigint_handler_is_default():
        if _callback_stack:
            # This is an inner event loop, append our callback
            # to the stack so the parent context can call it.
            _callback_stack.append(callback)
            try:
                yield
            finally:
                cb = _callback_stack.pop()
                if _sigint_called:
                    cb()
        else:
            # There is a signal handler set by the user, just do nothing
            yield
        return

    _sigint_called = False

    def sigint_handler(sig_num, frame):
        global _callback_stack, _sigint_called

        if _sigint_called:
            return
        _sigint_called = True
        _callback_stack.pop()()

    _callback_stack.append(callback)
    try:
        with sigint_handler_set_and_restore_default(sigint_handler):
            yield
    finally:
        if _sigint_called:
            signal.default_int_handler(signal.SIGINT, None)
        else:
            _callback_stack.pop()
コード例 #11
0
 def _inner(_, __):
     cleanup(ifindex, safe=True)
     signal.default_int_handler(_, __)
コード例 #12
0
ファイル: process.py プロジェクト: EIChaoYang/avocado
 def signal_handler(signum, frame):
     self.result.interrupted = "signal/ctrl+c"
     self.wait()
     signal.default_int_handler()
コード例 #13
0
    def kill_children(self, *args, **kwargs):
        '''
        Kill all of the children
        '''
        # first lets reset signal handlers to default one to prevent running this twice
        signal.signal(signal.SIGTERM, signal.SIG_IGN)
        signal.signal(signal.SIGINT, signal.SIG_IGN)

        # check that this is the correct process, children inherit this
        # handler, if we are in a child lets just run the original handler
        if os.getpid() != self._pid:
            if callable(self._sigterm_handler):
                return self._sigterm_handler(*args)
            elif self._sigterm_handler is not None:
                return signal.default_int_handler(signal.SIGTERM)(*args)
            else:
                return
        if salt.utils.is_windows():
            if multiprocessing.current_process().name != 'MainProcess':
                # Since the main process will kill subprocesses by tree,
                # no need to do anything in the subprocesses.
                # Sometimes, when both a subprocess and the main process
                # call 'taskkill', it will leave a 'taskkill' zombie process.
                # We want to avoid this.
                return
            with salt.utils.fopen(os.devnull, 'wb') as devnull:
                for pid, p_map in six.iteritems(self._process_map):
                    # On Windows, we need to explicitly terminate sub-processes
                    # because the processes don't have a sigterm handler.
                    subprocess.call(
                        ['taskkill', '/F', '/T', '/PID', str(pid)],
                        stdout=devnull, stderr=devnull
                        )
                    p_map['Process'].terminate()
        else:
            for pid, p_map in six.iteritems(self._process_map.copy()):
                log.trace('Terminating pid {0}: {1}'.format(pid, p_map['Process']))
                if args:
                    # escalate the signal to the process
                    try:
                        os.kill(pid, args[0])
                    except OSError:
                        pass
                try:
                    p_map['Process'].terminate()
                except OSError as exc:
                    if exc.errno not in (errno.ESRCH, errno.EACCES):
                        raise
                if not p_map['Process'].is_alive():
                    try:
                        del self._process_map[pid]
                    except KeyError:
                        # Race condition
                        pass

        end_time = time.time() + self.wait_for_kill  # when to die

        log.trace('Waiting to kill process manager children')
        while self._process_map and time.time() < end_time:
            for pid, p_map in six.iteritems(self._process_map.copy()):
                log.trace('Joining pid {0}: {1}'.format(pid, p_map['Process']))
                p_map['Process'].join(0)

                if not p_map['Process'].is_alive():
                    # The process is no longer alive, remove it from the process map dictionary
                    try:
                        del self._process_map[pid]
                    except KeyError:
                        # This is a race condition if a signal was passed to all children
                        pass

        # if any managed processes still remain to be handled, let's kill them
        kill_iterations = 2
        while kill_iterations >= 0:
            kill_iterations -= 1
            for pid, p_map in six.iteritems(self._process_map.copy()):
                if not p_map['Process'].is_alive():
                    # The process is no longer alive, remove it from the process map dictionary
                    try:
                        del self._process_map[pid]
                    except KeyError:
                        # This is a race condition if a signal was passed to all children
                        pass
                    continue
                log.trace('Killing pid {0}: {1}'.format(pid, p_map['Process']))
                try:
                    os.kill(pid, signal.SIGKILL)
                except OSError as exc:
                    log.exception(exc)
                    # in case the process has since decided to die, os.kill returns OSError
                    if not p_map['Process'].is_alive():
                        # The process is no longer alive, remove it from the process map dictionary
                        try:
                            del self._process_map[pid]
                        except KeyError:
                            # This is a race condition if a signal was passed to all children
                            pass

        if self._process_map:
            # Some processes disrespected the KILL signal!!!!
            available_retries = kwargs.get('retry', 3)
            if available_retries >= 0:
                log.info(
                    'Some processes failed to respect the KILL signal: %s',
                        '; '.join(
                            'Process: {0} (Pid: {1})'.format(v['Process'], k) for
                            (k, v) in self._process_map.items()
                        )
                )
                log.info('kill_children retries left: %s', available_retries)
                kwargs['retry'] = available_retries - 1
                return self.kill_children(*args, **kwargs)
            else:
                log.warning(
                    'Failed to kill the following processes: %s',
                    '; '.join(
                        'Process: {0} (Pid: {1})'.format(v['Process'], k) for
                        (k, v) in self._process_map.items()
                    )
                )
                log.warning(
                    'Salt will either fail to terminate now or leave some '
                    'zombie processes behind'
                )
コード例 #14
0
ファイル: process.py プロジェクト: HowardMei/saltstack
    def kill_children(self, *args, **kwargs):
        '''
        Kill all of the children
        '''
        # check that this is the correct process, children inherit this
        # handler, if we are in a child lets just run the original handler
        if os.getpid() != self._pid:
            if callable(self._sigterm_handler):
                return self._sigterm_handler(*args)
            elif self._sigterm_handler is not None:
                return signal.default_int_handler(signal.SIGTERM)(*args)
            else:
                return
        if salt.utils.is_windows():
            with open(os.devnull, 'wb') as devnull:
                for pid, p_map in six.iteritems(self._process_map):
                    # On Windows, we need to explicitly terminate sub-processes
                    # because the processes don't have a sigterm handler.
                    subprocess.call(
                        ['taskkill', '/F', '/T', '/PID', str(pid)],
                        stdout=devnull, stderr=devnull
                        )
                    p_map['Process'].terminate()
        else:
            for pid, p_map in six.iteritems(self._process_map.copy()):
                log.trace('Terminating pid {0}: {1}'.format(pid, p_map['Process']))
                if args:
                    # escalate the signal to the process
                    os.kill(pid, args[0])
                try:
                    p_map['Process'].terminate()
                except OSError as exc:
                    if exc.errno != errno.ESRCH:
                        raise
                if not p_map['Process'].is_alive():
                    try:
                        del self._process_map[pid]
                    except KeyError:
                        # Race condition
                        pass

        end_time = time.time() + self.wait_for_kill  # when to die

        log.trace('Waiting to kill process manager children')
        while self._process_map and time.time() < end_time:
            for pid, p_map in six.iteritems(self._process_map.copy()):
                log.trace('Joining pid {0}: {1}'.format(pid, p_map['Process']))
                p_map['Process'].join(0)

                if not p_map['Process'].is_alive():
                    # The process is no longer alive, remove it from the process map dictionary
                    try:
                        del self._process_map[pid]
                    except KeyError:
                        # This is a race condition if a signal was passed to all children
                        pass

        # if any managed processes still remain to be handled, let's kill them
        kill_iterations = 2
        while kill_iterations >= 0:
            kill_iterations -= 1
            for pid, p_map in six.iteritems(self._process_map.copy()):
                if not p_map['Process'].is_alive():
                    # The process is no longer alive, remove it from the process map dictionary
                    try:
                        del self._process_map[pid]
                    except KeyError:
                        # This is a race condition if a signal was passed to all children
                        pass
                    continue
                log.trace('Killing pid {0}: {1}'.format(pid, p_map['Process']))
                try:
                    os.kill(signal.SIGKILL, pid)
                except OSError:
                    # in case the process has since decided to die, os.kill returns OSError
                    if not p_map['Process'].is_alive():
                        # The process is no longer alive, remove it from the process map dictionary
                        try:
                            del self._process_map[pid]
                        except KeyError:
                            # This is a race condition if a signal was passed to all children
                            pass

        if self._process_map:
            # Some processes disrespected the KILL signal!!!!
            available_retries = kwargs.get('retry', 3)
            if available_retries >= 0:
                log.info(
                    'Some processes failed to respect the KILL signal: %s',
                        '; '.join(
                            'Process: {0} (Pid: {1})'.format(v['Process'], k) for
                            (k, v) in self._process_map.items()
                        )
                )
                log.info('kill_children retries left: %s', available_retries)
                kwargs['retry'] = available_retries - 1
                return self.kill_children(*args, **kwargs)
            else:
                log.warning(
                    'Failed to kill the following processes: %s',
                    '; '.join(
                        'Process: {0} (Pid: {1})'.format(v['Process'], k) for
                        (k, v) in self._process_map.items()
                    )
                )
                log.warning(
                    'Salt will either fail to terminate now or leave some '
                    'zombie processes behind'
                )
コード例 #15
0
 def forceful_handler(sig, frame):
     print("Second SIGINT received. Triggering "
           "KeyboardInterrupt immediately.")
     log.info('Second SIGINT received. Triggering '
              'KeyboardInterrupt immediately.')
     signal.default_int_handler(sig, frame)
コード例 #16
0
ファイル: process.py プロジェクト: ismagulb/avocado
 def signal_handler(signum, frame):
     self.result.interrupted = "signal/ctrl+c"
     self.wait()
     signal.default_int_handler()
コード例 #17
0
ファイル: app.py プロジェクト: mullikine/ptghci
def int_handler(sig, frame):
    #print("Python - Got SIGINT")
    signal.default_int_handler(sig, frame)
コード例 #18
0
    def kill_children(self, *args):
        '''
        Kill all of the children
        '''
        # check that this is the correct process, children inherit this
        # handler, if we are in a child lets just run the original handler
        if os.getpid() != self._pid:
            if callable(self._sigterm_handler):
                return self._sigterm_handler(*args)
            elif self._sigterm_handler is not None:
                return signal.default_int_handler(signal.SIGTERM)(*args)
            else:
                return
        if salt.utils.is_windows():
            with open(os.devnull, 'wb') as devnull:
                for pid, p_map in six.iteritems(self._process_map):
                    # On Windows, we need to explicitly terminate sub-processes
                    # because the processes don't have a sigterm handler.
                    subprocess.call(['taskkill', '/F', '/T', '/PID',
                                     str(pid)],
                                    stdout=devnull,
                                    stderr=devnull)
                    p_map['Process'].terminate()
        else:
            for pid, p_map in six.iteritems(self._process_map.copy()):
                log.trace('Terminating pid {0}: {1}'.format(
                    pid, p_map['Process']))
                if args:
                    # escalate the signal to the process
                    os.kill(pid, args[0])
                try:
                    p_map['Process'].terminate()
                except OSError as exc:
                    if exc.errno != errno.ESRCH:
                        raise
                if not p_map['Process'].is_alive():
                    try:
                        del self._process_map[pid]
                    except KeyError:
                        # Race condition
                        pass

        end_time = time.time() + self.wait_for_kill  # when to die

        log.trace('Waiting to kill process manager children')
        while self._process_map and time.time() < end_time:
            for pid, p_map in six.iteritems(self._process_map.copy()):
                log.trace('Joining pid {0}: {1}'.format(pid, p_map['Process']))
                p_map['Process'].join(0)

                if not p_map['Process'].is_alive():
                    # The process is no longer alive, remove it from the process map dictionary
                    try:
                        del self._process_map[pid]
                    except KeyError:
                        # This is a race condition if a signal was passed to all children
                        pass

        # if any managed processes still remain to be handled, let's kill them
        for pid, p_map in six.iteritems(self._process_map):
            if not p_map['Process'].is_alive():
                # If the process is no longer alive, carry on
                continue
            log.trace('Killing pid {0}: {1}'.format(pid, p_map['Process']))
            try:
                os.kill(signal.SIGKILL, pid)
            except OSError:
                # in case the process has since decided to die, os.kill returns OSError
                pass
コード例 #19
0
 def signal_handler(self, signum, stackframe):
     if POLLING:
         H2OConnection.post(url_suffix="Jobs/" + self.job_key + "/cancel")
         print("Job {} was cancelled.".format(self.job_key))
     else:
         signal.default_int_handler()
コード例 #20
0
 def handle_sigint(signum, frame):
   # Ignore any new SIGINTs since we are already tearing down.
   signal.signal(signal.SIGINT, signal.SIG_IGN)
   # Execute the default SIGINT handler which throws a KeyboardInterrupt
   # in the main thread of the process.
   signal.default_int_handler(signum, frame)
コード例 #21
0
 def handle_sigint(signum, frame):
     # Ignore any new SIGINTs since we are already tearing down.
     signal.signal(signal.SIGINT, signal.SIG_IGN)
     # Execute the default SIGINT handler which throws a KeyboardInterrupt
     # in the main thread of the process.
     signal.default_int_handler(signum, frame)
コード例 #22
0
ファイル: process.py プロジェクト: mxie91/avocado
 def signal_handler(signum, frame):  # pylint: disable=W0613
     self.result.interrupted = "signal/ctrl+c"
     self.wait()
     signal.default_int_handler()
コード例 #23
0
 def chatty_interrupt(self, signum: int, frame) -> None:
     """ Executed when SIGINT/SIGTERM is emitted during alert supplier execution """
     self.print_feedback(signum, "(outside of alert processing)")
     self._cancel_run = signum
     default_int_handler(signum, frame)