Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with: status = StatusCode.UNAVAILABLE

I have written a simple GRPC service, client code in python. Sometimes out of nowhere, the client fails with the following error:

Traceback (most recent call last):
  File "grpc_client.py", line 35, in <module>
    response = stub.YOLO_frame(image_req)
  File "/home/vmuser/anaconda3/envs/lp_reg_brta/lib/python3.7/site-packages/grpc/_channel.py", line 923, in __call__
    return _end_unary_response_blocking(state, call, False, None)
  File "/home/vmuser/anaconda3/envs/lp_reg_brta/lib/python3.7/site-packages/grpc/_channel.py", line 826, in _end_unary_response_blocking
    raise _InactiveRpcError(state)
grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
        status = StatusCode.UNAVAILABLE
        details = "failed to connect to all addresses"
        debug_error_string = "{"created":"@1613478605.328638006","description":"Failed to pick subchannel","file":"src/core/ext/filters/client_channel/client_channel.cc","file_line":5390,"referenced_errors":[{"created":"@1613478605.328628806","description":"failed to connect to all addresses","file":"src/core/ext/filters/client_channel/lb_policy/pick_first/pick_first.cc","file_line":397,"grpc_status":14}]}"
>

My server:

import grpc
from concurrent import futures
import time

import darknet

# import the generated classes
import lp_reg_pb2
import lp_reg_pb2_grpc



# based on .proto service
class LP_REGServicer(lp_reg_pb2_grpc.LP_REGServicer):
    def YOLO_frame(self, request, context):
        response = lp_reg_pb2.PredictionBBox()
        # print(type(request.b64_frame))


        response.class_labels.extend([1])
        response.xc.extend([1])
        response.yc.extend([1])
        response.w.extend([1])
        response.h.extend([1])
        
        # darknet.dummy_predict_grpc(request.b64_frame)
        return response

    def YOLO_lp(self, request, context):
        response = lp_reg_pb2.LPBBox()
        response.class_labels.extend([1])
        response.xc.extend([1])
        response.yc.extend([1])
        response.w.extend([1])
        response.h.extend([1])
        return response

# create a gRPC server
server = grpc.server(futures.ThreadPoolExecutor(max_workers=12))


# add the defined class to the server
lp_reg_pb2_grpc.add_LP_REGServicer_to_server(
        LP_REGServicer(), server)

# listen on port 5005
print('Starting server. Listening on port 5005.')
server.add_insecure_port('[::]:5005')
server.start()

try:
    while True:
        time.sleep(5)
except KeyboardInterrupt:
    server.stop(0)

My client code:

import grpc

# import the generated classes
import lp_reg_pb2
import lp_reg_pb2_grpc

# data encoding

import numpy as np 
import base64
import zlib
import time
import cv2

# open a gRPC channel
channel = grpc.insecure_channel('127.0.0.1:5005')

# create a stub (client)
stub = lp_reg_pb2_grpc.LP_REGStub(channel)

# encoding image/numpy array

t1 = time.time()
for _ in range(10):
    frame = cv2.resize( cv2.imread('prediction.jpg'), (416, 416) ) # dummy rgb image

    img_encoded = cv2.imencode('.jpg', frame)[1]

    img_bytes = img_encoded.tobytes()  # bytes class

    # create a valid request message
    image_req = lp_reg_pb2.B64Frame(b64_frame = img_bytes)

    # make the call
    response = stub.YOLO_frame(image_req)
    print(response)
t2 = time.time()

print(f"time: {(t2-t1)/10.}")

I tried adding channel.close() in the client code but it still throws this error without any proper response.

  • The client works after I change the port, but after a while, I get this error again.
like image 662
Zabir Al Nazi Avatar asked Feb 16 '21 12:02

Zabir Al Nazi


1 Answers

I found a solution from https://github.com/grpc/grpc/issues/9987

unset http_proxy; unset https_proxy; python server.py &
unset http_proxy; unset https_proxy; python client.py

I also found additional options can be passed to grpc.insecure_channel.

In client code,

channel = grpc.insecure_channel('localhost:5005', options=(('grpc.enable_http_proxy', 0),))

This solves the issue.

like image 90
Zabir Al Nazi Avatar answered Nov 11 '22 05:11

Zabir Al Nazi