def join(self, timeout=None): r""" Tries to join one or more processes in this spawn context. If one of them exited with a non-zero exit status, this function kills the remaining processes and raises an exception with the cause of the first process exiting. Returns ``True`` if all processes have been joined successfully, ``False`` if there are more processes that need to be joined. Arguments: timeout (float): Wait this long before giving up on waiting. """ # Ensure this function can be called even when we're done. if len(self.sentinels) == 0: return True # Wait for any process to fail or all of them to succeed. ready = multiprocessing.connection.wait( self.sentinels.keys(), timeout=timeout, ) error_index = None for sentinel in ready: index = self.sentinels.pop(sentinel) process = self.processes[index] process.join() if process.exitcode != 0: error_index = index break # Return if there was no error. if error_index is None: # Return whether or not all processes have been joined. return len(self.sentinels) == 0 # Assume failure. Terminate processes that are still alive. for process in self.processes: if process.is_alive(): process.terminate() process.join() # There won't be an error on the queue if the process crashed. if self.error_queues[error_index].empty(): exitcode = self.processes[error_index].exitcode if exitcode < 0: name = signal.Signals(-exitcode).name raise Exception("process %d terminated with signal %s" % (error_index, name)) else: raise Exception("process %d terminated with exit code %d" % (error_index, exitcode)) original_trace = self.error_queues[error_index].get() msg = "\n\n-- Process %d terminated with the following error:\n" % error_index msg += original_trace raise Exception(msg)
def signal_handler(self, sig, frame) -> None: """ Hyperopt SIGINT handler """ logger.info('Hyperopt received %s', signal.Signals(sig).name) self.save_trials() self.log_trials_result() sys.exit(0)
def signal_handler(sig, frame): """ Logs the signal received into LOG_PATH :param sig: signal that has been called :param frame: """ logger = get_logger() logger.warn('Signal received: ' + signal.Signals(sig).name) sys.exit(0)
def signal_handler(signum: int, frame: Any): """Signal handler.""" from PyQt5.QtWidgets import QApplication signame = signal.Signals(signum).name print("\r", flush=True) print(f" ! Caught {signame} ({signum}), gracefully exiting {APP_NAME}", flush=True) QApplication.quit()
def sig_handler(signum, frame): signame = signal.Signals(signum).name log.debug('Received... {}'.format(signame)) if signame == 'SIGTERM': log.info('Program is now shutting down...') loop.call_soon_threadsafe(loop.stop) loop.call_soon_threadsafe(loop.close) log.info('Torn down successfully') sys.exit(0)
def signal_handler(sig_num, frame): """ This is a handler for SIGTERM and SIGINT. Other signals can be mapped here as well (SIGHUP?) Basically it just sets a global flag, and main() will exit it's loop if the signal is trapped. """ # log the associated signal name (the python3 way) logger.warning('Received ' + signal.Signals(sig_num).name) if signal.Signals(sig_num).name == 'SIGINT': logger.info('Terminating dirwatcher -- keyboard interrupt signal') if signal.Signals(sig_num).name == 'SIGTERM': logger.info('Terminating dirwatcher -- OS interrupt signal') global exit_flag exit_flag = True
def signalHandler(signal_received, frame): global __signal_received__ if (__signal_received__ == None): print( str(signal.Signals(signal_received).name) + " received fail fast enabled. Send signal again to force exit") __signal_received__ = signal_received else: exit(1)
def log_atexit(sig, stack=None): sig_enum = None try: sig_enum = signal.Signals(sig) except: pass print('received atexit signal', sig, 'sig_enum', sig_enum, 'stopping process stack', stack) sys.exit()
def parse(cls, buf: bytes) -> 'ChildEvent': struct = ffi.cast('siginfo_t*', ffi.from_buffer(buf)) code = ChildCode(struct.si_code) pid = int(struct.si_pid) uid = int(struct.si_uid) if code is ChildCode.EXITED: return cls(code, pid, uid, int(struct.si_status), None) else: return cls(code, pid, uid, None, signal.Signals(struct.si_status))
def _shutdown_signal(self, sig, frame): self._logger.warning(f'Signal {sig} received - preparing shutdown.') broadcast.warning(_( 'Heating service received signal %(name)s(%(sig)d), shutting down.', name=signal.Signals(sig).name, sig=sig), receiver='bot', source=broadcast.SRC_CONTROLLER) self._shutdown_gracefully(True)
def run_tester(args, test_file): cmd = [ args.tester_binary, "--cluster-file", args.cluster_file, "--test-file", test_file, "--stats-interval", str(TESTER_STATS_INTERVAL_SEC * 1000) ] if args.external_client_library is not None: cmd += ["--external-client-library", args.external_client_library] if args.tmp_dir is not None: cmd += ["--tmp-dir", args.tmp_dir] log_dir = None if args.log_dir is not None: log_dir = Path(args.log_dir).joinpath(random_string(8)) log_dir.mkdir(exist_ok=True) cmd += ['--log', "--log-dir", str(log_dir)] if args.blob_granule_local_file_path is not None: cmd += [ "--blob-granule-local-file-path", args.blob_granule_local_file_path ] if args.tls_ca_file is not None: cmd += ["--tls-ca-file", args.tls_ca_file] if args.tls_key_file is not None: cmd += ["--tls-key-file", args.tls_key_file] if args.tls_cert_file is not None: cmd += ["--tls-cert-file", args.tls_cert_file] get_logger().info('\nRunning tester \'%s\'...' % ' '.join(cmd)) proc = Popen(cmd, stdout=sys.stdout, stderr=sys.stderr) timed_out = False ret_code = 1 try: ret_code = proc.wait(args.timeout) except TimeoutExpired: proc.kill() timed_out = True except Exception as e: raise Exception('Unable to run tester (%s)' % e) if ret_code != 0: if timed_out: reason = 'timed out after %d seconds' % args.timeout elif ret_code < 0: reason = signal.Signals(-ret_code).name else: reason = 'exit code: %d' % ret_code get_logger().error('\n\'%s\' did not complete succesfully (%s)' % (cmd[0], reason)) if (log_dir is not None): dump_client_logs(log_dir) get_logger().info('') return ret_code
def signal_handler(signum: int, _: FrameType, /) -> None: """Signal handler.""" from nxdrive.qt.imports import QApplication signame = signal.Signals(signum).name print("\r", flush=True) print(f" ! Caught {signame} ({signum}), gracefully exiting {APP_NAME}", flush=True) QApplication.quit() QApplication.processEvents()
def deferred_signals(signal_list=None): if signal_list is None: if hasattr(signal, "SIGHUP"): signal_list = [signal.SIGHUP, signal.SIGINT, signal.SIGTERM] else: # pragma: no cover signal_list = [signal.SIGINT, signal.SIGTERM] for deferred_signal in signal_list: signal_name = signal.Signals(deferred_signal).name logger.debug("Deferring signal: %s", signal_name) signal.signal(deferred_signal, signal.SIG_IGN) try: yield finally: for deferred_signal in signal_list: signal_name = signal.Signals(deferred_signal).name logger.debug("Restoring signal: %s", signal_name) signal.signal(deferred_signal, signal.SIG_DFL)
def signal_handler(sig_num, frame): """ This is a handler for SIGTERM and SIGINT. Other signals can be mapped here as well (SIGHUP?) Basically, it just sets a global flag, and main() will exit its loop if the signal is trapped. :param sig_num: The integer signal number that was trapped from the OS. :param frame: Not used :return None""" # Your code here logger.warning('Received: ' + signal.Signals(sig_num).name) global exit_flag exit_flag = True
def signal_handler(sig, frame): global program_running log.warning("Got signal, " + signal.Signals(sig).name + " terminating!") print("Got signal,", signal.Signals(sig).name, "terminating!") try: device.__exit__() log.info("Removed uinput device") print("Removed uinput device") except SystemExit: sys.exit(0) except Exception as e: print("Could not remove uinput device:", e) log.info("Could not remove uinput device:", e) # pid_handler.remove_pid() log.info("----------------------EXITING-----------------------") print("Exiting") program_running = False sys.exit(0)
def force_shutdown(signum, frame): global forced_shutdown if not forced_shutdown: forced_shutdown = True if signum is not None: print("<%s>" % signal.Signals(signum).name) for p in running_solvers.values(): # os.killpg(os.getpgid(p.pid), signal.SIGTERM) os.kill(p.pid, signal.SIGTERM) sys.exit(1)
def signal_handler(_signo, _stack_frame): """ Signal handler for orchestrator. :param _signo: signal number :param _stack_frame: current stack grame :return: None """ logger.info('Received a signal %s, stopping orchestrator...' % signal.Signals(_signo).name) sys.exit(0)
def _debug_dump(self, signum, frame): # pylint: disable=unused-argument try: sig_name = signal.Signals(signum).name # pylint: disable=no-member except Exception: # pylint: disable=broad-except sig_name = str(signum) self.log.info("%s\n%s received, printing debug\n%s", "-" * 80, sig_name, "-" * 80) self.executor.debug_dump() self.log.info("-" * 80)
def __str__(self): if self.returncode and self.returncode < 0: try: return "Process '%s' died with %r." % ( self.cmd, signal.Signals(-self.returncode)) except ValueError: return "Process '%s' died with unknown signal %d." % ( self.cmd, -self.returncode) else: return "Process '%s' returned non-zero exit status %d." % ( self.cmd, self.returncode)
def signal_handler(sig_num, frame): """ This is a handler for SIGTERM and SIGINT. Basically it just sets a global flag, and main() will exit it's loop if the signal is trapped. """ # log the associated signal name (the python3 way) logger.warning('Received ' + signal.Signals(sig_num).name) global exit_flag exit_flag = True
def signal_shutdown(self, sig, frame): """Flag shutdown when signal received.""" _LOGGER.debug('%s received; shutting down...', signal.Signals(sig).name) # pylint: disable=no-member self._shutdown = True if self._get_task: self._get_task.cancel() # Issue #8 - Cancel not processed until next message added to queue. # Just put a dummy object on the queue to ensure it is handled immediately. self._pending_messages.sync_q.put_nowait(None)
def __str__(self): if self.returncode and self.returncode < 0: try: return "Command '%s' died with %r.\n %s" % ( self.cmd, signal.Signals(-self.returncode), self.stderr) except ValueError: return "Command '%s' died with unknown signal %d.\n %s" % ( self.cmd, -self.returncode, self.stderr) else: return "Command '%s' returned non-zero exit status %d.\n %s" % ( self.cmd, self.returncode, self.stderr)
def signal_name(signum): try: if sys.version_info[:2] >= (3, 5): return signal.Signals(signum).name else: return _signames[signum] except KeyError: return 'SIG_UNKNOWN' except ValueError: return 'SIG_UNKNOWN'
def update_contents(self): ClientPropertiesDialog.update_contents(self) self.rhack.lineEditExecutable.setText(self.client.executable_path) self.rhack.lineEditArguments.setText(self.client.arguments) self.rhack.lineEditEnviron.setText(self.client.pre_env) self.rhack.lineEditConfigFile.setText(self.client.ray_hack.config_file) save_sig = self.client.ray_hack.save_sig for i in range(self.rhack.comboSaveSig.count()): if self.rhack.comboSaveSig.itemData(i) == save_sig: self.rhack.comboSaveSig.setCurrentIndex(i) break else: try: signal_text = str(signal.Signals(save_sig)).rpartition('.')[2] self.rhack.comboSaveSig.addItem(signal_text, save_sig) self.rhack.comboSaveSig.setCurrentIndex(i + 1) except: self.rhack.comboSaveSig.setCurrentIndex(0) stop_sig = self.client.ray_hack.stop_sig for i in range(self.rhack.comboStopSig.count()): if self.rhack.comboStopSig.itemData(i) == stop_sig: self.rhack.comboStopSig.setCurrentIndex(i) break else: try: signal_text = str(signal.Signals(stop_sig)).rpartition('.')[2] self.rhack.comboStopSig.addItem(signal_text, stop_sig) self.rhack.comboStopSig.setCurrentIndex(i + 1) except: self.rhack.comboStopSig.setCurrentIndex(0) self.rhack.checkBoxWaitWindow.setChecked( bool(self.client.ray_hack.wait_win)) self.rhack.checkBoxTellUser.setChecked( bool(self.client.ray_hack.no_save_level >= 1)) self.rhack.checkBoxCloseGracefully.setChecked( bool(self.client.ray_hack.no_save_level == 2))
def _signal_received(self, sig_num: int, frame: typing.Any) -> None: try: sig_name = signal.Signals(sig_num).name except (ValueError, AttributeError): sig_name = str(sig_num) try: frame_name = frame.f_code.co_name except AttributeError: frame_name = "(unknown)" self.show("Received signal {} while in function {}".format( sig_name, frame_name)) self.exit(0)
def _terminate_process_handler(signum: int, frame: FrameType) -> None: """Termination handler that raises exceptions on the main process. When the process receives death signal(SIGTERM, SIGINT), this termination handler will be invoked. It raises the ``SignalException`` exception that should be processed by the user code. Python does not terminate process after the termination handler is finished, so the exception should not be silently ignored, otherwise the process will never be terminated. """ sigval = signal.Signals(signum) raise SignalException(f"Process {os.getpid()} got signal: {sigval}", sigval=sigval)
def _on_sigterm(signum): signame = signal.Signals(signum).name self.__logger.error( 'user interrupted with ctrl-\\ ({}), terminating...'. format(signame)) # TODO(wjwwood): try to terminate running subprocesses before exiting. self.__logger.error( 'using {} can result in orphaned processes'.format( signame)) self.__logger.error( 'make sure no processes launched are still running') this_loop.call_soon(this_task.cancel)
def register_signal( self, block: blocks.Block, signums: Iterable[Union[int, signal.Signals]], ) -> None: """Registers a list of Unix signals for a Block. This will register a :meth:`~i3pyblocks.blocks.base.Block.signal_handler` method as a callback for when signums[] is called. Note that since signals are associated with the main thread of i3pyblocks, each signal can only be assigned to a specific Block. The received signal will be passed to :meth:`~i3pyblocks.blocks.base.Block.signal_handler` as a parameter, so when receiving multiple signals it is possible to identify each of them separately. This method capture the errors inside :meth:`~i3pyblocks.blocks.base.Block.signal_handler`, but it also logs it so you can inspect the issue later on. :param block: instance of :class:`~i3pyblocks.blocks.base.Block` that will receive the signal. :param signmums: Any iterable containing signal numbers. Each signal can be an int or a `Signal enum`_. .. _`Signal enum`: https://docs.python.org/3/library/signal.html#module-contents """ async def signal_handler(sig: signal.Signals): try: logger.debug( f"Block {block.block_name} with id {block.id} received " f"a signal {sig.name}") await block.signal_handler(sig=sig) except Exception as e: block.exception( e, format= "Exception in {block_name} signal handler: {exception}", reraise=False, ) def callback_fn(sig: signal.Signals): return asyncio.ensure_future(signal_handler(sig)) for signum in signums: sig = signal.Signals( signum) # Make sure this is a Signals instance self.loop.add_signal_handler(sig, callback_fn, sig) logger.debug( f"Registered signal {sig.name} for {block.block_name}")
def _returncode_to_signal_name(returncode: int) -> Optional[str]: """ Try to convert return codes into their corresponding signal name. E.g. returncode_to_signal_name(-2) -> "SIGINT" """ try: # A negative value -N indicates that the child was terminated by signal N (POSIX only). s = signal.Signals(-returncode) # pylint: disable=no-member return s.name except Exception: # Should generally be a ValueError, but catch everything just in case. return None
async def wait(self): ''' Wait for a single signal from the signal set to arrive. ''' if not self.watching: async with self: return await self.wait() while True: if self.pending: return signal.Signals(self.pending.popleft()) await _sigwait(self)