Пример #1
0
 def _post_run(self, future: asyncio.Future):
     try:
         future.exception()
     except asyncio.CancelledError:
         pass
     except Exception as E:
         raise E
Пример #2
0
 def check_for_exceptions(done_future: asyncio.Future):
     if not done_future.cancelled() and done_future.exception():
         logger_for_error.error('Future {}done with exception!'.format(
             '' if not future_description else '({}) '.
             format(future_description)),
                                exc_info=make_exc_info(
                                    done_future.exception()))
Пример #3
0
    def run_until_complete(self, future: asyncio.Future) -> Any:
        self._check_running()

        new_task = False

        if not asyncio.isfuture(future):
            future = asyncio.ensure_future(future, loop=self)

            # We wrapped `future` in a new Task since it was not a Future.
            new_task = True

            # An exception is raised if the new task doesn't complete, so there is no need to log the "destroy
            # pending task" message.
            future._log_destroy_pending = False
        else:
            if _helpers.get_future_loop(future) is not self:
                raise ValueError("Future does not belong to this loop")

        future.add_done_callback(_run_until_complete_cb)
        try:
            self._run_mainloop()
        except:
            if new_task and future.done() and not future.cancelled():
                # The coroutine raised a BaseException. Consume the exception to not log a warning (Future
                # will log a warning if its exception is not retrieved), the caller doesn't have access to the
                # task wrapper we made.
                future.exception()
            raise
        finally:
            future.remove_done_callback(_run_until_complete_cb)

        if not future.done():
            raise RuntimeError('Event loop stopped before Future completed.')

        return future.result()
Пример #4
0
 def _done_callback(future: asyncio.Future) -> None:
     if future.exception() is not None:
         e: BaseException = future.exception()  # type: ignore
         self.sync_event.set({
             "type": "error",
             "exception": (type(e), e, e.__traceback__)
         })
     asgi_done.set()
Пример #5
0
 def _future_result(self, task: asyncio.Future):
     try:
         task.result()
     except SelfDestructError:
         logger.debug("Behavior self-destructed")
         task.exception()
         asyncio.ensure_future(self.stop_behavior()) \
             .add_done_callback(functools.partial(self._delete_behavior, behavior_def=self.behavior_def))
Пример #6
0
        def bg_loop_error_callback(fut: asyncio.Future):

            try:
                fut.exception()
            except asyncio.CancelledError:
                log.info("Background loop closed.")
            except Exception as exc:
                log.exception("Unhandled exception in background loop: ", exc_info=exc)
Пример #7
0
        def on_finish(fut: asyncio.Future) -> None:
            if interrupted:
                # this is just a RuntimeError because we're awaiting an already
                # awaited coroutine.
                fut.exception()
                return

            self._loop.create_task(self.__handle_finish())
Пример #8
0
        def _handle_exception(future: asyncio.Future):
            logging.debug(f"error: {type(future.exception())}, {str(future.exception())}")
            if isinstance(future.exception(), NotImplementedError):
                asyncio.ensure_future(self.__subscribe_call_by_rest_stub(subscribe_event))

            elif isinstance(future.exception(), ConnectionError):
                logging.warning(f"Waiting for next subscribe request...")
                if self.__state_machine.state != "SubscribeNetwork":
                    self.__state_machine.subscribe_network()
Пример #9
0
 def when_remote_delivery_done(fut: Future):
     asyncio.ensure_future(self.queue.remove(index))
     delivery_task.done = True
     if not fut.exception():
         delivery_task.success = True
     else:
         delivery_task.success = False
         delivery_task.status_message = str(
             fut.exception())
Пример #10
0
 def future_done(other: asyncio.Future, current: asyncio.Future):
     if current.cancelled():
         other.set_exception(RuntimeError("Canceled."))
     elif current.exception():
         other.set_exception(current.exception())
     else:
         payload = current.result()
         payload.data = b'(server ' + payload.data + b')'
         payload.metadata = b'(server ' + payload.metadata + b')'
         other.set_result(payload)
Пример #11
0
 def _log_future_error(self, future: asyncio.Future):
     # Technically the task housing the task this callback is for is what's
     # usually cancelled, therefore cancelled() doesn't actually catch this case
     try:
         if future.cancelled():
             return
         elif exc := future.exception():
             self.bot.loop.create_task(self.log_error(exc))
     except asyncio.CancelledError:
         return
Пример #12
0
def callback(future: asyncio.Future, extra: typing.Dict):
    logger = logging.getLogger(f"{__name__}.update_relux_plan.callback")
    if future.exception():
        logger.error(f"Update plan failed: \nextra({extra})",
                     exc_info=future.exception(),
                     extra=extra)
    res = future.result()
    if res.modified_count != 1:
        logger.error(
            f"Update plan failed:\n raw_result({res.raw_result})\n, extra({extra})",
            extra=extra)
