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
Explicit documentation / contract for free-threading #34596
This issue is not yet ready for a PR. If you are interested in contributing to scikit-learn, please have a look at our contributing guidelines, and in particular the sections for new contributors and the "Needs triage" label.
Describe the issue linked to the documentation
Scikit-learn offers builds for free-threaded Python, but it is not clear as a user what is supposed to be safe to call from multiple threads.
For example, it feels quite obvious that one should not call .fit() on the same object from multiple threads even if not documented, but does the interface guarantee that other methods are safe to call on the same object from multiple threads? For instance, KNN depending on the arguments might do most of the work in other methods, and I'm not sure if it doesn't modify any internal attributes starting with underscores.
Further, since lots of other Python libraries offer scikit-learn-compatible objects, it would be quite useful to have official documentation from scikit-learn about what needs or doesn't need to be thread-safe in order to develop scikit-learn-compatible classes.
Suggest a potential alternative/fix
Add a documentation page for free threading covering all these kinds of details, plus additional documentation on a per-estimator basis if needed for exceptions. Mention these thread safety requirements also in the developer guide.
calling fit concurrently on the same estimator instance is not guaranteed to be thread-safe;
"inference" methods such as predict, predict_proba, decision_function, transform, score... should not modify internal attributes and should all be thread-safe. I think we considered violations to this implicit rule as bugs in the past.
However, I don't think this is explicitly written anywhere nor systematically tested in a dedicated estimator check run as part of the common tests.
Maybe we could maybe write a common check such as:
fit an estimator instance on suitable data;
call joblib.hash on the estimator and store the result;
call predict/predict_proba/decision_function/transform/score when applicable;
call joblib.hash again to check that the internal state of the estimator wasn't mutated.
This is not enough to guarantee thread-safety of those methods but it's cheaper to run and easier to debug than a thread-safety stress test. I think we could open a draft PR to implement such a common test and analyze the results. If there are no (or very few) detected violations, we could document the above policy to make it official. If there are many detected violations, we will have to decide what to do before proceeding further.
Note: there is a very related draft PR #34057 that proposes a similar strategy for testing thread-safety/mutations but that PR has a focus on the thread-safety analysis of function that are called in parallel via joblib.Parallel within the scikit-learn code base which is not exactly the same objective.
On the documentation side, I think this is a bit early, as we are pretty much figuring this as we make progress on free-threaded support in scikit-learn and joblib, loky, etc ...
As inspiration, I would be curious if you find other projects that have already documented some kind of contracts for free-threaded, maybe numpy?
For scikit-learn, I think a small first step could be a scoped docs page rather than a full per-estimator audit: document that concurrent calls to mutating methods such as fit / partial_fit on the same estimator instance are not guaranteed to be thread-safe, while inference methods such as predict, transform, decision_function, predict_proba, and score are expected to be side-effect free and safe to call concurrently on a fitted estimator, unless an estimator-specific exception is documented.
I can open a draft PR with that limited wording plus a short developer-guide note if this scope sounds reasonable.
Activity
Tharunbilluri commentedon Jul 29, 2026
ogrisel commentedon Aug 6, 2026
The general policy should be:
fitconcurrently on the same estimator instance is not guaranteed to be thread-safe;predict,predict_proba,decision_function,transform,score... should not modify internal attributes and should all be thread-safe. I think we considered violations to this implicit rule as bugs in the past.However, I don't think this is explicitly written anywhere nor systematically tested in a dedicated estimator check run as part of the common tests.
Maybe we could maybe write a common check such as:
joblib.hashon the estimator and store the result;predict/predict_proba/decision_function/transform/scorewhen applicable;joblib.hashagain to check that the internal state of the estimator wasn't mutated.This is not enough to guarantee thread-safety of those methods but it's cheaper to run and easier to debug than a thread-safety stress test. I think we could open a draft PR to implement such a common test and analyze the results. If there are no (or very few) detected violations, we could document the above policy to make it official. If there are many detected violations, we will have to decide what to do before proceeding further.
Note: there is a very related draft PR #34057 that proposes a similar strategy for testing thread-safety/mutations but that PR has a focus on the thread-safety analysis of function that are called in parallel via
joblib.Parallelwithin the scikit-learn code base which is not exactly the same objective.lesteve commentedon Aug 7, 2026
On the documentation side, I think this is a bit early, as we are pretty much figuring this as we make progress on free-threaded support in scikit-learn and joblib, loky, etc ...
As inspiration, I would be curious if you find other projects that have already documented some kind of contracts for free-threaded, maybe numpy?
david-cortes-intel commentedon Aug 7, 2026
NumPy has a docpage: https://numpy.org/devdocs/reference/thread_safety.html
Not much info on it, but they do have docs.
Tharunbilluri commentedon Aug 10, 2026
Thanks, the NumPy page is a helpful reference.
For scikit-learn, I think a small first step could be a scoped docs page rather than a full per-estimator audit: document that concurrent calls to mutating methods such as
fit/partial_fiton the same estimator instance are not guaranteed to be thread-safe, while inference methods such aspredict,transform,decision_function,predict_proba, andscoreare expected to be side-effect free and safe to call concurrently on a fitted estimator, unless an estimator-specific exception is documented.I can open a draft PR with that limited wording plus a short developer-guide note if this scope sounds reasonable.
ogrisel commentedon Aug 10, 2026
I would rather conduct the audit before stating anything in the doc.
itamarst commentedon Aug 11, 2026
I will implement that draft PR.
itamarst commentedon Aug 11, 2026
I've implemented a first pass at #34729 - the failures I'm seeing locally are:
itamarst commentedon Aug 11, 2026
Almost done fixing those, so doesn't look like extensive problems with this invariant.
itamarst commentedon Aug 11, 2026
OK PR is done, waiting for tests to pass and then will put up for review.
itamarst commentedon Sep 7, 2026
Forgot to link it; the PR is here: #34729