diff --git a/src/executorlib/task_scheduler/file/shared.py b/src/executorlib/task_scheduler/file/shared.py index 5e06547d..0d45d9f8 100644 --- a/src/executorlib/task_scheduler/file/shared.py +++ b/src/executorlib/task_scheduler/file/shared.py @@ -140,6 +140,16 @@ def execute_tasks_h5( cache_key=cache_key, ) data_dict["error_log_file"] = error_log_file + if task_key in memory_dict and memory_dict[task_key].done(): + # The previous task with the same key already finished, but was not yet + # removed by _refresh_memory_dict(). Handle the new task like a new + # submission, rather than as a duplicate which is never resolved. + _remove_finished_task( + task_key=task_key, + memory_dict=memory_dict, + process_dict=process_dict, + status_check_dict=status_check_dict, + ) if serialize_exception is not None: task_dict["future"].set_exception(serialize_exception) elif task_key not in memory_dict: @@ -300,6 +310,27 @@ def _check_task_output( return future_obj +def _remove_finished_task( + task_key: str, + memory_dict: dict, + process_dict: dict, + status_check_dict: dict, +) -> None: + """ + Remove the bookkeeping of a finished task, so a new task with the same key can be submitted. + + Args: + task_key (str): The key of the finished task. + memory_dict (dict): The dictionary mapping task keys to their associated future objects. + process_dict (dict): The dictionary mapping task keys to process handles or queue IDs. + status_check_dict (dict): The dictionary mapping task keys to the last time their queuing + system job status was queried. + """ + del memory_dict[task_key] + process_dict.pop(task_key, None) + status_check_dict.pop(task_key, None) + + def _update_future( future_obj: Future, exec_flag: bool, no_error_flag: bool, result: Any ) -> None: diff --git a/tests/unit/task_scheduler/file/test_serial.py b/tests/unit/task_scheduler/file/test_serial.py index 86d6a044..43a6d66f 100644 --- a/tests/unit/task_scheduler/file/test_serial.py +++ b/tests/unit/task_scheduler/file/test_serial.py @@ -72,6 +72,22 @@ def test_submit_dependency_on_failed_task(self): ) ) + def test_resubmit_finished_task_with_same_cache_key(self): + with FileTaskScheduler(execute_function=subprocess_execute) as exe: + for i in range(3): + resource_dict = {"cache_key": "error_" + str(i)} + fs1 = exe.submit(get_error, a=1, resource_dict=resource_dict) + with self.assertRaises(ValueError): + fs1.result(timeout=15) + fs2 = exe.submit(get_error, a=1, resource_dict=resource_dict) + with self.assertRaises(ValueError): + fs2.result(timeout=15) + resource_dict = {"cache_key": "result_" + str(i)} + fs3 = exe.submit(my_funct, 1, b=2, resource_dict=resource_dict) + self.assertEqual(fs3.result(timeout=15), 3) + fs4 = exe.submit(my_funct, 1, b=2, resource_dict=resource_dict) + self.assertEqual(fs4.result(timeout=15), 3) + def test_submit_dependency_on_duplicate_task(self): with FileTaskScheduler(execute_function=subprocess_execute) as exe: fs1 = exe.submit(my_funct, 1, b=2) @@ -383,6 +399,49 @@ def test_executor_function_unpicklable_argument_with_cache_key(self): q.put({"shutdown": True, "wait": True}) process.join() + def test_executor_function_resubmit_after_failed_submission(self): + fs_bad_1 = Future() + fs_bad_2 = Future() + fs_good = Future() + q = Queue() + for fs in [fs_bad_1, fs_bad_2]: + q.put( + { + "fn": len, + "args": ([Unpicklable()],), + "kwargs": {}, + "future": fs, + "resource_dict": {"cache_key": "bad/key"}, + } + ) + q.put( + { + "fn": my_funct, + "args": (), + "kwargs": {"a": 1, "b": 2}, + "future": fs_good, + "resource_dict": {}, + } + ) + cache_dir = os.path.abspath("executorlib_cache") + os.makedirs(cache_dir, exist_ok=True) + process = Thread( + target=execute_tasks_h5, + kwargs={ + "future_queue": q, + "execute_function": subprocess_execute, + "executor_kwargs": {"cores": 1, "cwd": None, "cache_directory": cache_dir}, + "terminate_function": subprocess_terminate, + }, + ) + process.start() + for fs in [fs_bad_1, fs_bad_2]: + with self.assertRaises(TypeError): + fs.result(timeout=15) + self.assertEqual(fs_good.result(timeout=15), 3) + q.put({"shutdown": True, "wait": True}) + process.join() + def test_convert_args_and_kwargs(self): f1 = Future() f1.set_result(1)