Skip to content

FIX Read LOKY_MAX_MEMORY_LEAK_SIZE at each memory check - #688

Open
megacodes24 wants to merge 2 commits into
joblib:masterfrom
megacodes24:memory-leak-size-runtime
Open

megacodes24 wants to merge 2 commits into
joblib:masterfrom
megacodes24:memory-leak-size-runtime

Conversation

@megacodes24

Copy link
Copy Markdown

Follow-up of joblib/joblib#1915. As discussed there, the fix should go to loky first.

LOKY_MAX_MEMORY_LEAK_SIZE was read once when loky.process_executor was imported, so a worker kept the value it started with, even when the variable was changed later inside the worker. As suggested by @stepan-srsen in the issue, the worker now reads it on the fly: _get_max_memory_leak_size() reads the environment variable each time a worker checks its memory usage (at most once per _MEMORY_LEAK_CHECK_DELAY). _MAX_MEMORY_LEAK_SIZE is kept as the default value.

Changes:

  • loky/process_executor.py: add _get_max_memory_leak_size() and use it in the worker memory check and in the recycling warning.
  • tests/test_reusable_executor.py: new test where a first task raises the threshold in the worker's environment, then later tasks leak more than 300 MB, and the worker should not be recycled. It fails before the change and passes after.
  • docs/API.rst and CHANGES.md: updated.
  • pre-commit's trailing-whitespace hook also removed trailing spaces from a few nearby lines in docs/API.rst.

Limitation: each worker has its own copy of the environment, so changing the variable in the parent process after the workers have started still does not reach them (I tested this in joblib/joblib#1915). Passing it with env= in get_reusable_executor keeps working, and the existing test_max_memory_leak_size_env_var still passes.

Tested on Windows, Python 3.12: tests/test_reusable_executor.py and tests/test_process_executor_loky.py pass (131 passed, 7 skipped). pre-commit passes.

@megacodes24

Copy link
Copy Markdown
Author

About the codecov/patch failure: I checked locally and the worker-side lines aren’t measured in CI. continuous_integration/runtests.sh installs coverage_subprocess.pth, but COVERAGE_PROCESS_START is never set, so coverage.process_startup() does nothing in the worker processes.

Running test_max_memory_leak_size_env_var_changed_in_worker with --cov=loky:

as in CI: “No data to combine”, and the whole worker loop (476-583) is reported as missing, including the new lines;
with COVERAGE_PROCESS_START=.coveragerc: the worker data is recorded, process_executor.py goes from 52% to 62%, and the changed lines 553/556 and _get_max_memory_leak_size are covered. Line 565 is only reached when the usage is above the threshold, which this test avoids on purpose.

Would a separate small PR setting COVERAGE_PROCESS_START in runtests.sh be welcome? It should make the coverage of worker-side code visible in general.

This branch has not been deployed

No deployments
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.

1 participant