Skip to content

Commit 1c381d7

Browse files
Raise runtime error when submitting after executor shutdown (#851)
* Raise runtime error when submitting after executor shutdown * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 8957a38 commit 1c381d7

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/executorlib/executor/base.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class BaseExecutor(FutureExecutor, ABC):
2121

2222
def __init__(self, executor: TaskSchedulerBase):
2323
self._task_scheduler = executor
24+
self._is_active = True
2425

2526
@property
2627
def max_workers(self) -> Optional[int]:
@@ -99,9 +100,12 @@ def submit( # type: ignore
99100
Returns:
100101
Future: A Future representing the given call.
101102
"""
102-
return self._task_scheduler.submit(
103-
*([fn] + list(args)), resource_dict=resource_dict, **kwargs
104-
)
103+
if self._is_active:
104+
return self._task_scheduler.submit(
105+
*([fn] + list(args)), resource_dict=resource_dict, **kwargs
106+
)
107+
else:
108+
raise RuntimeError("cannot schedule new futures after shutdown")
105109

106110
def shutdown(self, wait: bool = True, *, cancel_futures: bool = False):
107111
"""
@@ -119,6 +123,7 @@ def shutdown(self, wait: bool = True, *, cancel_futures: bool = False):
119123
cancelled.
120124
"""
121125
self._task_scheduler.shutdown(wait=wait, cancel_futures=cancel_futures)
126+
self._is_active = False
122127

123128
def __len__(self) -> int:
124129
"""
@@ -143,3 +148,4 @@ def __exit__(self, *args, **kwargs) -> None:
143148
Exit method called when exiting the context manager.
144149
"""
145150
self._task_scheduler.__exit__(*args, **kwargs)
151+
self._is_active = False

tests/test_singlenodeexecutor_noblock.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,10 @@ def test_single_node_executor_init_function(self):
166166
with SingleNodeExecutor(max_workers=2, init_function=exit_funct, block_allocation=True) as exe:
167167
f = exe.submit(sum, [1, 1])
168168
print(f.result())
169+
170+
def test_single_node_executor_exit(self):
171+
exe = SingleNodeExecutor(max_workers=2)
172+
self.assertEqual(exe.submit(sum, [1,2,3]).result(), 6)
173+
exe.shutdown()
174+
with self.assertRaises(RuntimeError):
175+
exe.submit(sum, [1, 2, 3])

0 commit comments

Comments
 (0)