Skip to content

Cloud metadata collection is leaking sockets #1133

Closed
@psontag

Description

@psontag

When collecting the meta data information from cloud providers a socket connection is opened but not closed. This causes ResourceWarnings when running pytest. The issue seems to be the socket that is opened here.

To Reproduce
Create a file test.py with the following content:

import elasticapm

def test():
    c = elasticapm.Client({"SERVICE_NAME": "dev"})
    c.build_metadata()
    assert False

Run the tests with:

pytest --pythonwarnings once test.py

You should see warnings similar to this one (you have to run this on a cloud hosted VM):

test.py::test
  /tmp/venv/lib/python3.6/site-packages/elasticapm/utils/cloud.py:48: ResourceWarning: unclosed <socket.socket fd=11, family=AddressFamily.AF_INET, type=2049, proto=6, laddr=('10.5.128.4', 52428), raddr=('169.254.169.254', 80)>
    socket.create_connection(("169.254.169.254", 80), 0.1)

test.py::test
  /tmp/venv/lib/python3.6/site-packages/elasticapm/utils/cloud.py:48: ResourceWarning: unclosed <socket.socket fd=12, family=AddressFamily.AF_INET, type=2049, proto=6, laddr=('10.5.128.4', 52430), raddr=('169.254.169.254', 80)>
    socket.create_connection(("169.254.169.254", 80), 0.1)

test.py::test
  /tmp/venv/lib/python3.6/site-packages/elasticapm/base.py:393: ResourceWarning: unclosed <socket.socket fd=11, family=AddressFamily.AF_INET, type=2049, proto=6, laddr=('10.5.128.4', 52432), raddr=('169.254.169.254', 80)>
    data = cloud.aws_metadata()

test.py::test
  /tmp/venv/lib/python3.6/site-packages/elasticapm/base.py:393: ResourceWarning: unclosed <socket.socket fd=12, family=AddressFamily.AF_INET, type=2049, proto=6, laddr=('10.5.128.4', 52434), raddr=('169.254.169.254', 80)>
    data = cloud.aws_metadata()

test.py::test
  /tmp/venv/lib/python3.6/site-packages/elasticapm/utils/cloud.py:138: ResourceWarning: unclosed <socket.socket fd=12, family=AddressFamily.AF_INET, type=2049, proto=6, laddr=('10.5.128.4', 52436), raddr=('169.254.169.254', 80)>
    socket.create_connection(("169.254.169.254", 80), 0.1)

test.py::test
  /tmp/venv/lib/python3.6/site-packages/elasticapm/utils/cloud.py:138: ResourceWarning: unclosed <socket.socket fd=11, family=AddressFamily.AF_INET, type=2049, proto=6, laddr=('10.5.128.4', 52438), raddr=('169.254.169.254', 80)>
    socket.create_connection(("169.254.169.254", 80), 0.1)

Environment (please complete the following information)

  • OS: Mac and Linux
  • Python version: 3.6.6
  • APM Server version:
  • Agent version: 5.10.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions