-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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.socket when I run a unittest? #3912
Comments
This warning happens because Requests uses a keep-alive model that means we try not to explicitly close sockets in many cases. Exactly why this is happening is likely due to you either using a Session object that you don't explicitly close at the end of your test, or using an older version of Requests. |
@Lukasa |
It's hard to know. Depends if they change the warning filter, depends if anything else changes the warnings filter, depends on how your code is written. |
Does it mean I must call |
You don't have to. The warning is just that: a warning. No error occurs when you see it, and it is not an indication of the program doing the wrong thing. It's entirely expected behaviour, and this is working as designed. However, if you're concerned about them, you may call |
…g: unclosed socket.socket') by following the solution discussed on psf/requests#3912
I find this advice contrary to Python's typical approach to resource handling. Python encourages deterministic resource handling. For example, the stdlib produces warnings when files are mismanaged in a non-deterministic way to help guide developers. Developers should take action on the warnings produced by stdlib. See the following Python bug where core developers discuss warnings during destructors: https://bugs.python.org/issue28867 Additionally, Python docs warn against relying on
And, exceptions occurring during
I run all my tests and environments with warnings enabled to help catch real bugs and upgrade paths. If this warning really should be ignored as suggested here, then maybe it shouldn't be logged. As it is now, people are taking this advice of not calling close and this results in lots of (ignorable?) noise in my output. |
Hi! Could you provide an example of how to solve this issue? |
@jdufresne I don't believe Requests is actually logging this at all. That's the interpreter logging it. Also, I'm rather confident that Requests doesn't rely on
@Garito, this is not StackOverflow - the designated Q&A resource for Requests - so please post your question there with code snippets. As a hint, this is likely something to do with your code and not the ResourceWarning. An unclosed socket should not cause your tests to hang so there's likely something else going on with your code. Given the shear amount of off-topic discussion on this issue, I'm locking it. |
requests show me following warning when I run a unittest:
But this warning won't happen if I don't run the code in a unittest environment.
Is there somebody who can tell me why this warning happen?
The text was updated successfully, but these errors were encountered: