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

Add cleanup to be called when future is garbage collected #493

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ name: build
on: push
jobs:
tox:
runs-on: ubuntu-18.04
runs-on: ubuntu-22.04
strategy:
fail-fast: false
matrix:
python:
- py27
- py36
- py37
httplib:
- default
Expand All @@ -24,7 +22,7 @@ jobs:
- run: tox -e ${{ matrix.python }}-${{ matrix.httplib }}

misc:
runs-on: ubuntu-18.04
runs-on: ubuntu-22.04
strategy:
fail-fast: false
matrix:
Expand Down
9 changes: 9 additions & 0 deletions bravado/http_future.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ def _raise_connection_error(self, exception):
# type: (BaseException) -> typing.NoReturn
self._raise_error(BravadoConnectionError, 'ConnectionError', exception)

def cleanup(self):
# type: () -> None
"""Perform any cleanup necessary to avoid resource leaks."""
pass

def result(self, timeout=None):
# type: (typing.Optional[float]) -> T
"""
Expand Down Expand Up @@ -285,6 +290,10 @@ def cancel(self):
# type: () -> None
return self.future.cancel()

def __del__(self):
# type: () -> None
self.future.cleanup()

@reraise_errors
def _get_incoming_response(self, timeout=None):
# type: (typing.Optional[float]) -> IncomingResponse
Expand Down
2 changes: 0 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
'Topic :: Software Development :: Libraries :: Python Modules',
'License :: OSI Approved :: BSD License',
'Operating System :: OS Independent',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
],
package_data={
Expand Down
10 changes: 10 additions & 0 deletions tests/http_future/HttpFuture/cleanup_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# -*- coding: utf-8 -*-
import mock

from bravado.http_future import HttpFuture


def test_cleanup_on_gc(mock_future_adapter):
http_future = HttpFuture(future=mock_future_adapter, response_adapter=mock.Mock()) # type: HttpFuture[None]
del http_future
assert mock_future_adapter.cleanup.call_count == 1
4 changes: 3 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tox]
envlist = {py27,py36,py37}-{default,fido}, {py27,py36,py37}-fido-requests2dot17, mypy, pre-commit
envlist = {py37}-{default,fido}, {py37}-fido-requests2dot17, mypy, pre-commit

[testenv]
deps =
Expand Down Expand Up @@ -37,6 +37,8 @@ deps =
Twisted<21
# since we run in py 2.7 compatible mode, swagger-spec-validator>=3.0.0 causes syntax errors
swagger-spec-validator<3.0.0
# so does yelp-bytes > 0.4.0
yelp-bytes==0.4.0
-rrequirements-dev.txt
.[fido]
mypy==0.790
Expand Down
Loading