Merge pull request #1278 from ashleysommer/graceful_cancel
Gracefully handle when the request_handler_task is cancelled.
This commit is contained in:
commit
6f813f940e
34
sanic/app.py
34
sanic/app.py
@ -571,6 +571,10 @@ class Sanic:
|
|||||||
|
|
||||||
:return: Nothing
|
:return: Nothing
|
||||||
"""
|
"""
|
||||||
|
# Define `response` var here to remove warnings about
|
||||||
|
# allocation before assignment below.
|
||||||
|
response = None
|
||||||
|
cancelled = False
|
||||||
try:
|
try:
|
||||||
# -------------------------------------------- #
|
# -------------------------------------------- #
|
||||||
# Request Middleware
|
# Request Middleware
|
||||||
@ -597,6 +601,13 @@ class Sanic:
|
|||||||
response = handler(request, *args, **kwargs)
|
response = handler(request, *args, **kwargs)
|
||||||
if isawaitable(response):
|
if isawaitable(response):
|
||||||
response = await response
|
response = await response
|
||||||
|
except CancelledError:
|
||||||
|
# If response handler times out, the server handles the error
|
||||||
|
# and cancels the handle_request job.
|
||||||
|
# In this case, the transport is already closed and we cannot
|
||||||
|
# issue a response.
|
||||||
|
response = None
|
||||||
|
cancelled = True
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
# -------------------------------------------- #
|
# -------------------------------------------- #
|
||||||
# Response Generation Failed
|
# Response Generation Failed
|
||||||
@ -622,13 +633,22 @@ class Sanic:
|
|||||||
# -------------------------------------------- #
|
# -------------------------------------------- #
|
||||||
# Response Middleware
|
# Response Middleware
|
||||||
# -------------------------------------------- #
|
# -------------------------------------------- #
|
||||||
try:
|
# Don't run response middleware if response is None
|
||||||
response = await self._run_response_middleware(request,
|
if response is not None:
|
||||||
response)
|
try:
|
||||||
except BaseException:
|
response = await self._run_response_middleware(request,
|
||||||
error_logger.exception(
|
response)
|
||||||
'Exception occurred in one of response middleware handlers'
|
except CancelledError:
|
||||||
)
|
# Response middleware can timeout too, as above.
|
||||||
|
response = None
|
||||||
|
cancelled = True
|
||||||
|
except BaseException:
|
||||||
|
error_logger.exception(
|
||||||
|
'Exception occurred in one of response '
|
||||||
|
'middleware handlers'
|
||||||
|
)
|
||||||
|
if cancelled:
|
||||||
|
raise CancelledError()
|
||||||
|
|
||||||
# pass the response to the correct callback
|
# pass the response to the correct callback
|
||||||
if isinstance(response, StreamingHTTPResponse):
|
if isinstance(response, StreamingHTTPResponse):
|
||||||
|
@ -7,6 +7,7 @@ from sanic.config import Config
|
|||||||
Config.RESPONSE_TIMEOUT = 1
|
Config.RESPONSE_TIMEOUT = 1
|
||||||
response_timeout_app = Sanic('test_response_timeout')
|
response_timeout_app = Sanic('test_response_timeout')
|
||||||
response_timeout_default_app = Sanic('test_response_timeout_default')
|
response_timeout_default_app = Sanic('test_response_timeout_default')
|
||||||
|
response_handler_cancelled_app = Sanic('test_response_handler_cancelled')
|
||||||
|
|
||||||
|
|
||||||
@response_timeout_app.route('/1')
|
@response_timeout_app.route('/1')
|
||||||
@ -36,3 +37,29 @@ def test_default_server_error_response_timeout():
|
|||||||
request, response = response_timeout_default_app.test_client.get('/1')
|
request, response = response_timeout_default_app.test_client.get('/1')
|
||||||
assert response.status == 503
|
assert response.status == 503
|
||||||
assert response.text == 'Error: Response Timeout'
|
assert response.text == 'Error: Response Timeout'
|
||||||
|
|
||||||
|
|
||||||
|
response_handler_cancelled_app.flag = False
|
||||||
|
|
||||||
|
|
||||||
|
@response_handler_cancelled_app.exception(asyncio.CancelledError)
|
||||||
|
def handler_cancelled(request, exception):
|
||||||
|
# If we get a CancelledError, it means sanic has already sent a response,
|
||||||
|
# we should not ever have to handle a CancelledError.
|
||||||
|
response_handler_cancelled_app.flag = True
|
||||||
|
return text("App received CancelledError!", 500)
|
||||||
|
# The client will never receive this response, because the socket
|
||||||
|
# is already closed when we get a CancelledError.
|
||||||
|
|
||||||
|
|
||||||
|
@response_handler_cancelled_app.route('/1')
|
||||||
|
async def handler_3(request):
|
||||||
|
await asyncio.sleep(2)
|
||||||
|
return text('OK')
|
||||||
|
|
||||||
|
|
||||||
|
def test_response_handler_cancelled():
|
||||||
|
request, response = response_handler_cancelled_app.test_client.get('/1')
|
||||||
|
assert response.status == 503
|
||||||
|
assert response.text == 'Error: Response Timeout'
|
||||||
|
assert response_handler_cancelled_app.flag is False
|
||||||
|
Loading…
x
Reference in New Issue
Block a user