Пример #13
0
 def done_callback(fut: asyncio.Future):
     try:
         fut.exception()
     except asyncio.CancelledError:
         pass
     except asyncio.InvalidStateError as exc:
         log.exception("We somehow have a done callback when not done?",
                       exc_info=exc)
     except Exception as exc:
         log.exception("Unexpected exception in terraria: ",
                       exc_info=exc)
Пример #14
0
        def done_callback(fut: asyncio.Future):

            try:
                fut.exception()
            except asyncio.CancelledError:
                log.info("Relays didn't set up and was cancelled")
            except asyncio.InvalidStateError as exc:
                log.exception("We somehow have a done callback when not done?",
                              exc_info=exc)
            except Exception as exc:
                log.exception("Unexpected exception in relays: ", exc_info=exc)
Пример #15
0
 def done_callback(fut: asyncio.Future):
     try:
         fut.exception()
     except asyncio.CancelledError:
         pass
     except asyncio.InvalidStateError as exc:
         log.exception("We're not done but we did a callback?",
                       exc_info=exc)
     except Exception as exc:
         log.exception("Unexpected exception in chanfeed: ",
                       exc_info=exc)
Пример #16
0
 def run_job_done_callback(f: asyncio.Future):
     if f.cancelled():
         if not job.done():
             job.cancel()
     elif f.exception():
         job.set_exception(f.exception())
     elif f.done():
         job.set_result(f.result())
         # cancel should be triggered by job
     else:
         raise RuntimeError(f"Unexpected future {f}")
Пример #17
0
    def _services_call_done(operation: dict, future: asyncio.Future) -> None:
        """Called when services call (i.e., an operation) is done.

        This needs to handle both successful and exception-raising calls.

        Arguments:
            operation: Operation dict
            future: Finished future

        """
        # either the call succeeded or it raised an exception.
        operation["done"] = True

        exc = future.exception()
        if exc:
            # e.g., "hmc_nuts_diag_e_adapt_wrapper() got an unexpected keyword argument, ..."
            # e.g., dimension errors in variable declarations
            # e.g., initialization failed
            message, status = (
                f"Exception during call to services function: `{repr(exc)}`, traceback: `{traceback.format_tb(exc.__traceback__)}`",
                400,
            )
            logger.critical(message)
            operation["result"] = _make_error(message, status=status)
            # Delete messages associated with the fit. If initialization
            # fails, for example, messages will exist on disk. Remove them.
            try:
                httpstan.cache.delete_fit(operation["metadata"]["fit"]["name"])
            except KeyError:
                pass
        else:
            logger.info(f"Operation `{operation['name']}` finished.")
            operation["result"] = schemas.Fit().load(
                operation["metadata"]["fit"])
Пример #18
0
def _on_result(future: asyncio.Future, new_future: asyncio.Future = None):
    if not new_future.done():
        exc = future.exception()
        if exc:
            return new_future.set_exception(exc)

        new_future.set_result(future.result())
Пример #19
0
def wait_for_future(future: asyncio.Future, timeout: float = None):
    """Waits for anon asyncio future. Helper method.

    Args:
        future (asyncio.Future): The future to wait for.
        timeout (float, optional): The wait timeout. Defaults to None.

    Returns:
        any: The future result.
    """
    if asyncio.iscoroutine(future):
        loop = get_active_loop()
        future = loop.create_task(future)
    else:
        loop = get_asyncio_future_event_loop(future)

    if timeout is not None:
        if sys.version_info.minor < 10:
            future = asyncio.wait_for(future, timeout=timeout, loop=loop)
        else:
            future = asyncio.wait_for(future, timeout=timeout)

    loop.run_until_complete(future)

    last_exception = future.exception()
    if last_exception is not None:
        raise last_exception

    return future.result()
Пример #20
0
    def feature_finished(self,
                         future: asyncio.Future,
                         reconnect_fn=None,
                         name=''):
        try:
            self.logInfo('Feature finished: "{}"'.format(name))

            if future.cancelled():
                return

            exc = future.exception()
            if exc:
                if (isinstance(exc, ConnectionClosed) and exc.code > 1002) \
                        or isinstance(exc, InvalidStatusCode, TypeError):
                    self.logError(exc)

                    if reconnect_fn and not self.stop:
                        self.logInfo('Trying to reconnect...')
                        sleep(1)
                        reconnect_fn()
                    else:
                        self.logInfo('No reconnection function...')
                    return
                else:
                    self.logError(exc)
            else:
                ws: WebSocketClientProtocol = future.result()

                if ws and ws.state == State.CLOSED:
                    self.logInfo('WS Closed: {} - {}'.format(
                        ws.close_code, ws.close_reason))
        except:
            self.logError(traceback.format_exc())
Пример #21
0
 def _done_callback(_future: Future) -> None:
     nonlocal results
     if _future.exception() is None:
         segment, status, flag = _future.result()
         if flag is None:
             segment.content = []
             # logger.error('下载过程中出现已知异常 需重新下载\n')
         elif flag is False:
             segment.content = []
             # 某几类已知异常 如状态码不对 返回头没有文件大小 视为无法下载 主动退出
             cancel_all_task()
             if status in ['STATUS_CODE_ERROR', 'NO_CONTENT_LENGTH']:
                 logger.error(
                     f'{status} {t_msg.segment_cannot_download}')
             elif status == 'EXIT':
                 pass
             else:
                 logger.error(
                     f'{status} {t_msg.segment_cannot_download_unknown_status}'
                 )
         results[segment] = flag
         if self.xprogress.is_ending():
             cancel_uncompleted_task()
     else:
         # 出现未知异常 强制退出全部task
         logger.error(
             f'{t_msg.segment_cannot_download_unknown_exc} => {_future.exception()}\n'
         )
         cancel_all_task()
         results['未知segment'] = False
Пример #22
0
 def handle_task_exc(inner_task: asyncio.Future):
     try:
         exc = inner_task.exception()
         if exc:
             self._inner_exception = exc
             self.inner_error_ev.set()
     except asyncio.CancelledError:
         pass
Пример #23
0
        def _runner_cb(fut: asyncio.Future) -> None:
            if not fut.cancelled():
                exc = fut.exception()
                if exc and not isinstance(exc,
                                          (SystemExit, KeyboardInterrupt)):
                    loop.default_exception_handler({'exception': exc})

            loop.stop()
Пример #24
0
        def _handle_exception(future: asyncio.Future):
            logging.debug(
                f"error: {type(future.exception())}, {str(future.exception())}"
            )

            if ChannelProperty().node_type != conf.NodeType.CitizenNode:
                logging.debug(f"This node is not Citizen anymore.")
                return

            if isinstance(future.exception(), AnnounceNewBlockError):
                self.__state_machine.block_sync()
                return

            if future.exception():
                logging.warning(f"Waiting for next subscribe request...")
                if self.__state_machine.state != "SubscribeNetwork":
                    self.__state_machine.subscribe_network()
    def _completed_callback(self,
        invoke_result_future: asyncio.Future
    ):
        '''
        Cloud Function invoke completes the callback method.
        '''

        if invoke_result_future.exception():
            if not self.__schedule_invoked_callback:
                raise invoke_result_future.exception()

        try:
            if self.__schedule_invoked_callback:
                self.__schedule_invoked_callback(self)
        finally:
            if self.__schedule_completed_callback:
                self.__schedule_completed_callback(self)
Пример #26
0
 def callback(fut: Future):
     if fut.cancelled():
         return
     if fut.exception() is not None:
         create_task(loop,
                     coroutine,
                     __task__=task,
                     restart_on=restart_on)
Пример #27
0
 def worker_disconn(eol: asyncio.Future):
     exc = None
     try:
         exc = eol.exception()
     except (Exception, asyncio.CancelledError) as e:
         exc = e
     err_sink.publish(
         exc or EdhPeerError(peer.ident, "Swarm worker disconnected"))
Пример #28
0
def _create_job_callback(job: Job, fut: asyncio.Future):

    exc = fut.exception()
    if not fut.cancelled() and exc:
        raise exc

    if fut.done():
        job.meta_update(result=fut.result())
        job.save()
Пример #29
0
 def _after_request(self, stream_id: int, future: asyncio.Future) -> None:
     del self.streams[stream_id]
     if not self.streams:
         self._timeout_handle = self.loop.call_later(
             self._timeout, self._handle_timeout)
     exception = future.exception()
     if exception is not None and not isinstance(exception,
                                                 asyncio.CancelledError):
         self.logger.error('Request handling exception', exc_info=exception)
Пример #30
0
                    def copy(f: asyncio.Future) -> None:
                        if original_future.done():
                            return

                        exc = f.exception()
                        if exc is not None:
                            original_future.set_exception(exc)
                        else:
                            original_future.set_result(f.result())
Пример #31
0
 def _on_task_done(task: Future) -> None:
     tasks.remove(task)
     if not task.cancelled() and task.exception():
         self._render_future.set_exception(task.exception())
Пример #32
0
 def check_for_exceptions(done_future: asyncio.Future):
     if not done_future.cancelled() and done_future.exception():
         logger_for_error.error('Future {}done with exception!'.format('' if not future_description else
                                                                       '({}) '.format(future_description)),
                                exc_info=make_exc_info(done_future.exception()))
Пример #33
0
 def _remove_done_future(self, fut: Future, *, req_id: int) -> None:
     self._futures.pop(req_id, None)
     if not fut.cancelled() and fut.exception():
         self._render_future.set_exception(fut.exception())