Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Break cyclical references #189

Merged
merged 1 commit into from
Sep 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions uvicorn/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,6 @@ def get_logger(log_level):
help="Close Keep-Alive connections if no new data is received within this timeout.",
show_default=True,
)
@click.option(
"--timeout-response",
type=int,
default=60,
help="Cancel request/response tasks that do not complete within this timeout.",
show_default=True,
)
def main(
app,
host: str,
Expand All @@ -161,7 +154,6 @@ def main(
limit_concurrency: int,
limit_max_requests: int,
timeout_keep_alive: int,
timeout_response: int,
):
sys.path.insert(0, ".")

Expand All @@ -183,7 +175,6 @@ def main(
"limit_concurrency": limit_concurrency,
"limit_max_requests": limit_max_requests,
"timeout_keep_alive": timeout_keep_alive,
"timeout_response": timeout_response,
}

if debug:
Expand Down Expand Up @@ -212,7 +203,6 @@ def run(
limit_concurrency=None,
limit_max_requests=None,
timeout_keep_alive=5,
timeout_response=60,
install_signal_handlers=True,
ready_event=None,
):
Expand Down Expand Up @@ -261,7 +251,6 @@ def create_protocol():
root_path=root_path,
limit_concurrency=limit_concurrency,
timeout_keep_alive=timeout_keep_alive,
timeout_response=timeout_response,
)

server = Server(
Expand Down
20 changes: 3 additions & 17 deletions uvicorn/protocols/http/h11_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ def __init__(
root_path="",
limit_concurrency=None,
timeout_keep_alive=5,
timeout_response=60,
):
self.app = app
self.loop = loop or asyncio.get_event_loop()
Expand All @@ -133,7 +132,6 @@ def __init__(
# Timeouts
self.timeout_keep_alive_task = None
self.timeout_keep_alive = timeout_keep_alive
self.timeout_response = timeout_response

# Per-connection state
self.transport = None
Expand Down Expand Up @@ -268,11 +266,8 @@ def handle_events(self):
on_response=self.on_response_complete,
)
task = self.loop.create_task(self.cycle.run_asgi(app))
task.add_done_callback(self.on_task_complete)
task.add_done_callback(self.tasks.discard)
self.tasks.add(task)
self.loop.call_later(
self.timeout_response, self.timeout_response_handler, task
)

elif event_type is h11.Data:
if self.conn.our_state is h11.DONE:
Expand Down Expand Up @@ -351,9 +346,6 @@ def on_response_complete(self):
self.conn.start_next_cycle()
self.handle_events()

def on_task_complete(self, task):
self.tasks.discard(task)

def shutdown(self):
"""
Called by the server to commence a graceful shutdown.
Expand Down Expand Up @@ -386,14 +378,6 @@ def timeout_keep_alive_handler(self):
self.conn.send(event)
self.transport.close()

def timeout_response_handler(self, task):
"""
Called once per task, when the reponse timeout is reached.
"""
if not task.done():
self.logger.error("Task exceeded response timeout.")
task.cancel()


class RequestResponseCycle:
def __init__(
Expand Down Expand Up @@ -457,6 +441,8 @@ async def run_asgi(self, app):
self.logger.error(msg)
if not self.disconnected:
self.transport.close()
finally:
self.on_response = None

async def send_500_response(self):
await self.send(
Expand Down
25 changes: 4 additions & 21 deletions uvicorn/protocols/http/httptools_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ def __init__(
root_path="",
limit_concurrency=None,
timeout_keep_alive=5,
timeout_response=60,
):
self.app = app
self.loop = loop or asyncio.get_event_loop()
Expand All @@ -134,7 +133,6 @@ def __init__(
# Timeouts
self.timeout_keep_alive_task = None
self.timeout_keep_alive = timeout_keep_alive
self.timeout_response = timeout_response

# Per-connection state
self.transport = None
Expand Down Expand Up @@ -297,11 +295,8 @@ def on_headers_complete(self):
if existing_cycle is None or existing_cycle.response_complete:
# Standard case - start processing the request.
task = self.loop.create_task(self.cycle.run_asgi(app))
task.add_done_callback(self.on_task_complete)
task.add_done_callback(self.tasks.discard)
self.tasks.add(task)
self.loop.call_later(
self.timeout_response, self.timeout_response_handler, task
)
else:
# Pipelined HTTP requests need to be queued up.
self.flow.pause_reading()
Expand Down Expand Up @@ -340,14 +335,8 @@ def on_response_complete(self):
if self.pipeline:
cycle, app = self.pipeline.pop()
task = self.loop.create_task(cycle.run_asgi(app))
task.add_done_callback(self.on_task_complete)
task.add_done_callback(self.tasks.discard)
self.tasks.add(task)
self.loop.call_later(
self.timeout_response, self.timeout_response_handler, task
)

def on_task_complete(self, task):
self.tasks.discard(task)

def shutdown(self):
"""
Expand Down Expand Up @@ -377,14 +366,6 @@ def timeout_keep_alive_handler(self):
if not self.transport.is_closing():
self.transport.close()

def timeout_response_handler(self, task):
"""
Called once per task, when the reponse timeout is reached.
"""
if not task.done():
self.logger.error("Task exceeded response timeout.")
task.cancel()


class RequestResponseCycle:
def __init__(
Expand Down Expand Up @@ -449,6 +430,8 @@ async def run_asgi(self, app):
self.logger.error(msg)
if not self.disconnected:
self.transport.close()
finally:
self.on_response = None

async def send_500_response(self):
await self.send(
Expand Down