Skip to content

Commit 8558f46

Browse files
authored
[Aio] Ensure Core channel closes when deallocated (grpc#29797)
* [Aio] Ensure Core channel closes when deallocated * Keep channel object alive * Let Multicallable objects hold the reference to Channel * Make YAPF happy
1 parent d316cf7 commit 8558f46

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/python/grpcio/grpc/aio/_channel.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ class _BaseMultiCallable:
8080
_request_serializer: SerializingFunction
8181
_response_deserializer: DeserializingFunction
8282
_interceptors: Optional[Sequence[ClientInterceptor]]
83+
_references: List[Any]
8384
_loop: asyncio.AbstractEventLoop
8485

8586
# pylint: disable=too-many-arguments
@@ -90,6 +91,7 @@ def __init__(
9091
request_serializer: SerializingFunction,
9192
response_deserializer: DeserializingFunction,
9293
interceptors: Optional[Sequence[ClientInterceptor]],
94+
references: List[Any],
9395
loop: asyncio.AbstractEventLoop,
9496
) -> None:
9597
self._loop = loop
@@ -98,6 +100,7 @@ def __init__(
98100
self._request_serializer = request_serializer
99101
self._response_deserializer = response_deserializer
100102
self._interceptors = interceptors
103+
self._references = references
101104

102105
@staticmethod
103106
def _init_metadata(
@@ -370,6 +373,11 @@ async def _close(self, grace): # pylint: disable=too-many-branches
370373
async def close(self, grace: Optional[float] = None):
371374
await self._close(grace)
372375

376+
def __del__(self):
377+
if hasattr(self, '_channel'):
378+
if not self._channel.closed():
379+
self._channel.close()
380+
373381
def get_state(self,
374382
try_to_connect: bool = False) -> grpc.ChannelConnectivity:
375383
result = self._channel.check_connectivity_state(try_to_connect)
@@ -397,7 +405,7 @@ def unary_unary(
397405
return UnaryUnaryMultiCallable(self._channel, _common.encode(method),
398406
request_serializer,
399407
response_deserializer,
400-
self._unary_unary_interceptors,
408+
self._unary_unary_interceptors, [self],
401409
self._loop)
402410

403411
def unary_stream(
@@ -409,7 +417,7 @@ def unary_stream(
409417
return UnaryStreamMultiCallable(self._channel, _common.encode(method),
410418
request_serializer,
411419
response_deserializer,
412-
self._unary_stream_interceptors,
420+
self._unary_stream_interceptors, [self],
413421
self._loop)
414422

415423
def stream_unary(
@@ -421,7 +429,7 @@ def stream_unary(
421429
return StreamUnaryMultiCallable(self._channel, _common.encode(method),
422430
request_serializer,
423431
response_deserializer,
424-
self._stream_unary_interceptors,
432+
self._stream_unary_interceptors, [self],
425433
self._loop)
426434

427435
def stream_stream(
@@ -434,7 +442,7 @@ def stream_stream(
434442
request_serializer,
435443
response_deserializer,
436444
self._stream_stream_interceptors,
437-
self._loop)
445+
[self], self._loop)
438446

439447

440448
def insecure_channel(

0 commit comments

Comments
 (0)