glanceclient doesn't cleanup session it creates if one is not provided

Bug #1838694 reported by Alex Schultz
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Glance Client
Fix Released
Undecided
Alex Schultz

Bug Description

If a session object is not provided to the glance client, the HTTPClient defined in glanceclient.common.http will create a session object. This session object leaks open connections because it is not properly closed when the object is no longer needed. This leads to a ResourceWarning about an unclosed socket:

sys:1: ResourceWarning: unclosed <ssl.SSLSocket fd=3, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('192.168.24.2', 55912), raddr=('192.168.24.2', 13292)>

Example code:

$ cat g.py
#!/usr/bin/python3 -Wd
import glanceclient.common.http as h
client = h.get_http_client(endpoint='https://192.168.24.2:13292',
                           token='<my magical token>',
                           cacert='/etc/pki/ca-trust/source/anchors/cm-local-ca.pem',
                           insecure=False)
print(client.get('/v2/images'))

Results in:

$ ./g.py
/usr/lib64/python3.6/importlib/_bootstrap_external.py:426: ImportWarning: Not importing directory /usr/lib/python3.6/site-packages/repoze: missing __init__
  _warnings.warn(msg.format(portions[0]), ImportWarning)
/usr/lib64/python3.6/importlib/_bootstrap_external.py:426: ImportWarning: Not importing directory /usr/lib/python3.6/site-packages/paste: missing __init__
  _warnings.warn(msg.format(portions[0]), ImportWarning)
/usr/lib/python3.6/site-packages/pytz/__init__.py:499: ResourceWarning: unclosed file <_io.TextIOWrapper name='/usr/share/zoneinfo/zone.tab' mode='r' encoding='UTF-8'>
  for l in open(os.path.join(_tzinfo_dir, 'zone.tab'))
/usr/lib/python3.6/site-packages/eventlet/patcher.py:1: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
  import imp
(<Response [200]>, {'images': [{<image data>}], 'first': '/v2/images', 'schema': '/v2/schemas/images'})
sys:1: ResourceWarning: unclosed <ssl.SSLSocket fd=3, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('192.168.24.2', 55912), raddr=('192.168.24.2', 13292)>

This can be mitigated by adding a __del__ function to glanceclient.common.http.HTTPClient that closes the session.

Revision history for this message
Alex Schultz (alex-schultz) wrote :
affects: glance → python-glanceclient
Changed in python-glanceclient:
assignee: nobody → Alex Schultz (alex-schultz)
status: New → In Progress
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to python-glanceclient (master)

Reviewed: https://review.opendev.org/674133
Committed: https://git.openstack.org/cgit/openstack/python-glanceclient/commit/?id=436f797e8db2fc11b7dace7cb7020e4d44a3d2d7
Submitter: Zuul
Branch: master

commit 436f797e8db2fc11b7dace7cb7020e4d44a3d2d7
Author: Alex Schultz <email address hidden>
Date: Thu Aug 1 14:39:46 2019 -0600

    Cleanup session object

    If a session object is not provided to the get_http_client function (as
    is done via osc), the glance client uses it's own HTTPClient class. This
    class creates a session but does not properly close it when it is done.
    This can lead to resource warnings about unclosed sockets. This change
    adds a __del__() to the HTTPClient class to close out the session
    correctly.

    Change-Id: Idccff338fa84c46ca0e429bb533a2a5217205eef
    Closes-Bug: #1838694

Changed in python-glanceclient:
status: In Progress → Fix Released
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix included in openstack/python-glanceclient 3.0.0

This issue was fixed in the openstack/python-glanceclient 3.0.0 release.

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix proposed to python-glanceclient (stable/train)

Fix proposed to branch: stable/train
Review: https://review.opendev.org/712461

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to python-glanceclient (stable/train)

Reviewed: https://review.opendev.org/712461
Committed: https://git.openstack.org/cgit/openstack/python-glanceclient/commit/?id=1aba8f2d248040364e690dd8dc7f3a508baed631
Submitter: Zuul
Branch: stable/train

commit 1aba8f2d248040364e690dd8dc7f3a508baed631
Author: Alex Schultz <email address hidden>
Date: Thu Aug 1 14:39:46 2019 -0600

    Cleanup session object

    If a session object is not provided to the get_http_client function (as
    is done via osc), the glance client uses it's own HTTPClient class. This
    class creates a session but does not properly close it when it is done.
    This can lead to resource warnings about unclosed sockets. This change
    adds a __del__() to the HTTPClient class to close out the session
    correctly.

    Change-Id: Idccff338fa84c46ca0e429bb533a2a5217205eef
    Closes-Bug: #1838694
    (cherry picked from commit 436f797e8db2fc11b7dace7cb7020e4d44a3d2d7)

tags: added: in-stable-train
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.