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

HTTPX instrumentation: ResourceWarning: unclosed socket (httpx.Client, httpx.AsyncClient) #1107

Closed
Tayum opened this issue May 26, 2022 · 4 comments
Labels
bug Something isn't working help wanted Extra attention is needed instrumentation

Comments

@Tayum
Copy link

Tayum commented May 26, 2022

Describe your environment

  • Python 3.9.6
  • httpx==0.18.2
  • opentelemetry-instrumentation-httpx==0.30b1
    • opentelemetry-api==1.11.1
    • opentelemetry-instrumentation==0.30b1
    • opentelemetry-semantic-conventions==0.30b1
    • wrapt==1.14.1
    • Deprecated==1.2.13
  • pytest==7.1.2

Steps to reproduce

# test_instrumentation.py
import httpx
from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor

ATTEMPTS = 10

def test_httpx_instrumentor() -> None:
    HTTPXClientInstrumentor().instrument()

    for _ in range(ATTEMPTS):
        with httpx.Client() as client:
            client.get("https://example.com")

    HTTPXClientInstrumentor().uninstrument()
$ pytest -W error test_instrumentation.py

What is the expected behavior?
Command successfully completes.

What is the actual behavior?

E               pytest.PytestUnraisableExceptionWarning: Exception ignored in: <ssl.SSLSocket fd=-1, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6>
E               
E               Traceback (most recent call last):
E                 File "<...>/venv/lib/python3.9/site-packages/httpcore/_sync/connection.py", line 163, in _open_socket
E                   return self._backend.open_tcp_stream(
E               ResourceWarning: unclosed <ssl.SSLSocket fd=18, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('192.168.0.102', 49992), raddr=('93.184.216.34', 443)>

Additional context

  • Problem is not reproducible with ATTEMPTS = 1, somewhen reproducible with ATTEMPTS = 2..9 and highly reproducible with ATTEMPTS = 9+;
  • Could see the same problem of unclosed socket (though with a bit of different traceback) if httpx.AsyncClient() is used.
@Tayum Tayum added the bug Something isn't working label May 26, 2022
@srikanthccv srikanthccv added help wanted Extra attention is needed instrumentation labels Sep 9, 2022
@Lawouach
Copy link

Lawouach commented Jan 3, 2023

Hi there,

I also notice this issue and never really saw it in the past so I'm wondering if anything could have changed.

@MaxwellPayne
Copy link

Might this have been addressed by #1695?

@srikanthccv
Copy link
Member

I believe so, @Tayum, can you please bump the version and verify?

@Tayum
Copy link
Author

Tayum commented Apr 22, 2023

Hey @srikanthccv,

With the environment of:

  • Python 3.9.14
  • opentelemetry-instrumentation-httpx==0.38b0

I can confirm that:
$ pytest -W error test_instrumentation.py

Command successfully completes 🎉


Although please note, that with

  • Python 3.10.9
  • opentelemetry-instrumentation-httpx==0.38b0

The command:
$ pytest -W error test_instrumentation.py

Gives:

test_instrumentation.py:3: in <module>
    from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor
../venvs/sample/lib/python3.10/site-packages/opentelemetry/instrumentation/httpx/__init__.py:172: in <module>
    from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
../venvs/sample/lib/python3.10/site-packages/opentelemetry/instrumentation/instrumentor.py:24: in <module>
    from opentelemetry.instrumentation.dependencies import (
../venvs/sample/lib/python3.10/site-packages/opentelemetry/instrumentation/dependencies.py:4: in <module>
    from pkg_resources import (
../venvs/sample/lib/python3.10/site-packages/pkg_resources/__init__.py:121: in <module>
    warnings.warn("pkg_resources is deprecated as an API", DeprecationWarning)
E   DeprecationWarning: pkg_resources is deprecated as an API

With:
$ pytest -W error -W ignore::DeprecationWarning:pkg_resources test_instrumentation.py

though command completes successfully as well.

So I can confirm original problem of the issue is solved. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed instrumentation
Projects
None yet
Development

No branches or pull requests

4 participants