Handle asyncio timeouts. Ref #1307.

This commit is contained in:
grossmj 2018-04-28 17:42:02 +07:00
parent a56d5b453f
commit 305fe2e817
6 changed files with 40 additions and 21 deletions

View File

@ -198,7 +198,7 @@ class Docker(BaseManager):
pass pass
if progress_callback: if progress_callback:
progress_callback("Pull {} from docker hub".format(image)) progress_callback("Pulling '{}' from docker hub".format(image))
response = yield from self.http_query("POST", "images/create", params={"fromImage": image}, timeout=None) response = yield from self.http_query("POST", "images/create", params={"fromImage": image}, timeout=None)
# The pull api will stream status via an HTTP JSON stream # The pull api will stream status via an HTTP JSON stream
content = "" content = ""
@ -206,6 +206,10 @@ class Docker(BaseManager):
try: try:
chunk = yield from response.content.read(1024) chunk = yield from response.content.read(1024)
except aiohttp.ServerDisconnectedError: except aiohttp.ServerDisconnectedError:
log.error("Disconnected from server while pulling Docker image '{}' from docker hub".format(image))
break
except asyncio.TimeoutError:
log.error("Timeout while pulling Docker image '{}' from docker hub".format(image))
break break
if not chunk: if not chunk:
break break

View File

@ -90,7 +90,10 @@ def export_project(project, temporary_dir, include_images=False, keep_compute_id
f = open(fd, "wb", closefd=True) f = open(fd, "wb", closefd=True)
response = yield from compute.download_file(project, compute_file["path"]) response = yield from compute.download_file(project, compute_file["path"])
while True: while True:
data = yield from response.content.read(1024) try:
data = yield from response.content.read(1024)
except asyncio.TimeoutError:
raise aiohttp.web.HTTPRequestTimeout(text="Timeout when downloading file '{}' from remote compute server {}:{}".format(compute_file["path"], compute.host, compute.port))
if not data: if not data:
break break
f.write(data) f.write(data)
@ -250,7 +253,10 @@ def _export_remote_images(project, compute_id, image_type, image, project_zipfil
raise aiohttp.web.HTTPConflict(text="Cannot export image from '{}' compute. Compute returned status code {}.".format(compute_id, response.status)) raise aiohttp.web.HTTPConflict(text="Cannot export image from '{}' compute. Compute returned status code {}.".format(compute_id, response.status))
while True: while True:
data = yield from response.content.read(1024) try:
data = yield from response.content.read(1024)
except asyncio.TimeoutError:
raise aiohttp.web.HTTPRequestTimeout(text="Timeout when downloading image '{}' from remote compute server {}:{}".format(image, compute.host, compute.port))
if not data: if not data:
break break
f.write(data) f.write(data)

View File

@ -319,10 +319,13 @@ class ProjectHandler:
os.makedirs(os.path.dirname(path), exist_ok=True) os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, 'wb+') as f: with open(path, 'wb+') as f:
while True: while True:
packet = yield from request.content.read(1024) try:
if not packet: chunk = yield from request.content.read(1024)
except asyncio.TimeoutError:
raise aiohttp.web.HTTPRequestTimeout(text="Timeout when writing to file '{}'".format(path))
if not chunk:
break break
f.write(packet) f.write(chunk)
except FileNotFoundError: except FileNotFoundError:
raise aiohttp.web.HTTPNotFound() raise aiohttp.web.HTTPNotFound()
@ -380,10 +383,10 @@ class ProjectHandler:
try: try:
with tempfile.SpooledTemporaryFile(max_size=10000) as temp: with tempfile.SpooledTemporaryFile(max_size=10000) as temp:
while True: while True:
packet = yield from request.content.read(1024) chunk = yield from request.content.read(1024)
if not packet: if not chunk:
break break
temp.write(packet) temp.write(chunk)
project.import_zip(temp, gns3vm=bool(int(request.GET.get("gns3vm", "1")))) project.import_zip(temp, gns3vm=bool(int(request.GET.get("gns3vm", "1"))))
except OSError as e: except OSError as e:
raise aiohttp.web.HTTPInternalServerError(text="Could not import the project: {}".format(e)) raise aiohttp.web.HTTPInternalServerError(text="Could not import the project: {}".format(e))

View File

@ -409,8 +409,6 @@ class NodeHandler:
node_type = node.node_type node_type = node.node_type
path = "/project-files/{}/{}/{}".format(node_type, node.id, path) path = "/project-files/{}/{}/{}".format(node_type, node.id, path)
data = yield from request.content.read() #FIXME: are we handling timeout or large files correctly?
data = yield from request.content.read()
yield from node.compute.http_query("POST", "/projects/{project_id}/files{path}".format(project_id=project.id, path=path), data=data, timeout=None, raw=True) yield from node.compute.http_query("POST", "/projects/{project_id}/files{path}".format(project_id=project.id, path=path), data=data, timeout=None, raw=True)
response.set_status(201) response.set_status(201)

View File

@ -335,10 +335,10 @@ class ProjectHandler:
try: try:
with tempfile.SpooledTemporaryFile(max_size=10000) as temp: with tempfile.SpooledTemporaryFile(max_size=10000) as temp:
while True: while True:
packet = yield from request.content.read(1024) chunk = yield from request.content.read(1024)
if not packet: if not chunk:
break break
temp.write(packet) temp.write(chunk)
project = yield from import_project(controller, request.match_info["project_id"], temp, location=path, name=name) project = yield from import_project(controller, request.match_info["project_id"], temp, location=path, name=name)
except OSError as e: except OSError as e:
raise aiohttp.web.HTTPInternalServerError(text="Could not import the project: {}".format(e)) raise aiohttp.web.HTTPInternalServerError(text="Could not import the project: {}".format(e))
@ -448,10 +448,13 @@ class ProjectHandler:
try: try:
with open(path, 'wb+') as f: with open(path, 'wb+') as f:
while True: while True:
packet = yield from request.content.read(1024) try:
if not packet: chunk = yield from request.content.read(1024)
except asyncio.TimeoutError:
raise aiohttp.web.HTTPRequestTimeout(text="Timeout when writing to file '{}'".format(path))
if not chunk:
break break
f.write(packet) f.write(chunk)
except FileNotFoundError: except FileNotFoundError:
raise aiohttp.web.HTTPNotFound() raise aiohttp.web.HTTPNotFound()
except PermissionError: except PermissionError:

View File

@ -17,6 +17,8 @@
import os import os
import aiohttp import aiohttp
import asyncio
from gns3server.web.route import Route from gns3server.web.route import Route
from gns3server.controller import Controller from gns3server.controller import Controller
@ -66,10 +68,13 @@ class SymbolHandler:
try: try:
with open(path, 'wb') as f: with open(path, 'wb') as f:
while True: while True:
packet = yield from request.content.read(1024) try:
if not packet: chunk = yield from request.content.read(1024)
except asyncio.TimeoutError:
raise aiohttp.web.HTTPRequestTimeout(text="Timeout when writing to symbol '{}'".format(path))
if not chunk:
break break
f.write(packet) f.write(chunk)
except OSError as e: except OSError as e:
raise aiohttp.web.HTTPConflict(text="Could not write symbol file '{}': {}".format(path, e)) raise aiohttp.web.HTTPConflict(text="Could not write symbol file '{}': {}".format(path, e))
# Reset the symbol list # Reset the symbol list