Skip to content

Commit

Permalink
Fix a bug where bootstrap code wasn't called (#1894)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartfeenstra authored Aug 25, 2024
1 parent f2673b9 commit db402e8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 19 deletions.
3 changes: 3 additions & 0 deletions betty/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
from pathlib import Path


from betty import _bootstrap # 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]
29 changes: 29 additions & 0 deletions betty/_bootstrap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from asyncio import BaseEventLoop
from multiprocessing import set_start_method
from typing import Any


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


_bootstrapped = False


if not _bootstrapped:
_bootstrapped = True

_original_shutdown_default_executor = BaseEventLoop.shutdown_default_executor
BaseEventLoop.shutdown_default_executor = _shutdown_default_executor # type: ignore[assignment, callable-functiontype, method-assign, misc]

# Avoid `fork` so as not to start worker processes with unneeded resources.
# Use `spawn`, which is the Python 3.14 default.
set_start_method("spawn", force=True)
19 changes: 0 additions & 19 deletions betty/_patch.py

This file was deleted.

0 comments on commit db402e8

Please sign in to comment.