Skip to content

Commit

Permalink
Add a workaround for a bug in Python 3.12 that would randomly cause R…
Browse files Browse the repository at this point in the history
…untimeError to be raised when cleaning up thread pools during interpreter shutdown. (#1231)
  • Loading branch information
bartfeenstra authored Feb 1, 2024
1 parent 9dac6c5 commit e978045
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions betty/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
"""The Betty package root."""
from pathlib import Path

from betty import _patch # noqa: F401


# This lives here so it can be imported before any third-party Python modules are available.
_ROOT_DIRECTORY_PATH = Path(__file__).resolve().parents[1]
15 changes: 15 additions & 0 deletions betty/_patch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from asyncio import BaseEventLoop
from typing import Any

_original_shutdown_default_executor = BaseEventLoop.shutdown_default_executor


async def _shutdown_default_executor(loop: BaseEventLoop, *args: Any, **kwargs: Any) -> None:
try:
await _original_shutdown_default_executor(loop, *args, **kwargs)
except RuntimeError as error:
# Work around a bug in Python 3.12 that will randomly cause a RuntimeError with the
# following message to be raised.
if "can't create new thread at interpreter shutdown" not in str(error):
raise
BaseEventLoop.shutdown_default_executor = _shutdown_default_executor # type: ignore[assignment, method-assign]

0 comments on commit e978045

Please sign in to comment.