Repository: python/cpython
Pull request: gh-106723: forward -Xfrozen_modules option to spawned interpreters
<!--
Thanks for your contribution!
Please read this comment in its entirety. It's quite important.

# Forward frozen_modules xoption to subinterpreters

It should be in the following format:

```
gh-NNNNN: Summary of the changes made
```

Where: gh-NNNNN refers to the GitHub issue number.

Most PRs will require an issue number. Trivial changes, like fixing a typo, do not need an issue.

# Backport Pull Request title

If this is a backport PR (PR made against branches other than `main`),
please ensure that the PR title is in the following format:

```
[X.Y] <title from the original PR> (GH-NNNN)
```

Where: [X.Y] is the branch name, e.g. [3.6].

GH-NNNN refers to the PR number from `main`.

-->

Fixes https://github.com/python/cpython/issues/106723


<!-- gh-issue-number: gh-106723 -->
* Issue: gh-106723
<!-- /gh-issue-number -->


Problem statement:
Multiprocessing not propagating -Xfrozen_modules=off
# Bug report

After python3.11 changes around frozen imports, when using multiprocessing contexts other than `fork`, the newly added `-Xfrozen_modules=off` isn't passed to spawned process interpreters.

Simple snippet demonstrating the issue:

```python
"""

$ python -Xfrozen_modules=off test.py
main: {'frozen_modules': 'off'}
forkserver: {}
spawn: {}
fork: {'frozen_modules': 'off'}

"""

import sys
import multiprocessing

def xoptions():
    return sys._xoptions

def main():
    print('main:', xoptions())
    for ctx in ('forkserver', 'spawn', 'fork'):
        with multiprocessing.get_context(ctx).Pool(1) as pool:
            print(f'{ctx}:', pool.apply(xoptions))

if __name__ == '__main__':
    main()
```

The issue seems to be `subprocess._args_from_interpreter_flags` not honoring `frozen_modules` key from `sys._xoptions`.

```bash
$ python -Xfrozen_modules=off -c 'import subprocess;print(subprocess._args_from_interpreter_flags())'
[]
```

# Your environment

python 3.11.4


<!-- gh-linked-prs -->
### Linked PRs
* gh-106724
* gh-107367
* gh-107368
<!-- /gh-linked-prs -->

Hints from issue discussion:

Commit message: NEWS updated (blurb_it fix)

Patch to review:
diff --git a/Lib/subprocess.py b/Lib/subprocess.py
index fbc76b8d0f14b2..6df5dd551ea67e 100644
--- a/Lib/subprocess.py
+++ b/Lib/subprocess.py
@@ -346,7 +346,7 @@ def _args_from_interpreter_flags():
     if dev_mode:
         args.extend(('-X', 'dev'))
     for opt in ('faulthandler', 'tracemalloc', 'importtime',
-                'showrefcount', 'utf8'):
+                'frozen_modules', 'showrefcount', 'utf8'):
         if opt in xoptions:
             value = xoptions[opt]
             if value is True:
diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-07-13-14-55-45.gh-issue-106723.KsMufQ.rst b/Misc/NEWS.d/next/Core and Builtins/2023-07-13-14-55-45.gh-issue-106723.KsMufQ.rst
new file mode 100644
index 00000000000000..1d70f8b3cbaf33
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2023-07-13-14-55-45.gh-issue-106723.KsMufQ.rst	
@@ -0,0 +1 @@
+Fix -Xfrozen_modules=off passing to multiprocessing subinterpreters.


Code review comments:
diff_hunk: @@ -0,0 +1 @@
+Fix -Xfrozen_modules=off passing to multiprocessing subinterpreters.
path: Misc/NEWS.d/next/Core and Builtins/2023-07-13-14-55-45.gh-issue-106723.KsMufQ.rst
text: @user1:
```suggestion
Propagate ``frozen_modules`` to multiprocessing subinterpreters.
```