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.socket when I run a unittest? #3912

Closed
ghost opened this issue Mar 6, 2017 · 8 comments
Closed

ResourceWarning: unclosed socket.socket when I run a unittest? #3912

ghost opened this issue Mar 6, 2017 · 8 comments

Comments

@ghost
Copy link

ghost commented Mar 6, 2017

requests show me following warning when I run a unittest:

ResourceWarning: unclosed <socket.socket fd=5, family=AddressFamily.AF_INET, 
type=SocketKind.SOCK_STREAM, proto=6, laddr=('219.82.68.148', 36006), raddr=('125.46.22.55', 80)>

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?

@Lukasa
Copy link
Member

Lukasa commented Mar 6, 2017

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 Lukasa closed this as completed Mar 6, 2017
@ghost
Copy link
Author

ghost commented Mar 6, 2017

@Lukasa
Thanks for answering. But why this warning won't happen if I I don't run the code in a unittest environment?

@Lukasa
Copy link
Member

Lukasa commented Mar 6, 2017

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.

@ghost
Copy link
Author

ghost commented Mar 6, 2017

Does it mean I must call session.close() if I want to use the requests.Session()?

@Lukasa
Copy link
Member

Lukasa commented Mar 6, 2017

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 close.

biomadeira added a commit to biomadeira/pyPDBeREST that referenced this issue Jun 23, 2017
…g: unclosed socket.socket') by following the solution discussed on psf/requests#3912
@jdufresne
Copy link
Contributor

jdufresne commented Sep 6, 2017

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 close.

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 __del__ for resource management:

It is not guaranteed that __del__() methods are called for objects that still exist when the interpreter exits.

And, exceptions occurring during __del__ are ignored and can't be handled:

Due to the precarious circumstances under which __del__() methods are invoked, exceptions that occur during their execution are ignored, and a warning is printed to sys.stderr instead.

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.

@Garito
Copy link

Garito commented Jan 2, 2018

Hi!
I've same problem with my test only they never end because this error (?)

Could you provide an example of how to solve this issue?

@sigmavirus24
Copy link
Contributor

If this warning really should be ignored as suggested here, then maybe it shouldn't be logged.

@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 __del__ so I'm confused as to why you're mentioning that here. It seems you're arguing against an invisible person and I'm not sure why you're doing that here.

I've same problem with my test only they never end because this error (?)

@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.

@psf psf locked as off-topic and limited conversation to collaborators Jan 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants