-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
LogCaptureHandler
can be garbage collected when caplog
fixture is in use
#9236
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
Comments
It is impressive that you managed to create a MWE for this issue.! I've just tested your MWE and I can reproduce your reported issue 👍🏿. I also had a quick look at the code and did some test, however I don't yet know where the problem is. What I know so far is that when |
So: |
It's me again. In the |
@leamingrad Why you do need a custom Line 693 in d6e6b96
def test():
assert False test is executed, the code after the yield statement in the Line 707 in d6e6b96
Maybe @nicoddemus @bluetech or @RonnyPfannschmidt can help me explain why this can happen. |
Hmmm as with any context manager, that |
@nicoddemus I should have mentioned this before, but @leamingrad what is the reason for you custom |
@Thisch Thanks for the investigation - let me know if there is any more info I can give to help. The implementation of from django.db import utils
class CustomException(Exception):
pass
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_call(item):
try:
outcome = yield
outcome.get_result()
except utils.ProgrammingError as e:
raise CustomException("Try running again with --create-db") from e |
I just had a look again at your issue. If you fix your issue by adding a |
The problem was that your custom |
@Thisch: Thanks for looking at this again. If I make the change to the MWE, I can confirm that things start to pass. However, I don't quite understand the reasoning above: why should my Looking at the docs, it seems like I've probably just misunderstood something though - it's not immediately obvious to me where the responsibility lies. |
Because otherwise there is currently a non graceful teardown of at least one hook. This should probably be fixed on pytest's side, but I'm no expert on this. |
Ah cool that makes sense - so this is a bug in pytest (I guess the logging hook needs to handle possible exceptions as suggested above), but we can work around it on our side if needed using this method. Thanks! |
For example pytest-dev/pytest#9236 Here it was passing when running this test alone but not when run with the other tests.
In our tests we have had issues with the
caplog
fixture not correctly saving every log intocaplog.messages
andcaplog.records
. From what I can tell, the underlying issue is that in some circumstances theLogCaptureHandler
s thatcaplog
relies on can get garbage collected.There seem to be a lot of issues in this area (#5160, #8977, #7335 etc.), but I'm not sure if any of them are the specific case that I have experienced.
Some debugging has got me to the stage where I have a MWE that reproduces the problem:
test.py
:conftest.py
:If you run
pytest test.py
, then it fails with the following:However, if you remove the
gc.collect()
call from the test, it passes:pip list
from the virtual environment you are using❯ pip list Package Version --------------------------------- --------- appdirs 1.4.4 attrs 21.2.0 backports.entry-points-selectable 1.1.0 CacheControl 0.12.6 cachy 0.3.0 certifi 2021.10.8 charset-normalizer 2.0.7 cleo 0.8.1 clikit 0.6.2 crashtest 0.3.1 distlib 0.3.3 filelock 3.0.12 html5lib 1.1 idna 3.2 iniconfig 1.1.1 keyring 21.8.0 lockfile 0.12.2 msgpack 1.0.2 packaging 20.9 pastel 0.2.1 pbr 5.4.5 pexpect 4.8.0 pip 19.2.3 pkginfo 1.7.1 platformdirs 2.4.0 pluggy 1.0.0 poetry 1.1.11 poetry-core 1.0.7 ptyprocess 0.7.0 py 1.10.0 pylev 1.4.0 pyparsing 2.4.7 pytest 6.2.5 requests 2.26.0 requests-toolbelt 0.9.1 setuptools 41.2.0 shellingham 1.4.0 six 1.15.0 stevedore 2.0.0 toml 0.10.2 tomlkit 0.7.2 urllib3 1.26.7 virtualenv 20.8.1 virtualenv-clone 0.5.4 virtualenvwrapper 4.8.4 webencodings 0.5.1 WARNING: You are using pip version 19.2.3, however version 21.3.1 is available. You should consider upgrading via the 'pip install --upgrade pip' command.
pytest 6.2.5
and MacOS BigSur 11.6.The text was updated successfully, but these errors were encountered: