Skip to content

Don't share regression data across tests and test runs - #34131

Merged
jeremiedbb merged 5 commits into
scikit-learn:mainfrom
itamarst:33470-shared-estimator
May 28, 2026
Merged

jeremiedbb merged 5 commits into
scikit-learn:mainfrom
itamarst:33470-shared-estimator

Conversation

@itamarst

@itamarst itamarst commented May 26, 2026 •

Copy link
Copy Markdown
Contributor

[free-threaded]

For now the goal is to do a series of CI runs and see if the failures recur or if this fixes them; I can't reproduce CI failures locally, unfortunately.

Reference Issues/PRs

Fixes #33470

Or that's the hope.

What does this implement/fix? Explain your changes.

Prevent the same regression data being used by multiple tests (sequentially across tests, or in the case of free-threading CI run, in parallel of the same test), since some tests may mutate it.

(Originally, as referred to in some commits below, this was trying to ensure estimators weren't being shared, but that was a dead end.)

AI usage disclosure

No AI.

@itamarst

Copy link
Copy Markdown
Contributor Author

Theory is clearly wrong, closing until I come up with next theory.

@jeremiedbb

Copy link
Copy Markdown
Member

Thanks for looking into this.

Prevent the same estimator object being used by multiple tests

That has been the main source of failure in the past. However all checks yielded by _parametrize_with_checks do clone the estimator already.

Looking at the failures summary, it only happens for the check_regressors_train check. It uses a dataset that is defined globally (REGRESSION_DATASET) and seems to be used and mutated inplace by several checks. This is clearly not threadsafe. _regression_dataset() should return copies of the global dataset (I believe the performance impact should be minimal, it's a very small one).

But if that's the only source of failure, I wonder why others checks that use this dataset pass. Maybe their assertion don't depend on it being modified. In check_regression_train we check a score, which can be sensitive to the dataset. And we fit and then score, so if another thread modifies the dataset between fit and score, a bad score would kind of be expected.

@itamarst

itamarst commented May 27, 2026 •

Copy link
Copy Markdown
Contributor Author

Trying copying X and y, as an experiment.

Update: First run passed!

Update 2: Second run passed!

@itamarst itamarst changed the title Don't share estimators across tests and test runs Don't share regression data across tests and test runs May 27, 2026
@itamarst

Copy link
Copy Markdown
Contributor Author

@jeremiedbb Maybe... it's working now?

@itamarst
itamarst marked this pull request as ready for review May 27, 2026 19:42

@jeremiedbb jeremiedbb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I restarted the CI a few times and they're all green. Almost 10 runs in a row were green. Previously it used to fail I'd say between a quarter and half of the time. So it looks good now (until we find another thread safety issue that is even harder to trigger :) ). Thanks

@jeremiedbb
jeremiedbb merged commit 39d0ad0 into scikit-learn:main May 28, 2026
113 checks passed
@ogrisel

ogrisel commented May 29, 2026

Copy link
Copy Markdown
Member

Thank you very much @itamarst!

@jeremiedbb jeremiedbb mentioned this pull request Jun 1, 2026
16 tasks done
prady0t pushed a commit to prady0t/scikit-learn that referenced this pull request Sep 2, 2026
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.

⚠️ CI failed on Unit tests Linux x86-64 pylatest_free_threaded (last failure: May 21, 2026) ⚠️

3 participants