free-threaded-compatibility icon indicating copy to clipboard operation
free-threaded-compatibility copied to clipboard

Add discussion of pytest thread safety

Open ngoldbaum opened this issue 1 year ago • 1 comments

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.

ngoldbaum avatar Oct 10 '24 13:10 ngoldbaum

Non-exhaustive list of pytest tools that are inherently not thread-safe:

  • the capsys fixture usage (shared sys.stdout/sys.stderr);
  • the monkeypatch fixture (and another monkeypatching tool, e.g. from unittest.mock);
  • pytest.warns (because the warnings module 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.

ogrisel avatar Oct 10 '24 13:10 ogrisel