Example #1
0
 def cleanup_on_exit(self):
     error = None
     try:
         yield
     except Exception:
         log.info(
             "ConnectionManager shutting down with error. Traceback:",
             exc_info=True,
         )
         error = GrpcError.from_exception(sys.exc_info(),
                                          code=StatusCode.UNAVAILABLE)
     finally:
         for send_stream in self.send_streams.values():
             if send_stream.closed:
                 continue  # stream.close() is idemponent but this prevents the log
             log.info(f"Terminating send stream {send_stream}"
                      f"{f' with error {error}' if error else ''}.")
             send_stream.close(error)
         for receive_stream in self.receive_streams.values():
             if receive_stream.closed:
                 continue  # stream.close() is idemponent but this prevents the log
             log.info(f"Terminating receive stream {receive_stream}"
                      f"{f' with error {error}' if error else ''}.")
             receive_stream.close(error)
         self.sock.close()
         self.stopped.set()
Example #2
0
    def handle_result(self, response_stream, worker_ctx, result, exc_info):

        if self.cardinality in (Cardinality.STREAM_UNARY,
                                Cardinality.UNARY_UNARY):
            result = (result, )

        if exc_info is None:
            try:
                response_stream.populate(result)
            except Exception as exception:
                message = "Exception iterating responses: {}".format(exception)
                error = GrpcError.from_exception(sys.exc_info(),
                                                 message=message)

                response_stream.close(error)
        else:
            message = "Exception calling application: {}".format(exc_info[1])
            error = GrpcError.from_exception(exc_info, message=message)

            response_stream.close(error)

        return result, exc_info