From 8c3a78e36a34ed5ec2d1d0909cde4489accb7595 Mon Sep 17 00:00:00 2001 From: Ihsan Ullah Date: Thu, 2 Apr 2026 04:05:38 +0500 Subject: [PATCH 1/2] safeguards added in bundle unpacking and made unkown error clear for users --- src/apps/competitions/tasks.py | 35 +++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/src/apps/competitions/tasks.py b/src/apps/competitions/tasks.py index 623ac2522..046c3b62b 100644 --- a/src/apps/competitions/tasks.py +++ b/src/apps/competitions/tasks.py @@ -353,11 +353,14 @@ def mark_status_as_failed_and_delete_dataset(competition_creation_status, detail with NamedTemporaryFile(mode="w+b") as temp_file: logger.info(f"Download competition bundle: {competition_dataset.data_file.name}") competition_bundle_url = make_url_sassy(competition_dataset.data_file.url) - with requests.get(competition_bundle_url, stream=True) as r: - r.raise_for_status() - for chunk in r.iter_content(chunk_size=8192): - temp_file.write(chunk) - r.close() + try: + with requests.get(competition_bundle_url, stream=True) as r: + r.raise_for_status() + for chunk in r.iter_content(chunk_size=8192): + temp_file.write(chunk) + r.close() + except requests.exceptions.RequestException as e: + raise CompetitionUnpackingException(f"Failed to download bundle from storage: {e}") # seek back to the start of the tempfile after writing to it.. temp_file.seek(0) @@ -371,10 +374,17 @@ def mark_status_as_failed_and_delete_dataset(competition_creation_status, detail # Read metadata (competition.yaml) yaml_path = os.path.join(temp_directory, "competition.yaml") if not os.path.exists(yaml_path): - raise CompetitionUnpackingException("competition.yaml is missing from zip, check your folder structure " - "to make sure it is in the root directory.") - with open(yaml_path) as f: - competition_yaml = yaml.safe_load(f.read()) + raise CompetitionUnpackingException( + "competition.yaml is missing from zip, check your folder structure " + "to make sure it is in the root directory." + ) + try: + with open(yaml_path) as f: + competition_yaml = yaml.safe_load(f.read()) + except yaml.YAMLError as e: + raise CompetitionUnpackingException(f"Error parsing competition.yaml: {e}") + except Exception as e: + raise CompetitionUnpackingException(f"Failed to read competition.yaml: {e}") yaml_version = str(competition_yaml.get('version', '1')) @@ -428,11 +438,14 @@ def _get_error_string(error_dict): mark_status_as_failed_and_delete_dataset(status, message) raise e - except Exception as e: # noqa: E722 + except Exception as e: # These are critical uncaught exceptions, make sure the end user is at least informed # that unpacking has failed -- do not share unhandled exception details logger.error(traceback.format_exc()) - message = "Unpacking the bundle failed. Here is the error log: {}".format(e) + if isinstance(e, KeyError): + message = f"Unpacking the bundle failed. A required key or referenced index ({e}) was not found in the YAML. Check that all mandatory fields are present and that any item referenced by index is correctly defined." + else: + message = "Unpacking the bundle failed. Here is the error log: {}".format(e) mark_status_as_failed_and_delete_dataset(status, message) From 298ce2bab9e966395eb79085753a5dafc5f0816c Mon Sep 17 00:00:00 2001 From: Ihsan Ullah Date: Thu, 2 Apr 2026 04:12:02 +0500 Subject: [PATCH 2/2] string formatting fixed --- src/apps/competitions/tasks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/apps/competitions/tasks.py b/src/apps/competitions/tasks.py index 046c3b62b..d72e9191a 100644 --- a/src/apps/competitions/tasks.py +++ b/src/apps/competitions/tasks.py @@ -445,7 +445,7 @@ def _get_error_string(error_dict): if isinstance(e, KeyError): message = f"Unpacking the bundle failed. A required key or referenced index ({e}) was not found in the YAML. Check that all mandatory fields are present and that any item referenced by index is correctly defined." else: - message = "Unpacking the bundle failed. Here is the error log: {}".format(e) + message = f"Unpacking the bundle failed. Here is the error log: {e}" mark_status_as_failed_and_delete_dataset(status, message)