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

PytestUnraisableExceptionWarning from asycio client #2692

Closed
ahgraber opened this issue Apr 4, 2023 · 1 comment · Fixed by #2731
Closed

PytestUnraisableExceptionWarning from asycio client #2692

ahgraber opened this issue Apr 4, 2023 · 1 comment · Fixed by #2731

Comments

@ahgraber
Copy link

ahgraber commented Apr 4, 2023

Version: What redis-py and what redis version is the issue happening on?

redis-py 4.3.4, 4.5.3, 4.5.4
redis 7.0.9

Platform: What platform / version? (For example Python 3.5.1 on Windows 7 / Ubuntu 15.10 / Azure)

Python 3.9.16 | packaged by conda-forge
[Clang 14.0.6 ] on darwin 

Occurs in both arm64 and Rosetta amd64 environments.

Description: Description of your issue, stack traces from errors and code that reproduces the issue

When running unit tests on a redis.asyncio.Redis object, I notice I keep getting PytestUnraisableExceptionWarning that is raised because of Redis.del(), as the self.connection attribute was not set due to early failure in the Redis() constructor.

  /Users/me/mambaforge/envs/x86_ds_utils/lib/python3.9/site-packages/_pytest/unraisableexception.py:78: PytestUnraisableExceptionWarning: Exception ignored in: <function Redis.__del__ at 0x17c0919d0>
  
  Traceback (most recent call last):
    File "/Users/me/mambaforge/envs/x86_ds_utils/lib/python3.9/site-packages/redis/asyncio/client.py", line 458, in __del__
      if self.connection is not None:
  AttributeError: 'RedisCache' object has no attribute 'connection'
  
    warnings.warn(pytest.PytestUnraisableExceptionWarning(msg))

This is the same issue as #2660; the fix #2669 did not resolve this particular AttributeError.
The issue is in Redis.__del__() around line 458, where self.connection is not initialized.

@ahgraber
Copy link
Author

ahgraber commented Apr 5, 2023

I have solved this locally by changing the Redis.__del__() method to be:

class Redis:
    # ...
    def __del__(self, _warnings: Any = warnings) -> None:
        if hasattr(self, "connection") and (self.connection is not None):
            _warnings.warn(
                f"Unclosed client session {self!r}", ResourceWarning, source=self
            )
            context = {"client": self, "message": self._DEL_MESSAGE}
            asyncio.get_running_loop().call_exception_handler(context)

I have not opened a PR because I am uncertain if this will cause unintended downstream consequences.

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

Successfully merging a pull request may close this issue.

1 participant