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

ResourceWarning: unclosed socket in pytest #98

Open
g3n35i5 opened this issue Dec 13, 2023 · 3 comments
Open

ResourceWarning: unclosed socket in pytest #98

g3n35i5 opened this issue Dec 13, 2023 · 3 comments

Comments

@g3n35i5
Copy link

g3n35i5 commented Dec 13, 2023

When using api4jenkins with pytest, it may happen that the following exception occurs:

    def unraisable_exception_runtest_hook() -> Generator[None, None, None]:
        with catch_unraisable_exception() as cm:
            yield
            if cm.unraisable:
                if cm.unraisable.err_msg is not None:
                    err_msg = cm.unraisable.err_msg
                else:
                    err_msg = "Exception ignored in"
                msg = f"{err_msg}: {cm.unraisable.object!r}\n\n"
                msg += "".join(
                    traceback.format_exception(
                        cm.unraisable.exc_type,
                        cm.unraisable.exc_value,
                        cm.unraisable.exc_traceback,
                    )
                )
>               warnings.warn(pytest.PytestUnraisableExceptionWarning(msg))
E               pytest.PytestUnraisableExceptionWarning: Exception ignored in: <socket.socket fd=-1, family=10, type=1, proto=6>
E               
E               Traceback (most recent call last):
E                 File "/usr/lib64/python3.11/encodings/idna.py", line 155, in encode
E                   result = input.encode('ascii')
E                            ^^^^^^^^^^^^^^^^^^^^^
E               ResourceWarning: unclosed <socket.socket fd=34, family=10, type=1, proto=6, laddr=('::1', 48440, 0, 0), raddr=('::1', 50001, 0, 0)>

This is because the http_client in the Jenkins class constructor (and probably the async version, too) isn't closed after being used. I think the garbage collector of python isn't working properly in this case because the jenkins object (instance) is being passed into all BaseItem items as reference.

As a workaround, the http client can be closed manually:

jenkins_client = Jenkins(...)

# Do something with the client
client.get_job(...)

# Close the http client
jenkins_client.http_client.close()

but I'd suggest to wrap the http client in an (auto closing) context manager or something like that.

@joelee2012
Copy link
Owner

@g3n35i5 this is similar with psf/requests#3912, you need close Client explicitly in some case. but it will not case your test failed.

@g3n35i5
Copy link
Author

g3n35i5 commented Jan 10, 2024

@joelee2012 you're right, my tests are failing because I run them in warnings as errors mode.
This is indeed similar to the linked issue but I still think that wrapping the socket in a context manager here would be a good idea.

@joelee2012
Copy link
Owner

@g3n35i5 yes it can be a context manager and welcome to create PR 😄

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

No branches or pull requests

2 participants