classification
Title: HTTPSConnection.close() does not immediately close the connection.
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 2.6
process
Status: closed Resolution: out of date
Dependencies: Superseder: SSL sockets do not retain the parent socket's attributes
View: 8524
Assigned To: Nosy List: PoltoS, TTT, amaury.forgeotdarc, dandrzejewski, eric.smith, giampaolo.rodola, orsenthil, pitrou, r.david.murray
Priority: normal Keywords:

Created on 2010-04-02 18:58 by dandrzejewski, last changed 2010-10-16 21:16 by r.david.murray. This issue is now closed.

Messages (11)
msg102186 - (view) Author: David Andrzejewski (dandrzejewski) Date: 2010-04-02 18:58
Python 2.6.4, Windows XP.

If you run the following code:

import httplib

http_connection = httplib.HTTPConnection("192.168.192.196")
http_connection.request("GET", "/")
http_connection.sock.settimeout(20)
response = http_connection.getresponse()
data = response.read()
http_connection.close()


And then run a netstat -a, you'll see that the connection is no longer established (it'll have a status of TIME_WAIT or something along those lines).

However, if you run the following code:

import httplib

http_connection = httplib.HTTPSConnection("192.168.192.196")
http_connection.request("GET", "/")
http_connection.sock.settimeout(20)
response = http_connection.getresponse()
data = response.read()
http_connection.close()

And run a netstat, the connection will still be in the ESTABLISHED state.  The connection does not end for several minutes (or when you terminate the program).

I can get the connection to end when I want it by doing a del on the connection object and then manually running garbage collection - although that seems a bit wonky.

I'm not really sure what all the differences are between HTTPConnection and HTTPSConnection, but it seems like they should both behave the same way in this regard.
msg102471 - (view) Author: David Andrzejewski (dandrzejewski) Date: 2010-04-06 16:08
Now, it turns out that if you send the HTTP "Connection: close" header, the connection does close at the end (because the server closes it).

Still, it seems like this should behave the same regardless of whether it's HTTPConnection or HTTPSConnection.
msg111083 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2010-07-21 16:19
issue8524 fixed a similar issue (the timeout of the initial socket was not passed to the ssl socket).
Can someone test again with a recent py3k build?
msg111118 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-07-21 20:53
Works fine here under Linux, with 3.2, 2.6 and 2.7. HTTPS connections actually get recycled quicker than plain HTTP ones.
Please note that your results could also depend on the behaviour of the target server. I tested against http://linuxfr.org.
msg111119 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-07-21 20:57
Works fine under a Windows XP VM too.
msg118117 - (view) Author: PoltoS (PoltoS) Date: 2010-10-07 16:13
I've the with module ssl. If I do sock.close() (sock is instance of ssl.SSLSocket), the connection is not closed: I see it as Established in netstat and nothing is sent over network: tcpdump show nothing going thru the network.

Python 2.6.5
Linux Ubuntu
msg118119 - (view) Author: PoltoS (PoltoS) Date: 2010-10-07 16:22
By the way, doing a sock.shutdown(socket.SHUT_RDWR) with a subsequent sock.close() helps to really close an SSL socket. But this may raise additional errors with subsequent close() since on some OS (like OS X) shutdown may close the socket. Am I right?
msg118712 - (view) Author: Tiago Teresa Teodosio (TTT) Date: 2010-10-14 19:26
I can make the connection really get closed by running the garbage collector. It seems that the SSL socket has some kind of lazy close.

import gc
gc.collect()
msg118763 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-10-15 12:43
If I'm reading this thread correctly, this bug should be closed and a new one opened about SSL socket close.  Antoine, does that sound correct?
msg118765 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-10-15 13:03
Probably, yes.
msg118897 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-10-16 21:16
I opened issue 10127 for the ssl.SSLSocket().close() problem.
History
Date User Action Args
2010-10-16 21:16:30r.david.murraysetstatus: open -> closed
resolution: works for me -> out of date
messages: + msg118897

stage: resolved
2010-10-15 13:03:39pitrousetmessages: + msg118765
2010-10-15 12:47:47giampaolo.rodolasetnosy: + giampaolo.rodola
2010-10-15 12:43:28r.david.murraysetnosy: + r.david.murray
messages: + msg118763
2010-10-14 19:26:09TTTsetnosy: + TTT
messages: + msg118712
2010-10-07 16:22:16PoltoSsetmessages: + msg118119
2010-10-07 16:13:09PoltoSsetstatus: pending -> open
nosy: + PoltoS
messages: + msg118117

2010-07-21 20:57:27pitrousetstatus: open -> pending
resolution: out of date -> works for me
messages: + msg111119
2010-07-21 20:53:08pitrousetnosy: + pitrou
messages: + msg111118
2010-07-21 16:19:28amaury.forgeotdarcsetnosy: + amaury.forgeotdarc
messages: + msg111083
resolution: out of date

superseder: SSL sockets do not retain the parent socket's attributes
2010-04-06 16:08:34dandrzejewskisetmessages: + msg102471
2010-04-02 21:25:59eric.smithsetnosy: + eric.smith
2010-04-02 19:01:11pitrousetpriority: normal
nosy: + orsenthil
2010-04-02 18:58:27dandrzejewskicreate