gRPC异步服务pytest测试报错StatusCode.UNKNOWN问题求助
gRPC异步服务pytest测试报错排查求助
问题背景
我实现了一个小型gRPC异步服务,使用pytest编写异步测试用例进行验证,测试代码如下:
@pytest.mark.asyncio async def test_poc_server(): # 启动poc服务 server = PoCGrpcServer(50051) await server.start() # 创建连接到服务的客户端 async with grpc.aio.insecure_channel('localhost:50051') as channel: stub = PoCStub(channel) response = await stub.Move(poc__pb2.MoveRequest(x=10.0, y=10.0)) assert response.result() == poc__pb2.MoveResponse
报错信息
测试在执行stub.Move()时失败,报错如下:
> response = await stub.Move(poc__pb2.MoveRequest(x=10.0, y=10.0)) tests/test_grpc_server.py:51: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ self = <_AioCall of RPC that terminated with: status = Exception calling application: details = "StatusCode.UNKNOWN" debu...peer {created_time:"2023-06-15T14:38:59.0601+02:00", grpc_status:2, grpc_message:"Exception calling application: "}" > def __await__(self) -> ResponseType: """Wait till the ongoing RPC request finishes.""" try: response = yield from self._call_response except asyncio.CancelledError: # Even if we caught all other CancelledError, there is still # this corner case. If the application cancels immediately after # the Call object is created, we will observe this # `CancelledError`. if not self.cancelled(): self.cancel() raise # NOTE(lidiz) If we raise RpcError in the task, and users doesn't # 'await' on it. AsyncIO will log 'Task exception was never retrieved'. # Instead, if we move the exception raising here, the spam stops. # Unfortunately, there can only be one 'yield from' in '__await__'. So, # we need to access the private instance variable. if response is cygrpc.EOF: if self._cython_call.is_locally_cancelled(): raise asyncio.CancelledError() else: > raise _create_rpc_error(self._cython_call._initial_metadata, self._cython_call._status) E grpc.aio._call.AioRpcError: <AioRpcError of RPC that terminated with: E status = StatusCode.UNKNOWN E details = "Exception calling application: " E debug_error_string = "UNKNOWN:Error received from peer {created_time:"2023-06-15T14:38:59.0601+02:00", grpc_status:2, grpc_message:"Exception calling application: "}" E > ../../../../Library/Caches/pypoetry/virtualenvs/pytaf-6R8xcXyw-py3.11/lib/python3.11/site-packages/grpc/aio/_call.py:290: AioRpcError
现有猜测与求助
- 猜测问题可能和服务与客户端运行在同一个asyncio运行器有关,但不确定。
- 注意到pytest-grpc库也存在类似错误,但不清楚具体修复方案。
- 寻求该问题的排查思路与解决方法。
内容的提问来源于stack exchange,提问作者sebastianpfischer
相关产品推荐
相关产品推荐

