Skip to content

Commit

Permalink
Merge branch 'master' into issue-2080
Browse files Browse the repository at this point in the history
  • Loading branch information
ahopkins authored Mar 22, 2021
2 parents 3fa76c0 + 4998fd5 commit 4cdf229
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
5 changes: 5 additions & 0 deletions sanic/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,16 @@ def check_timeouts(self):
if stage is Stage.IDLE and duration > self.keep_alive_timeout:
logger.debug("KeepAlive Timeout. Closing connection.")
elif stage is Stage.REQUEST and duration > self.request_timeout:
logger.debug("Request Timeout. Closing connection.")
self._http.exception = RequestTimeout("Request Timeout")
elif stage is Stage.HANDLER and self._http.upgrade_websocket:
logger.debug("Handling websocket. Timeouts disabled.")
return
elif (
stage in (Stage.HANDLER, Stage.RESPONSE, Stage.FAILED)
and duration > self.response_timeout
):
logger.debug("Response Timeout. Closing connection.")
self._http.exception = ServiceUnavailable("Response Timeout")
else:
interval = (
Expand Down
61 changes: 44 additions & 17 deletions tests/test_response_timeout.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import asyncio
import logging

from time import sleep

from sanic import Sanic
from sanic.exceptions import ServiceUnavailable
from sanic.log import LOGGING_CONFIG_DEFAULTS
from sanic.response import text


Expand All @@ -13,6 +17,8 @@
response_timeout_default_app.config.RESPONSE_TIMEOUT = 1
response_handler_cancelled_app.config.RESPONSE_TIMEOUT = 1

response_handler_cancelled_app.ctx.flag = False


@response_timeout_app.route("/1")
async def handler_1(request):
Expand All @@ -25,32 +31,17 @@ def handler_exception(request, exception):
return text("Response Timeout from error_handler.", 503)


def test_server_error_response_timeout():
request, response = response_timeout_app.test_client.get("/1")
assert response.status == 503
assert response.text == "Response Timeout from error_handler."


@response_timeout_default_app.route("/1")
async def handler_2(request):
await asyncio.sleep(2)
return text("OK")


def test_default_server_error_response_timeout():
request, response = response_timeout_default_app.test_client.get("/1")
assert response.status == 503
assert "Response Timeout" in response.text


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
response_handler_cancelled_app.ctx.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.
Expand All @@ -62,8 +53,44 @@ async def handler_3(request):
return text("OK")


def test_server_error_response_timeout():
request, response = response_timeout_app.test_client.get("/1")
assert response.status == 503
assert response.text == "Response Timeout from error_handler."


def test_default_server_error_response_timeout():
request, response = response_timeout_default_app.test_client.get("/1")
assert response.status == 503
assert "Response Timeout" in response.text


def test_response_handler_cancelled():
request, response = response_handler_cancelled_app.test_client.get("/1")
assert response.status == 503
assert "Response Timeout" in response.text
assert response_handler_cancelled_app.flag is False
assert response_handler_cancelled_app.ctx.flag is False


def test_response_timeout_not_applied(caplog):
modified_config = LOGGING_CONFIG_DEFAULTS
modified_config["loggers"]["sanic.root"]["level"] = "DEBUG"

app = Sanic("test_logging", log_config=modified_config)
app.config.RESPONSE_TIMEOUT = 1
app.ctx.event = asyncio.Event()

@app.websocket("/ws")
async def ws_handler(request, ws):
sleep(2)
await asyncio.sleep(0)
request.app.ctx.event.set()

with caplog.at_level(logging.DEBUG):
_ = app.test_client.websocket("/ws")
assert app.ctx.event.is_set()
assert (
"sanic.root",
10,
"Handling websocket. Timeouts disabled.",
) in caplog.record_tuples

0 comments on commit 4cdf229

Please sign in to comment.