Skip to content

Commit 63110b0

Browse files
committed
Handle case when executor is shutdow
Avoid exception: Traceback (most recent call last): File "lib/python3.13/threading.py", line 1041, in _bootstrap_inner self.run() ~~~~~~~~^^ File "cassandra/cluster.py", line 4429, in run future = self._executor.submit(fn, *args, **kwargs) File "lib/python3.13/concurrent/futures/thread.py", line 171, in submit Error: raise RuntimeError('cannot schedule new futures after shutdown') RuntimeError: cannot schedule new futures after shutdown
1 parent 347f332 commit 63110b0

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

cassandra/cluster.py

+24-5
Original file line numberDiff line numberDiff line change
@@ -208,15 +208,20 @@ def new_f(self, *args, **kwargs):
208208
try:
209209
future = self.executor.submit(f, self, *args, **kwargs)
210210
future.add_done_callback(_future_completed)
211-
except Exception:
212-
log.exception("Failed to submit task to executor")
211+
except Exception as e:
212+
if not _is_executor_shutdown_exception(e):
213+
log.exception("Failed to submit task to executor")
213214

214215
return new_f
215216

216217

217218
_clusters_for_shutdown = set()
218219

219220

221+
def _is_executor_shutdown_exception(e: Exception) -> bool:
222+
return 'cannot schedule new futures after shutdown' in str(e)
223+
224+
220225
def _register_cluster_shutdown(cluster):
221226
_clusters_for_shutdown.add(cluster)
222227

@@ -3482,7 +3487,12 @@ def encode(val):
34823487
def submit(self, fn, *args, **kwargs):
34833488
""" Internal """
34843489
if not self.is_shutdown:
3485-
return self.cluster.executor.submit(fn, *args, **kwargs)
3490+
try:
3491+
return self.cluster.executor.submit(fn, *args, **kwargs)
3492+
except RuntimeError as e:
3493+
if _is_executor_shutdown_exception(e):
3494+
return None
3495+
raise
34863496

34873497
def get_pool_state(self):
34883498
return dict((host, pool.get_state()) for host, pool in tuple(self._pools.items()))
@@ -3820,7 +3830,11 @@ def _get_and_set_reconnection_handler(self, new_handler):
38203830
def _submit(self, *args, **kwargs):
38213831
try:
38223832
if not self._cluster.is_shutdown:
3823-
return self._cluster.executor.submit(*args, **kwargs)
3833+
try:
3834+
return self._cluster.executor.submit(*args, **kwargs)
3835+
except RuntimeError as e:
3836+
if _is_executor_shutdown_exception(e):
3837+
return None
38243838
except ReferenceError:
38253839
pass
38263840
return None
@@ -4426,7 +4440,12 @@ def run(self):
44264440
self._scheduled_tasks.discard(task)
44274441
fn, args, kwargs = task
44284442
kwargs = dict(kwargs)
4429-
future = self._executor.submit(fn, *args, **kwargs)
4443+
try:
4444+
future = self._executor.submit(fn, *args, **kwargs)
4445+
except RuntimeError as e:
4446+
if _is_executor_shutdown_exception(e):
4447+
return
4448+
raise
44304449
future.add_done_callback(self._log_if_failed)
44314450
else:
44324451
self._queue.put_nowait((run_at, i, task))

0 commit comments

Comments
 (0)