Skip to content

Set MALLOC_TRIM_THRESHOLD_ before interpreter start #6780

Closed

Description

@jrbourbeau

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.

cc @crusaderky @gjoseph92 @pentschev @jakirkham who all engaged on the previously linked to issue / PRs

Activity

  1. jakirkham commented on Jul 22, 2022

    @jakirkham
    Member

    Just to list some options from the previous issue:

    1. Add a flag to enable/disable setting environment variables before process startup
    2. Include two separate collections of environment variables, one for pre-startup and one for post-startup
    3. ...?
  2. self-assigned this
    on Jul 24, 2022
  3. gjoseph92 commented on Aug 3, 2022

    @gjoseph92
    Collaborator

    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.

    We need the first -1 here to be a sequence of b"NAME=VALUE" environment variables: https://github.com/python/cpython/blob/v3.9.1/Lib/multiprocessing/util.py#L454
    Then we'd copy-paste re-implement popen_spawn_posix.Popen and SpawnProcess, adding the necessary things to pass through the new environment mapping.

    Then, in our get_mp_context, if method == "spawn" we'd return our SpawnProcessWithEnviron instance instead of the default SpawnProcess instance.

    Alternatively, it looks like loky exposes 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#L18

    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.

  4. fjetter commented on Aug 4, 2022

    @fjetter
    Member

    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.

  5. crusaderky commented on Aug 4, 2022

    @crusaderky
    Collaborator

    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.

  6. fjetter commented on Aug 5, 2022

    @fjetter
    Member

    For those following this topic, the recent coiled-runtime benchmarks actually spotted a memory jump when reverting #6681

    see #6833

  7. pentschev commented on Aug 5, 2022

    @pentschev
    Member

    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.

  8. gjoseph92 commented on Aug 5, 2022

    @gjoseph92
    Collaborator

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions