free-threaded-compatibility
free-threaded-compatibility copied to clipboard
Add discussion of pytest thread safety
Pytest isn't thread safe and it can be confusing if people expect it to be. We should document this.
Also similarly, the warnings module as a whole isn't thread safe. Temporary directories with named files or directories also have thread safety issues since they're using global state via the file system.
Non-exhaustive list of pytest tools that are inherently not thread-safe:
- the
capsysfixture usage (sharedsys.stdout/sys.stderr); - the
monkeypatchfixture (and another monkeypatching tool, e.g. fromunittest.mock); pytest.warns(because thewarningsmodule is not safe by itself).
Usage of the tmpdir and tmp_path fixture is currently not thread-safe. However, they could probably be thread safe if the temporary folder they create for each test would be created and deleted within the same thread as the thread used to run them.