Hi Lidi,

Thanks for getting back. My application is for a long-lived bidirectional 
stream.
On the server, I need to detect a client disconnected. I understand 
multiple streams are multiplexed on same connection. However, in my case it 
is a long-lived bidirectional stream and the stream will terminate only 
when the client has died. 
When stream is terminated, and if the client/stream does not reconnect to 
server within a timeout, the server will need to purge out state associated 
with the client.

I'm counting on "add_done_callback" being able to register a callback that 
gets invoked before the stream is terminated. This allows me to start the 
timer on server side to wait before purging state associated with the 
client/stream.
I was also suggested to look into enabling TCP keepalives - but I have not 
seen a way for the application to be notified if/when TCP connection is 
terminated.

If you have better ideas, I much appreciate it.
BTW, I quickly sifted through the PR, there is a suggestion to catch 
asyncio.CancelledError.. However, 1) I am not sure if 
asyncio.CancelledError is raised if client disconnects, and 2) whether  
application/server will have a chance to do anything about it before 
underlying aio thread get cleaned up?

async def handler(request, service_context):
    try:
        make_something(...)
    except asyncio.CancelledError:
        clean_stuff_related_to_current_RPC(....)

Regards,
Tarek

On Monday, April 5, 2021 at 2:24:19 PM UTC-4 Lidi Zheng wrote:

> Hi,
>
> `add_done_callback` wasn't added to grpc.aio.Server because there was a 
> discussion that it overlaps with server interceptors: 
> https://github.com/grpc/grpc/pull/22503.
>
> I can update the PR later this week. If possible, please explain more 
> about your use case and why you think `add_done_callback` would be handy.
>
> On Saturday, April 3, 2021 at 2:56:56 PM UTC-7 [email protected] wrote:
>
>> Hi,
>>
>> Using python grpc.aio, I'm not able to add a callback  on server side 
>> (using context) to be invoked on a grpc stream terminated/cancelled -- I 
>> use grpc.aio.server().
>> The documentation here 
>> <https://grpc.github.io/grpc/python/grpc_asyncio.html#shared-context> 
>> mentions 
>> the callback, but I can't seem to be able to get it to work. 
>>
>> If I use grpc.server() instead, I can set add_callback()
>>
>> I'm using:
>> Name: grpcio
>> Version: 1.36.1
>> Name: grpcio-tools
>> Version: 1.35.0
>> Name: aiogrpc
>> Version: 1.8
>>
>> Thanks
>>
>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/grpc-io/5fbc0de4-ada1-46d0-813f-9ac6ce7e83e3n%40googlegroups.com.

Reply via email to