Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 923847 - sys-apps/portage: subprocess.Popen with stdio redirection triggers unsafe os.fork()
Summary: sys-apps/portage: subprocess.Popen with stdio redirection triggers unsafe os....
Status: RESOLVED INVALID
Alias: None
Product: Portage Development
Classification: Unclassified
Component: Core (show other bugs)
Hardware: All Linux
: Normal normal (vote)
Assignee: Portage team
URL:
Whiteboard:
Keywords:
Depends on:
Blocks: 914876
  Show dependency tree
 
Reported: 2024-02-05 19:01 UTC by Zac Medico
Modified: 2024-02-14 06:25 UTC (History)
1 user (show)

See Also:
Package list:
Runtime testing required: ---


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Zac Medico gentoo-dev 2024-02-05 19:01:48 UTC
When subprocess.Popen is used with stdio redirection, it can't use posix_spawn, which triggers unsafe os.fork() as shown by this warning:

lib/portage/tests/emerge/test_binpkg_fetch.py::BinpkgFetchtestCase::testLocalFilePkgSyncUpdate
  /usr/lib/python3.12/multiprocessing/popen_fork.py:66: DeprecationWarning: This process (pid=2493839) is multi-threaded, use of fork() may lead to deadlocks in the child.
    self.pid = os.fork()
Comment 1 Zac Medico gentoo-dev 2024-02-05 21:33:03 UTC
Maybe subprocess.Popen could be fixed to support stdio redirection with posix_spawn, since posix_spawn supports file descriptor inheritance similar to fork/exec, and file actions in the child process including posix_spawn_file_actions_adddup2 and posix_spawn_file_actions_addclose.
Comment 2 Zac Medico gentoo-dev 2024-02-05 21:43:19 UTC
There's https://github.com/python/cpython/issues/114467 to add subprocess support for cwd with posix_spawn.

We might use portage.process.spawn to implement a subset of the subprocess.Popen interface, since it can support all of the required features via multiprocessing.Process with the "spawn" multiprocessing start method.
Comment 3 Mike Gilbert gentoo-dev 2024-02-05 22:09:43 UTC
I'm curious how subprocess.Popen ends up calling into multiprocessing.popen_fork in the first place.

Reading through the code, I think it should make these calls:

subprocess.Popen._execute_child
_posixsubprocess.fork_exec(..., use_vfork=True)
vfork()
Comment 4 Zac Medico gentoo-dev 2024-02-05 22:41:12 UTC
(In reply to Mike Gilbert from comment #3)
> I'm curious how subprocess.Popen ends up calling into
> multiprocessing.popen_fork in the first place.
> 
> Reading through the code, I think it should make these calls:
> 
> subprocess.Popen._execute_child
> _posixsubprocess.fork_exec(..., use_vfork=True)
> vfork()

Yeah, you're right. Now that you mention it I can't reproduce the warning message without the pytest -n auto --dist=worksteal arguments, so I guess those args only trigger it via some kind of multiprocessing in pytest.

It seems like there's no problem here, and that's great! Thanks!
Comment 5 Zac Medico gentoo-dev 2024-02-14 06:25:59 UTC
Actually it wasn't using the multiprocessing spawn start method under the pytest-xdist plugin when I thought it was, and I fixed that here:

https://gitweb.gentoo.org/proj/portage.git/commit/?id=3110ec376cbcb1f5b7fb82ba30ec958798bb32cf

commit 3110ec376cbcb1f5b7fb82ba30ec958798bb32cf
Author: Zac Medico <zmedico@gentoo.org>
Date:   2024-02-12 16:46:52 -0800

    actions: Fix interaction between start-method and pytest-xdist
    
    Use portage.test.TestCase setUp method to setup the multiprocessing
    start method if needed. It needs to be done relatively late in order
    to work with the pytest-xdist plugin due to execnet usage.
    
    Bug: https://bugs.gentoo.org/914876
    Signed-off-by: Zac Medico <zmedico@gentoo.org>

However, this led to pytest-xdist workers crashing intermittently (bug 924416).