Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thread safety when usage with Pytest, and Coverage cause unexpected hang at exit #4052

Open
teruokun opened this issue Jul 18, 2024 · 5 comments

Comments

@teruokun
Copy link

Appears to be introduced in 6.103.4 and existing until the current version (6.108.2) but appears to happen specifically in our network-isolated environments (like a network-jailed build container). When doing deep level logging of the python interpreter, it appears specifically to be blocked during the destruction/cleanup phase, meaning there's either a stray thread or reference not being cleaned up properly when Coverage runs with the pytest hypothesis plugin. My current theory is that the new thread_local() internal cache has mutable structures shared out as references to the thread-local objects themselves because of their mutability instead of handling that thread-local nature entirely within the cache logic. The sharing of that reference means it no longer necessarily abides by thread-safety if a new thread is forked because the stored reference still points at the object (though admittedly thread_local itself has strange enough semantics that perhaps I misunderstand it as well).

I'm attempting to build an actual reproduction of the issue that is easily verifiable, but it may take time to get all of it set up properly and want to get the issue known about in case any others find a similar issue.

@nickittynack
Copy link

nickittynack commented Jul 23, 2024

When running pytest after hypothesis version 6.103.2 the cli will hang after test completion and not exit. When running the same tests on 6.103.2 the cli exits fine. We have had to fix the version of hypothesis, and fight Dependabot to not upgrade it. We found this during our pre-commit steps which run a few pytests.
I believe something in this change is what causes the hang: https://github.com/HypothesisWorks/hypothesis/compare/7629e02701b18d1d083e66361977feb084b239e9..9944b59011606cf3c3e93de2ebd4be3b3b7ad927

@Zac-HD
Copy link
Member

Zac-HD commented Jul 23, 2024

Thanks @nickittynack, corroborating evidence is super useful! If you could extract a minimal reproducer, a bug that we can replay as desired is most of the way to being fixed.

@nickittynack
Copy link

Hey @Zac-HD unfortunately I've been unable to reproduce it in a new project, or a new Django project. I understand that that's not super helpful for you! It is no longer an issue for us as we have removed the package from our projects.

@gurudave
Copy link

I've run into the same issue as well. In our environment, the following works as a minimal reproducer:

  • Create clean venv
  • pip install hypothesis pytest pytest-cov
  • Run python -m pytest --cov test_hang.py (test_hang.py is attached, it's just a single test that always passes)

The above hangs if --cov is supplied, and does not hang if --cov is omitted.

In an attempt to isolate the test from my local environment, I tried running it in a python docker container, and I found that it only seems to manifest under python 3.11.2.

Maybe it's not that big a deal given the specific set of conditions, but probably still worth investigating.

@Zac-HD
Copy link
Member

Zac-HD commented Sep 5, 2024

@gurudave I'm not seeing an attachment, mind including it inline?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants