Resolve a TODO and handle one more cancellation corner case

pull/21506/head
Lidi Zheng 5 years ago
parent 413d29218e
commit 6f0ffef2e9
  1. 21
      src/python/grpcio/grpc/experimental/aio/_call.py
  2. 4
      src/python/grpcio_tests/tests_aio/unit/call_test.py

@ -308,16 +308,17 @@ class UnaryUnaryCall(Call, _base_call.UnaryUnaryCall):
_LOCAL_CANCELLATION_DETAILS, None, None)) _LOCAL_CANCELLATION_DETAILS, None, None))
def __await__(self) -> ResponseType: def __await__(self) -> ResponseType:
"""Wait till the ongoing RPC request finishes. """Wait till the ongoing RPC request finishes."""
try:
Returns: response = yield from self._call
Response of the RPC call. except asyncio.CancelledError:
# Even if we converted all other CancelledError, there is still
Raises: # this corner case. If the application cancels immediately after
RpcError: Indicating that the RPC terminated with non-OK status. # the Call object is created, we will observe this
asyncio.CancelledError: Indicating that the RPC was canceled. # `CancelledError`.
""" if not self.cancelled():
response = yield from self._call self.cancel()
raise _create_rpc_error(_EMPTY_METADATA, self._status.result())
return response return response

@ -121,10 +121,6 @@ class TestUnaryUnaryCall(AioTestBase):
self.assertFalse(call.cancelled()) self.assertFalse(call.cancelled())
# TODO(https://github.com/grpc/grpc/issues/20869) remove sleep.
# Force the loop to execute the RPC task.
await asyncio.sleep(0)
self.assertTrue(call.cancel()) self.assertTrue(call.cancel())
self.assertFalse(call.cancel()) self.assertFalse(call.cancel())

Loading…
Cancel
Save