diff --git a/test/__init__.py b/test/__init__.py index 7aff0a4620..eb588abec9 100644 --- a/test/__init__.py +++ b/test/__init__.py @@ -950,7 +950,17 @@ def _target() -> None: ctx = multiprocessing.get_context("fork") proc = ctx.Process(target=_target) - proc.start() + # Python 3.12+ warns when os.fork() runs in a multi-threaded process. The + # warning is a general thread-count heuristic; benign here because the only + # extra threads are pymongo's, whose locks are reset via register_at_fork + # in the child. Suppressed only in this helper, not globally (PYTHON-5874). + with warnings.catch_warnings(): + warnings.filterwarnings( + "ignore", + message=r".*use of fork\(\) may lead to deadlocks.*", + category=DeprecationWarning, + ) + proc.start() try: yield proc # type: ignore finally: diff --git a/test/asynchronous/__init__.py b/test/asynchronous/__init__.py index 4ecaa589cf..d31e653705 100644 --- a/test/asynchronous/__init__.py +++ b/test/asynchronous/__init__.py @@ -950,7 +950,17 @@ def _target() -> None: ctx = multiprocessing.get_context("fork") proc = ctx.Process(target=_target) - proc.start() + # Python 3.12+ warns when os.fork() runs in a multi-threaded process. The + # warning is a general thread-count heuristic; benign here because the only + # extra threads are pymongo's, whose locks are reset via register_at_fork + # in the child. Suppressed only in this helper, not globally (PYTHON-5874). + with warnings.catch_warnings(): + warnings.filterwarnings( + "ignore", + message=r".*use of fork\(\) may lead to deadlocks.*", + category=DeprecationWarning, + ) + proc.start() try: yield proc # type: ignore finally: diff --git a/test/test_fork.py b/test/test_fork.py index 4b33dc76dc..e8a551d058 100644 --- a/test/test_fork.py +++ b/test/test_fork.py @@ -29,6 +29,8 @@ from test.utils_shared import is_greenthread_patched +# self.fork suppresses the Python 3.12+ fork() DeprecationWarning; it is +# benign for these intentional forks (PYTHON-5874). @unittest.skipIf( not hasattr(os, "register_at_fork"), "register_at_fork not available in this version of Python" ) @@ -36,10 +38,6 @@ is_greenthread_patched(), "gevent does not support POSIX-style forking.", ) -@unittest.skipIf( - sys.version_info >= (3, 15), - "fork() in multi-threaded processes is deprecated in Python 3.15+ (PYTHON-5874)", -) class TestFork(IntegrationTest): def test_lock_client(self): # Forks the client with some items locked.