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

Fix ResourceWarning unclosed socket and re-enable and fix tornado tests #801

Merged
merged 28 commits into from Jan 23, 2024

Conversation

graingert
Copy link
Collaborator

No description provided.

Mohammad Razavi and others added 9 commits August 7, 2023 08:42
This PR fixes issue kevin1024#710 by properly closing the underlying socket. It
first uses `pool._put_conn` to keep the connection in the pool, and
later removes and closes it when the context manager exits.

I was unsure about the exact purpose of the `ConnectonRemove` class,
so I made minimal changes to minimize the risk of breaking the code
and there may be better solutions for fixing this issue.

For example, the `urllib3.connectionpool.HTTPConnectionPool` will
utilize a weakref to terminate pool connections. By appending our
connection to it, it will also take care of closing our connection. So
another solution could be to modify the `__exit__` in
`patch.ConnectionRemover` method and add our connection to the pool:

```py
class ConnectionRemover:
    ...

    def __exit__(self, *args):
        for pool, connections in self._connection_pool_to_connections.items():
            for connection in connections:
                if isinstance(connection, self._connection_class):
                    pool._put_conn(connection)
```
@codecov-commenter
Copy link

codecov-commenter commented Dec 15, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (713cb36) 90.13% compared to head (8e13af2) 89.63%.
Report is 1 commits behind head on master.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #801      +/-   ##
==========================================
- Coverage   90.13%   89.63%   -0.51%     
==========================================
  Files          27       27              
  Lines        1815     1813       -2     
  Branches      336      282      -54     
==========================================
- Hits         1636     1625      -11     
- Misses        134      142       +8     
- Partials       45       46       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -260,6 +259,12 @@ def test_aiohttp_test_client_json(aiohttp_client, tmpdir):
assert cassette.play_count == 1


def test_cleanup_from_pytest_asyncio():
# work around https://github.com/pytest-dev/pytest-asyncio/issues/724
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@graingert graingert marked this pull request as ready for review December 15, 2023 19:18
pyproject.toml Outdated Show resolved Hide resolved
@jairhenrique
Copy link
Collaborator

@graingert please, rebase your branch with master.

@graingert graingert changed the title Fix ResourceWarning unclosed socket Fix ResourceWarning unclosed socket and re-enable and fix tornado tests Jan 23, 2024
@graingert
Copy link
Collaborator Author

@jairhenrique thanks! Turns out the tornado tests were not running, but thanks to filterwarnings=error I have discovered this and fixed it.

Comment on lines +16 to +19
'''ignore:There is no current event loop:DeprecationWarning''',
'''ignore:make_current is deprecated; start the event loop first:DeprecationWarning''',
'''ignore:clear_current is deprecated:DeprecationWarning''',
'''ignore:the \(type, exc, tb\) signature of throw\(\) is deprecated, use the single-arg signature instead.:DeprecationWarning''',
Copy link
Collaborator Author

@graingert graingert Jan 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll fix these deprecation warnings in a followup PR

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool!

Copy link
Collaborator

@jairhenrique jairhenrique left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@graingert graingert merged commit 8028420 into kevin1024:master Jan 23, 2024
13 checks passed
@graingert graingert deleted the fix-resource-warning-2 branch January 23, 2024 12:39
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 this pull request may close these issues.

None yet

3 participants