From 8b2ced122ca746c3b5c9effb4db73e43b8f9c9c5 Mon Sep 17 00:00:00 2001 From: Jelle Spijker Date: Wed, 15 Dec 2021 15:07:23 +0100 Subject: [PATCH] Explicitly close the downloaded curapackage Sometime very small curapackage where download and the `canInstallChanged` signal was emitted before the zipfile was completely processed. This could result in a failed installation, which was often the case for materials. I also narrowed down the try-catch block. --- plugins/Marketplace/PackageList.py | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/plugins/Marketplace/PackageList.py b/plugins/Marketplace/PackageList.py index 390bf841df..57d8c22183 100644 --- a/plugins/Marketplace/PackageList.py +++ b/plugins/Marketplace/PackageList.py @@ -209,24 +209,26 @@ class PackageList(ListModel): ) def _downloadFinished(self, package_id: str, reply: "QNetworkReply", update: bool = False) -> None: - try: - with tempfile.NamedTemporaryFile(mode = "wb+", suffix = ".curapackage", delete = False) as temp_file: + with tempfile.NamedTemporaryFile(mode = "wb+", suffix = ".curapackage", delete = False) as temp_file: + try: bytes_read = reply.read(self.DISK_WRITE_BUFFER_SIZE) while bytes_read: temp_file.write(bytes_read) bytes_read = reply.read(self.DISK_WRITE_BUFFER_SIZE) - self._to_install[package_id] = temp_file.name - self._ongoing_requests["download_package"] = None - self.canInstallChanged.emit(package_id, update) - except IOError as e: - Logger.error(f"Failed to write downloaded package to temp file {e}") - temp_file.close() - self._downloadError(package_id, update) - except RuntimeError: - # Setting the ownership of this object to not qml can still result in a RuntimeError. Which can occur when quickly toggling - # between de-/constructing Remote or Local PackageLists. This try-except is here to prevent a hard crash when the wrapped C++ object - # was deleted when it was still parsing the response - return + except IOError as e: + Logger.error(f"Failed to write downloaded package to temp file {e}") + temp_file.close() + self._downloadError(package_id, update) + except RuntimeError: + # Setting the ownership of this object to not qml can still result in a RuntimeError. Which can occur when quickly toggling + # between de-/constructing Remote or Local PackageLists. This try-except is here to prevent a hard crash when the wrapped C++ object + # was deleted when it was still parsing the response + temp_file.close() + return + temp_file.close() + self._to_install[package_id] = temp_file.name + self._ongoing_requests["download_package"] = None + self.canInstallChanged.emit(package_id, update) def _downloadError(self, package_id: str, update: bool = False, reply: Optional["QNetworkReply"] = None, error: Optional["QNetworkReply.NetworkError"] = None) -> None: if reply: