My python program use httplib2.Http to make http request. Once I need to generate a request, I create a httplib2.Http object, so that my program will frequently create/destroy httplib2.Http objects.
I found my program was easy to crash due to reach max number of open files. Checking /proc//fd, there were too many open socket fds. The problem made me to have to dig into httplib2 source code.
Then I found that, in httplib2.Http._conn_request method, there was code like this:
else:
content = ""
if method == "HEAD":
conn.close()
else:
content = response.read()
response = Response(response)
if method != "HEAD":
content = _decompressContent(response, content)
break
This shows that socket is only closed when http method is HEAD. Maybe httplib2 wanted somehow to reuse the socket. But Http class doesn't have a close() method. That mean when I makes a Http request, the socket will not close until my process terminates.
Then I modified the code:
else:
content = ""
if method == "HEAD":
conn.close()
else:
content = response.read()
response = Response(response)
if method != "HEAD":
content = _decompressContent(response, content)
conn.close() # I ADD THIS CLOSE
break
After that, my program worked well.
But I'm still curious if that's really httplib2's bug given that httplib2 is a very old and common lib.