[Aio] Ensure Core channel closes when deallocated (#29797)

* [Aio] Ensure Core channel closes when deallocated

* Keep channel object alive

* Let Multicallable objects hold the reference to Channel

* Make YAPF happy
pull/29840/head
Lidi Zheng 3 years ago committed by GitHub
parent d316cf7ae9
commit 8558f46d35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 16
      src/python/grpcio/grpc/aio/_channel.py

@ -80,6 +80,7 @@ class _BaseMultiCallable:
_request_serializer: SerializingFunction
_response_deserializer: DeserializingFunction
_interceptors: Optional[Sequence[ClientInterceptor]]
_references: List[Any]
_loop: asyncio.AbstractEventLoop
# pylint: disable=too-many-arguments
@ -90,6 +91,7 @@ class _BaseMultiCallable:
request_serializer: SerializingFunction,
response_deserializer: DeserializingFunction,
interceptors: Optional[Sequence[ClientInterceptor]],
references: List[Any],
loop: asyncio.AbstractEventLoop,
) -> None:
self._loop = loop
@ -98,6 +100,7 @@ class _BaseMultiCallable:
self._request_serializer = request_serializer
self._response_deserializer = response_deserializer
self._interceptors = interceptors
self._references = references
@staticmethod
def _init_metadata(
@ -370,6 +373,11 @@ class Channel(_base_channel.Channel):
async def close(self, grace: Optional[float] = None):
await self._close(grace)
def __del__(self):
if hasattr(self, '_channel'):
if not self._channel.closed():
self._channel.close()
def get_state(self,
try_to_connect: bool = False) -> grpc.ChannelConnectivity:
result = self._channel.check_connectivity_state(try_to_connect)
@ -397,7 +405,7 @@ class Channel(_base_channel.Channel):
return UnaryUnaryMultiCallable(self._channel, _common.encode(method),
request_serializer,
response_deserializer,
self._unary_unary_interceptors,
self._unary_unary_interceptors, [self],
self._loop)
def unary_stream(
@ -409,7 +417,7 @@ class Channel(_base_channel.Channel):
return UnaryStreamMultiCallable(self._channel, _common.encode(method),
request_serializer,
response_deserializer,
self._unary_stream_interceptors,
self._unary_stream_interceptors, [self],
self._loop)
def stream_unary(
@ -421,7 +429,7 @@ class Channel(_base_channel.Channel):
return StreamUnaryMultiCallable(self._channel, _common.encode(method),
request_serializer,
response_deserializer,
self._stream_unary_interceptors,
self._stream_unary_interceptors, [self],
self._loop)
def stream_stream(
@ -434,7 +442,7 @@ class Channel(_base_channel.Channel):
request_serializer,
response_deserializer,
self._stream_stream_interceptors,
self._loop)
[self], self._loop)
def insecure_channel(

Loading…
Cancel
Save