Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix "RuntimeError: cannot schedule new futures after interpreter shutdown" #1025

Open
wants to merge 5 commits into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/versionhistory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ APScheduler, see the :doc:`migration section <migration>`.

- Fixed ``scheduler.shutdown()`` not raising ``SchedulerNotRunning`` (or raising the
wrong exception) for asynchronous schedulers when the scheduler is in fact not running
- Fixed "RuntimeError: cannot schedule new futures after interpreter shutdown" (#985
<https://github.com/agronholm/apscheduler/issues/985>_; PR by @i-am-darshil)

**3.11.0**

Expand Down
22 changes: 19 additions & 3 deletions src/apscheduler/executors/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@

from apscheduler.executors.base import BaseExecutor, run_job

SHUTDOWN_MESSAGES = {
"cannot schedule new futures after shutdown",
"cannot schedule new futures after interpreter shutdown",
}


class BasePoolExecutor(BaseExecutor):
@abstractmethod
Expand All @@ -24,9 +29,20 @@ def callback(f):
else:
self._run_job_success(job.id, f.result())

f = self._pool.submit(
run_job, job, job._jobstore_alias, run_times, self._logger.name
)
try:
f = self._pool.submit(
run_job, job, job._jobstore_alias, run_times, self._logger.name
)
except RuntimeError as e:
# There is a possible race condition between shutdown being invoked and the job being submitted to pool.
# This helps with not polluting logs with RuntimeError traces.
if str(e) in SHUTDOWN_MESSAGES:
self._logger.warning(
"Executor has been shut down, skipping job %s: %s", job.id, e
)
return
else:
raise # Re-raise unexpected RuntimeErrors
f.add_done_callback(callback)

def shutdown(self, wait=True):
Expand Down
27 changes: 27 additions & 0 deletions tests/test_executors.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import gc
import logging
import os
import signal
import time
Expand Down Expand Up @@ -273,6 +274,32 @@ async def test_run_coroutine_job(asyncio_scheduler, asyncio_executor, exception)
assert events[0].retval is True


@pytest.mark.parametrize(
"message,raise_error",
[
("cannot schedule new futures after shutdown", False),
("cannot schedule new futures after interpreter shutdown", False),
("random RuntimeError", True),
],
)
def test_pool_shutdown(
mock_scheduler, executor, create_job, timezone, caplog, message, raise_error
):
executor._pool.submit = MagicMock(side_effect=RuntimeError(message))
with caplog.at_level(logging.WARNING):
try:
job = create_job(func=wait_event, max_instances=2, next_run_time=None)
run_time = datetime.now(timezone)
executor.submit_job(job, [run_time])
if raise_error:
pytest.fail("Random RuntimeErrors should be raised!")
except RuntimeError:
if not raise_error:
pytest.fail(
"Shutdown RuntimeErrors should be caught and logged, not raised!"
)


@pytest.mark.parametrize("exception", [False, True])
@pytest.mark.anyio
async def test_run_coroutine_job_tornado(
Expand Down