Skip to content

Fix Python 3.12 deprecation warning about os.fork() #1070

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

Merged
merged 2 commits into from
Jun 5, 2025

Conversation

mhucka
Copy link
Contributor

@mhucka mhucka commented Jun 4, 2025

In Python 3.12, on Linux at least, src/openfermion/testing/performance_benchmarks_test.py produces the following warnings:

~/.pyenv/versions/3.12.5/lib/python3.12/multiprocessing/popen_fork.py:66: RuntimeWarning:
os.fork() was called. os.fork() is incompatible with multithreaded code, and JAX is
multithreaded, so this will likely lead to a deadlock.
self.pid = os.fork()
src/openfermion/testing/performance_benchmarks_test.py::test_run_linear_qop
src/openfermion/testing/performance_benchmarks_test.py::test_run_linear_qop
src/openfermion/testing/performance_benchmarks_test.py::test_run_linear_qop
src/openfermion/testing/performance_benchmarks_test.py::test_run_linear_qop
src/openfermion/testing/performance_benchmarks_test.py::test_run_linear_qop
~/.pyenv/versions/3.12.5/lib/python3.12/multiprocessing/popen_fork.py:66: 
DeprecationWarning: This process (pid=3069778) is multi-threaded, use of fork() 
may lead to deadlocks in the child. self.pid = os.fork()

This can be resolved by calling multiprocessing.set_start_method() in __init__ of the ParallelLinearQuibitOperator class.

google-labs-jules bot and others added 2 commits June 4, 2025 17:32
I've adjusted the multiprocessing start method to 'forkserver' within the `ParallelLinearQubitOperator` class. This should resolve warnings concerning `os.fork()` in multithreaded environments (specifically with JAX in Python 3.12) that could potentially lead to deadlocks.

This change ensures that `multiprocessing.set_start_method('forkserver', force=True)` is invoked only once upon the initialization of `ParallelLinearQubitOperator`. The tests in `performance_benchmarks_test.py` are now passing without any warnings.
@mhucka mhucka changed the title Fix Python 3.12 deprecation warning about os.for() Fix Python 3.12 deprecation warning about os.fork() Jun 4, 2025
@mhucka mhucka requested review from pavoljuhas and dstrain115 June 4, 2025 18:21
@mhucka mhucka self-assigned this Jun 4, 2025
@mhucka mhucka requested a review from fdmalone June 5, 2025 04:22
@fdmalone
Copy link
Collaborator

fdmalone commented Jun 5, 2025

Seems like the default behaviour will change in python 3.14 (away from fork), maybe worth opening an issue and adding it as a link in the code to revisit in the future.

@mhucka mhucka added this pull request to the merge queue Jun 5, 2025
Merged via the queue into quantumlib:master with commit fc05585 Jun 5, 2025
29 checks passed
@mhucka mhucka deleted the fix-multiprocessing-warnings branch June 5, 2025 17:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants