From 2b55e3f7a7d1acbd10d57742b6f1c70d7b848916 Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Tue, 21 Jan 2020 15:46:14 +0100 Subject: [PATCH] Catch AttributeError when deleting temp file on image upload When the image upload finishes, we try to delete the temporary file that was used for the upload. However, depending on how Horizon is deployed, instead of a file it can be a BytesIO instance, which leads to an AttributeError. This happens in a separate thread, so Horizon itself doesn't crash, but it's better to catch that exception. Change-Id: Iaa66d9c87060c1e001f3f6e31f630d6092df55d3 Closes-Bug: #1866632 --- openstack_dashboard/api/glance.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/openstack_dashboard/api/glance.py b/openstack_dashboard/api/glance.py index aa2953f9e7..f5b53e716a 100644 --- a/openstack_dashboard/api/glance.py +++ b/openstack_dashboard/api/glance.py @@ -511,13 +511,17 @@ def image_create(request, **kwargs): try: return glanceclient(request).images.upload(image.id, data) finally: - filename = str(data.file.name) try: - os.remove(filename) - except OSError as e: - LOG.warning('Failed to remove temporary image file ' - '%(file)s (%(e)s)', - {'file': filename, 'e': e}) + filename = str(data.file.name) + except AttributeError: + pass + else: + try: + os.remove(filename) + except OSError as e: + LOG.warning('Failed to remove temporary image file ' + '%(file)s (%(e)s)', + {'file': filename, 'e': e}) thread.start_new_thread(upload, ()) return Image(image)