vmagamedov / grpclib

Pure-Python gRPC implementation for asyncio
http://grpclib.readthedocs.io
BSD 3-Clause "New" or "Revised" License
936 stars 92 forks source link

Does not handle cancellations gracefully originating from server side #131

Open AudriusButkevicius opened 3 years ago

AudriusButkevicius commented 3 years ago

The docs as per https://grpclib.readthedocs.io/en/latest/errors.html#server-side state:

The docs state:

  1. Task running SayHello coroutine gets cancelled and CancelledError is raised inside it
  2. When SayHello coroutine finishes, grpclib server internally re-raises CancelledError as TimeoutError or StreamTerminatedError to explain why request was cancelled

However, I still end up with the following in my application logs:

Application error
Traceback (most recent call last):
  File "/home/audriusb/code/python_prod/.venv/lib/python3.7/site-packages/grpclib/server.py", line 440, in request_handler
    await method_func(stream)
  File "/home/audriusb/code/python_prod/src/framework/base.py", line 122, in GetValues
    raise e
  File "/home/audriusb/code/python_prod/src/framework/base.py", line 114, in GetValues
    raise e
  File "/home/audriusb/code/python_prod/src/framework/base.py", line 97, in GetValues
    async for m in impl:
  File "/home/audriusb/code/python_prod/src/framework/base.py", line 172, in calculate
    async for a in aiter:
  File "/home/audriusb/code/python_prod/src/handler.py", line 21, in impl
    await asyncio.sleep(90000)
  File "/home/audriusb/.pyenv/versions/3.7.9/lib/python3.7/asyncio/tasks.py", line 595, in sleep
    return await future
concurrent.futures._base.CancelledError

I don't see any handling of CancelledError around the code that invokes the handler: https://github.com/vmagamedov/grpclib/blob/master/grpclib/server.py#L440

I see there is cancel handling in the wrapper object, but I think that is mostly for client cancelling the stream, not server calls being cancelled because it's shutting down?

alitoufighi commented 2 years ago

Same problem here. These canceled errors (when caused by timeouts) don't even result in grpc's DEADLINE_EXCEEDED status code.