Skip to content

Commit 4a63cb1

Browse files
authored
Bug: Extend error handling in backend_write_file() (#983)
* Bug: Extend error handling in backend_write_file() * dump first
1 parent 3c5d8b1 commit 4a63cb1

2 files changed

Lines changed: 39 additions & 8 deletions

File tree

src/executorlib/task_scheduler/file/backend.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,22 @@ def backend_write_file(file_name: str, output: Any, runtime: float) -> None:
4545
"""
4646
file_name_out = os.path.splitext(file_name)[0][:-2]
4747
os.rename(file_name, file_name_out + "_r.h5")
48-
if "result" in output:
49-
dump(
50-
file_name=file_name_out + "_r.h5",
51-
data_dict={"output": output["result"], "runtime": runtime},
52-
)
53-
else:
48+
try:
49+
if "result" in output:
50+
dump(
51+
file_name=file_name_out + "_r.h5",
52+
data_dict={"output": output["result"], "runtime": runtime},
53+
)
54+
else:
55+
dump(
56+
file_name=file_name_out + "_r.h5",
57+
data_dict={"error": output["error"], "runtime": runtime},
58+
)
59+
except Exception as serialize_error:
60+
# Serialization failed — store the error so the job is not stuck
5461
dump(
5562
file_name=file_name_out + "_r.h5",
56-
data_dict={"error": output["error"], "runtime": runtime},
63+
data_dict={"error": serialize_error, "runtime": runtime},
5764
)
5865
os.rename(file_name_out + "_r.h5", file_name_out + "_o.h5")
5966

tests/unit/task_scheduler/file/test_backend.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88

99
try:
10-
from executorlib.task_scheduler.file.backend import backend_execute_task_in_file
10+
from executorlib.task_scheduler.file.backend import backend_execute_task_in_file, backend_write_file
1111
from executorlib.task_scheduler.file.shared import _check_task_output, _convert_args_and_kwargs, FutureItem
1212
from executorlib.standalone.hdf import dump, get_runtime
1313
from executorlib.standalone.serialize import serialize_funct
@@ -64,6 +64,30 @@ def test_execute_function_mixed(self):
6464
self.assertTrue(future_file_obj.done())
6565
self.assertEqual(future_file_obj.result(), 3)
6666

67+
def test_backend_write_file(self):
68+
cache_directory = os.path.abspath("executorlib_cache")
69+
os.makedirs(cache_directory, exist_ok=True)
70+
file_name = os.path.join(cache_directory, "test_file_i.h5")
71+
dump(file_name=file_name, data_dict={"fn": my_funct, "args": [1], "kwargs": {"b": 2}})
72+
backend_write_file(file_name=file_name, output={"result": 3}, runtime=0.1)
73+
future_file_obj = FutureItem(
74+
file_name=os.path.join(cache_directory, "test_file_o.h5")
75+
)
76+
self.assertTrue(future_file_obj.done())
77+
self.assertEqual(future_file_obj.result(), 3)
78+
79+
def test_backend_write_file_serialization_error(self):
80+
cache_directory = os.path.abspath("executorlib_cache")
81+
os.makedirs(cache_directory, exist_ok=True)
82+
file_name = os.path.join(cache_directory, "test_file_i.h5")
83+
dump(file_name=file_name, data_dict={"fn": my_funct, "args": [1], "kwargs": {"b": 2}})
84+
backend_write_file(file_name=file_name, output={"result": Future()}, runtime=0.1)
85+
future_file_obj = FutureItem(
86+
file_name=os.path.join(cache_directory, "test_file_o.h5")
87+
)
88+
with self.assertRaises(Exception):
89+
future_file_obj.result()
90+
6791
def test_execute_function_mixed_selector_convert(self):
6892
cache_directory = os.path.abspath("executorlib_cache")
6993
os.makedirs(cache_directory, exist_ok=True)

0 commit comments

Comments
 (0)