Skip to content

pytest thread safety should be better documented #12356

Description

@ngoldbaum
  • a detailed description of the bug or problem you are having
  • output of pip list from the virtual environment you are using
  • pytest and operating system versions
  • minimal example if possible

pytest.warns is a subclass of the standard library warnings.catch_warnings context manager, which is not thread safe (this is noted in the docs).

If it's not possible for pytest to have a thread-safe version, it would be nice if the docs for pytest.warns could note that it's implemented using warnings.catch_warnings and is not thread safe for the same reason.

pytest 8.2.1 on MacOS Sonoma.

Fails about 10-20% of the time on my machine:

import warnings
import threading


def raise_warning():
    warnings.warn(RuntimeWarning())


def test_pytest_warns():
    b = threading.Barrier(2)

    def catch_warning():
        b.wait()
        with pytest.warns(RuntimeWarning):
            raise_warning()

    task1 = threading.Thread(target=catch_warning)
    task2 = threading.Thread(target=catch_warning)

    task1.start()
    task2.start()
    task1.join()
    task2.join()

Activity

  1. The-Compiler commented on May 23, 2024

    @The-Compiler
    Member

    pytest in its entirety is not intended to be thread-safe IIRC.

  2. RonnyPfannschmidt commented on May 23, 2024

    @RonnyPfannschmidt
    Member

    pytest.warns is implemented in terms of the stdlib catch-warnings helper - which is inherently not threadsave

  3. ngoldbaum commented on May 23, 2024

    @ngoldbaum
    ContributorAuthor

    pytest in its entirety is not intended to be thread-safe IIRC.

    TIL

    Best to avoid using pytest constructs inside a thread, then.

    This fact could be emphasized more in the docs, I don't see any mention of threads at all besides the discussion about unhandled exceptions.

  4. ngoldbaum commented on May 23, 2024

    @ngoldbaum
    ContributorAuthor

    pytest.warns is implemented in terms of the stdlib catch-warnings helper - which is inherently not threadsave

    Yes, I noted that in the issue description. The issue is a request for that fact to be noted in the docs, like the docs for the stdlib helper, if making it thread safe is out of scope for the library.

  5. nicoddemus commented on May 23, 2024

    @nicoddemus
    Member

    Makes sense to note somewhere that pytest's constructs/helpers are not thread safe.

    If somebody wants to take a look and update the docs, would love to review a PR. 👍

  6. changed the title [-]`pytest.warns` is not thread safe[/-] [+]pytest thread safety should be better documented[/+] on May 23, 2024
  7. added a commit that references this issue on May 24, 2024
    889d9b2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions