Repository navigation
Description
Describe the workflow you want to enable
To ensure thread-safety, it's useful to have test infrastructure that will provide:
- A way to identify thread-safety issues in code that has previously mostly been run in process pools. Free-threading's lack of GIL makes thread safety issues more likely than in the past.
- A way to prevent future changes to the code introducing new thread-safety problems.
- 🆕 A way to identify which parts of a data structure are intended to be thread-safe, and which are not.
A good starting point here is focusing on operations that are already parallel, where users might reasonably expect to be able to turn threading on as a joblib backend and have it Just Work. These operations also work in a very particular way: create some tasks, run them in a worker pool, get back the results of the tasks.
What can go wrong in this particular idiom? Switching from a process pool to a thread pool introduces thread-safety issues with the inputs to a Parallel() operation: whereas mutating in a process is fine, mutating in a thread can affect other threads, or future interactions with the object in the main thread.
This suggests a starting point for a invariant to be using in testing: the inputs to a Parallel() operation that can correctly run with both process and thread pools should not change when the function is run.
🆕 This invariant will not be true for objects that are designed to be mutated safely, so there needs to be a way to opt out of the check.
Describe your proposed solution
When pytest is run (no extra flags! 🆕) and Parallel()'s backend a thread pool
- Arguments passed to functions called in parallel are tracked to see if they changed as part of execution.
- "Change" is calculated heuristically, with a hash.
- If hashes change, raise an AssertionError.
🆕 There will be a mechanism to opt-out parameters, or parameter's attributes, from the hashing. In particular, they can be optionally be marked as one of:
thread-safe: indicating these attributes are designed to be safely mutated from multiple threads. This should include a short explanation or pointer to explanation.buggy-thread-unsafe: indicating this is not thread-safe but is still being accessed from multiple threads. This should include a reference to a GitHub issue explaining the problem.
In addition to opt-in out of the hashing test, these will therefore also serve as documentation of thread-safety or lack thereof. They can also suggest utility classes to formalize certain idioms that repeat, e.g. one can imagine a Rust-style lock that wraps an object, rather than having lock and lock-protected-object being two different parameters. Such classes would then also be automatically opted-out.
When pytest is first run with the hashing, it will fail in three ways; initial tests suggest perhaps 20 locations are affected:
- Known thread-unsafe code: For example, BinaryTree. This can be annotated with
ThreadSync, with pointer to an existing issue. - Thread-safe code: E.g. a result objects that is intended to used with a paired lock, an idiom used in at least one place. In this case, this can be annotated and perhaps an issue filed about formalizing the idiom into a reusable class.
- Unknown status: Depending how complex it is to investigate, and the investigation's conclusion, either
thread-safeorbuggy-thread-unsafewith issue filed to investigate it further and determine the final status.
At the end of this relatively quick process the tests should pass across the board, some new issues should be filed for follow up, and a new passing CI job can be added.
Describe alternatives you've considered, if relevant
Thread sanitizers can catch some of these issues, and they should be used to validate scikit-learn thread safety over the long term. However, they are not something a developer can easily run locally: they require recompiling CPython and all relevant extensions (though there is a Docker image, but still). Whereas this approach is just a normal Python build running normal Python tests.
Additional context
Based on #34057, but with more details and addressing some concerns in the discussion there.
#34071 would be needed to have this run broadly.
Differences from PR are marked with 🆕, and include:
- Added use case of identifying which parts of data structure are designed to be mutable from multiple threads.
- Added notions of marking known-thread-safe code and known-buggy code.
- Removed notion of
pytestflag, just do the test automatically. - Removed notion of switching thread pools on automatically.
Activity
parthbhattpy commentedon May 21, 2026
github-actions commentedon May 24, 2026
Thanks for the work you've done so far. The goal of this comment is to set expectations.
Deciding on new features or substantial changes is a lengthy process. It frequently happens that no maintainer is available to take on this task right now.
Please do not create a Pull Request before a decision has been made regarding the proposed work. Making this decision can often take a significant amount of time and effort.
itamarst commentedon May 26, 2026
To clarify the above: the thread-safety annotations would be object level and attribute level, so you'd only have to add them to a few specific classes.
ogrisel commentedon May 27, 2026
This sounds like an interesting proposal. I think we need such tooling to improve thread-safety in scikit-learn and unlock the potential value of free-threading Python.
I think it would be interesting to document the use of a dedicated docker image to test for thread-safety problems in scikit-learn and other projects in the SciPy ecosystem and then post results of this analysis as a scikit-learn issue. And it could also prove useful to assist debugging of thread safety problems reported by the proposed pytest harness or by users. But I agree that tsan is probably too complex and slow to run as part of the regular CI.
We still need either a pytest flag or an environment variable to use the threading backend by default (instead of loky) when running the scikit-learn tests.
ogrisel commentedon May 27, 2026
+1 for developing the proposed work in #34057 to add the support for object/attribute level annotations.
itamarst commentedon May 27, 2026
I've added examples of the parameter and attribute annotations to the pull request.