You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Set MALLOC_TRIM_THRESHOLD_ before interpreter start #6780
In #6777 we reverted #6681 in order to avoid #6749 (which broke dask-cuda). This revert is meant to be temporary with the intent of restoring the functionality of #6681, possibly addressing some of the implementation comments in #6749. Opening up an issue so we don't loose track of getting #6681 functionality back into main.
Creating a subprocess always involves, at some level, a fork(2) followed by an execve(2) (whether using spawn, fork, or forkserver in Python). So ideally, we'd just be passing the new environment variables in the execve (or setting them after the fork and inheriting them in the exec). Then we wouldn't be affecting the parent process's environment at all.
The only reason we don't do this is because while the subprocess module offers the option to pass env= to Popen, the multiprocessing module doesn't expose this option in the Process class.
There's no underlying technical reason why we can't do this properly, it's just that multiprocessing doesn't give the option, so the only thing we can do to ensure the variables are set prior to execve(2) is to also set them prior to fork(2), thereby polluting the parent process.
I've looked through the multiprocessing code a little and it would be possible to expose this if we're willing to poke somewhat deep into internals.
So loky could be an optional (but recommended) dependency, and if installed, we'd use multiprocessing.get_context("loky").Process(..., env=new_env) inside our AsyncProcess. If it's not installed (or you're not using the multiprocessing-method: "loky"), we'd probably just warn that env vars weren't being set and move on.
Re-implementing multiprocessing things, or using a third-party library, is the way to get the most correct behavior, but it's also obviously more work and more of a maintenance burden.
If we don't want to go down this path, @crusaderky's suggestion of separate env and pre-spawn-env dicts in the config seems like the most pragmatic approach.
I appreciate the thoroughness but I'm against vendoring multiprocessing code for the sole purpose of setting environment variables as long as there are simpler ways to meet our requirements.
Maintaining such a fork comes at significant cost as we can see with our Threadpool which is proving to be difficult to keep up to date with recent python versions.
Right now, it's about three environment variables and extending our configuration space to two dictionaries is a much better fit for our problem than forking multiprocessing.
if we're willing to poke somewhat deep into internals.
I am extremely unwilling to poke in the Python internals.
An upstream PR to CPython would be a good contribution, however I don't think that the effort is justified just for our use case. Also we need this feature now and it would not be able to rely on it, at the earliest, before we require python >= 3.12, several years from now.
FWIW, I understand @gjoseph92 's point, and that would be a nice solution. However, I also understand that this a pressing issue for some. The only problem we really do have now is variables leaking, given it seems possible to avoid that with the proposal from @crusaderky and @fjetter we should be good to go with that, and are happy to test it as soon as there's a draft/PR for it.
Activity
jakirkham commentedon Jul 22, 2022
Just to list some options from the previous issue:
gjoseph92 commentedon Aug 3, 2022
Creating a subprocess always involves, at some level, a
fork(2)followed by anexecve(2)(whether usingspawn,fork, orforkserverin Python). So ideally, we'd just be passing the new environment variables in theexecve(or setting them after theforkand inheriting them in theexec). Then we wouldn't be affecting the parent process's environment at all.The only reason we don't do this is because while the
subprocessmodule offers the option to passenv=toPopen, themultiprocessingmodule doesn't expose this option in theProcessclass.There's no underlying technical reason why we can't do this properly, it's just that multiprocessing doesn't give the option, so the only thing we can do to ensure the variables are set prior to
execve(2)is to also set them prior tofork(2), thereby polluting the parent process.I've looked through the multiprocessing code a little and it would be possible to expose this if we're willing to poke somewhat deep into internals.
We need the first
-1here to be a sequence ofb"NAME=VALUE"environment variables: https://github.com/python/cpython/blob/v3.9.1/Lib/multiprocessing/util.py#L454Then we'd copy-paste re-implement
popen_spawn_posix.PopenandSpawnProcess, adding the necessary things to pass through the new environment mapping.Then, in our
get_mp_context,if method == "spawn"we'd return ourSpawnProcessWithEnvironinstance instead of the defaultSpawnProcessinstance.Alternatively, it looks like
lokyexposes passing environment variables to spawned subprocesses (basically by doing their own copy-paste of the multiprocessing module): https://github.com/joblib/loky/blob/285fb74e24cbe3242c459f8c841bccd24926dcca/loky/backend/process.py#L18So loky could be an optional (but recommended) dependency, and if installed, we'd use
multiprocessing.get_context("loky").Process(..., env=new_env)inside ourAsyncProcess. If it's not installed (or you're not using themultiprocessing-method: "loky"), we'd probably just warn that env vars weren't being set and move on.Re-implementing multiprocessing things, or using a third-party library, is the way to get the most correct behavior, but it's also obviously more work and more of a maintenance burden.
If we don't want to go down this path, @crusaderky's suggestion of separate
envandpre-spawn-envdicts in the config seems like the most pragmatic approach.fjetter commentedon Aug 4, 2022
I appreciate the thoroughness but I'm against vendoring multiprocessing code for the sole purpose of setting environment variables as long as there are simpler ways to meet our requirements.
Maintaining such a fork comes at significant cost as we can see with our Threadpool which is proving to be difficult to keep up to date with recent python versions.
Right now, it's about three environment variables and extending our configuration space to two dictionaries is a much better fit for our problem than forking multiprocessing.
crusaderky commentedon Aug 4, 2022
I am extremely unwilling to poke in the Python internals.
An upstream PR to CPython would be a good contribution, however I don't think that the effort is justified just for our use case. Also we need this feature now and it would not be able to rely on it, at the earliest, before we require python >= 3.12, several years from now.
fjetter commentedon Aug 5, 2022
For those following this topic, the recent coiled-runtime benchmarks actually spotted a memory jump when reverting #6681
see #6833
pentschev commentedon Aug 5, 2022
FWIW, I understand @gjoseph92 's point, and that would be a nice solution. However, I also understand that this a pressing issue for some. The only problem we really do have now is variables leaking, given it seems possible to avoid that with the proposal from @crusaderky and @fjetter we should be good to go with that, and are happy to test it as soon as there's a draft/PR for it.
gjoseph92 commentedon Aug 5, 2022
Yeah, though I'd love to see a proper fork/exec solution, I don't expect to right now. We can get something pragmatic in much quicker.