Repository navigation
Make the test suite itself thread-safe to be able to detect thread-safety problems with or without free-threading - #30041
Conversation
|
Note that, I don't plan to continue manually flagging test functions individually. It's too tedious and invasive. |
|
I updated the PR to simplify it and rely on automated thread-unsafety heuristics of the latest version of I started to run Note that tests collection itself is slow, but still bearable compared to the actual test execution time and the memory usage problem. I guess we will need to profile a run on submodule of scikit-learn to check whether the slowdown is expected: when running with For the record, I created the nogil env using conda-forge: |
|
To avoid the memory usage problem, I reduced the parallelism to 2 threads and run 1 iteration of the tests per thread: Here are the result: pytest_all.log There are 146 errors. Many of those are caused by the use of stateful generators in EDIT: the specific problem with generators is now tracked at: Quansight-Labs/pytest-run-parallel#57 EDIT 2: actually, the generator state is not the culprit, but state within objects returned by the generators. Those tests are fundamentally thread-unsafe and need to be updated if we want to use |
| ], | ||
| ) | ||
| @pytest.mark.parametrize("csr_container", CSR_CONTAINERS) | ||
| @pytest.mark.thread_unsafe # manually captured stdout |
There was a problem hiding this comment.
Note: If we want to avoid having to manually add those thread_unsafe annotations, we should use the capsys fixture instead of manually monkey patching sys.stdout and pytest-run-parallel will automatically treat this test as thread-unsafe.
…metrics/tests/test_ranking.py
|
I fixed another batch of failures I keep on discovering with: Note that I use Quansight-Labs/pytest-run-parallel#109 However, there are also scikit-learn specific tests that mutate some inputs, and do not expect to be run several times in a row with mutated inputs. We could annotate those, but it does not really make sense to do so while there is no automated solution for |
|
I discovered a few more failures by running the tests locally and I fixed in my last commit. I ran the same command as you: I also saw a loky |
There was a problem hiding this comment.
A bit tedious, but in an ideal world it would be nice to have a uniform comment for the additional clone? Maybe something like this?
# Avoid side effects from shared instances when running tests in parallelMaybe git blame is enough to be able to go back to this PR and realize this is linked to running the tests in parallel with pytest-run-parallel ...
|
Note that cloning estimators can also be necessary to isolate tests even when not run in parallel with threads. For instance, when the list of parametrized estimator instances is shared across several tests it can cause side effects that cause test failures in https://github.com/scikit-learn/scikit-learn/pull/31543/files#diff-d6ffd0947e0ae3e7cc5dc9ff4eb687c9d53de094d1c6b06ab13d17ab78cc391cR146-R148 . |
Same for me. But I managed to write a reproducer that reproduces the necessary conditions that happens when running many successive parallel tests of our test suite without using pytest in joblib/loky#458. |
|
Also, w.r.t. #30041 (review) : some side effect problem can also happen if you run the same test several times in a row sequentially. So the (thread-)parallel condition is not always necessary to trigger this class of bugs. |
|
I push another fix replacing Before my last commit, I was able to reproduce the failure locally with: |
lesteve
left a comment
There was a problem hiding this comment.
I think this is good to be merged and we can iterate in further PRs if needed.
thomasjpfan
left a comment
There was a problem hiding this comment.
I'm seeing clone(est) in many places so that we work with pytest-run-parallel. Is this always going to be the case for tests that mutates the object?
Yes. But note that when an estimators list is shared between tests, we also need to clone the inputs to avoid side effects across tests. |
|
Thanks for the reviews and merge. We will have to follow-up with actual free-threading testing now. |
…fety problems with or without free-threading (scikit-learn#30041) Co-authored-by: Loïc Estève <[email protected]>
This is an investigative branch, not meant to be merged as such, to investigate what effort would be required to tackle #30007 as a whole (using
pytest-run-parallelinstead ofpytest-freethreaded).It includes commits from:
That can be merged independently.
On top of this, I started to use
@pytest.mark.parallel_threads(1)(EDIT: now@pytest.mark.thread_unsafe) on tests that are fundamentally not thread-safe or that use other fixtures such astmpdirthat would require to be initialized in the thread running the test to function properly (as opposed to running in the mainpytestthread).Many more similar changes to silence all the non-informative failures, but I will stop there for now.
This already highlights that the most common patterns are:
pytest.warns;sys.stdoutwithcapsys;tmpdirfixture;monkeypatchfixture.EDIT: since the first version of this PR,
pytest-run-parallelhas been updated to make thetmpdirandtmp_pathfixture work by default and automatically detect tests that use problematic fixtures and code patterns involving warnings automatically. The remaining problematic fixtures can probably be added by configuration.I have also found a failure in
test_minibatch_kmeans_partial_fit_initwith the lambda init case using:and I cannot explain it yet (using regular Python with GIL enabled).