Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
d862db0d
Commit
d862db0d
authored
Dec 01, 2014
by
Serhiy Storchaka
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #21032. Fixed socket leak if HTTPConnection.getresponse() fails.
Original patch by Martin Panter.
parent
169f195b
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
42 additions
and
11 deletions
+42
-11
Lib/httplib.py
Lib/httplib.py
+14
-10
Lib/test/test_httplib.py
Lib/test/test_httplib.py
+24
-1
Misc/ACKS
Misc/ACKS
+1
-0
Misc/NEWS
Misc/NEWS
+3
-0
No files found.
Lib/httplib.py
View file @
d862db0d
...
...
@@ -1070,18 +1070,22 @@ class HTTPConnection:
kwds
[
"buffering"
]
=
True
;
response
=
self
.
response_class
(
*
args
,
**
kwds
)
response
.
begin
()
assert
response
.
will_close
!=
_UNKNOWN
self
.
__state
=
_CS_IDLE
try
:
response
.
begin
()
assert
response
.
will_close
!=
_UNKNOWN
self
.
__state
=
_CS_IDLE
if
response
.
will_close
:
# this effectively passes the connection to the response
self
.
close
()
else
:
# remember this, so we can tell when it is complete
self
.
__response
=
response
if
response
.
will_close
:
# this effectively passes the connection to the response
self
.
close
()
else
:
# remember this, so we can tell when it is complete
self
.
__response
=
response
return
response
return
response
except
:
response
.
close
()
raise
class
HTTP
:
...
...
Lib/test/test_httplib.py
View file @
d862db0d
...
...
@@ -25,6 +25,7 @@ class FakeSocket:
self
.
text
=
text
self
.
fileclass
=
fileclass
self
.
data
=
''
self
.
file_closed
=
False
self
.
host
=
host
self
.
port
=
port
...
...
@@ -34,7 +35,13 @@ class FakeSocket:
def
makefile
(
self
,
mode
,
bufsize
=
None
):
if
mode
!=
'r'
and
mode
!=
'rb'
:
raise
httplib
.
UnimplementedFileMode
()
return
self
.
fileclass
(
self
.
text
)
# keep the file around so we can check how much was read from it
self
.
file
=
self
.
fileclass
(
self
.
text
)
self
.
file
.
close
=
self
.
file_close
#nerf close ()
return
self
.
file
def
file_close
(
self
):
self
.
file_closed
=
True
def
close
(
self
):
pass
...
...
@@ -433,6 +440,22 @@ class BasicTest(TestCase):
self
.
assertEqual
(
resp
.
read
(),
''
)
self
.
assertTrue
(
resp
.
isclosed
())
def
test_error_leak
(
self
):
# Test that the socket is not leaked if getresponse() fails
conn
=
httplib
.
HTTPConnection
(
'example.com'
)
response
=
[]
class
Response
(
httplib
.
HTTPResponse
):
def
__init__
(
self
,
*
pos
,
**
kw
):
response
.
append
(
self
)
# Avoid garbage collector closing the socket
httplib
.
HTTPResponse
.
__init__
(
self
,
*
pos
,
**
kw
)
conn
.
response_class
=
Response
conn
.
sock
=
FakeSocket
(
''
)
# Emulate server dropping connection
conn
.
request
(
'GET'
,
'/'
)
self
.
assertRaises
(
httplib
.
BadStatusLine
,
conn
.
getresponse
)
self
.
assertTrue
(
response
)
#self.assertTrue(response[0].closed)
self
.
assertTrue
(
conn
.
sock
.
file_closed
)
class
OfflineTest
(
TestCase
):
def
test_responses
(
self
):
self
.
assertEqual
(
httplib
.
responses
[
httplib
.
NOT_FOUND
],
"Not Found"
)
...
...
Misc/ACKS
View file @
d862db0d
...
...
@@ -1010,6 +1010,7 @@ Todd R. Palmer
Juan David Ibáñez Palomar
Jan Palus
Yongzhi Pan
Martin Panter
Mathias Panzenböck
M. Papillon
Peter Parente
...
...
Misc/NEWS
View file @
d862db0d
...
...
@@ -13,6 +13,9 @@ Core and Builtins
Library
-------
- Issue #21032. Fixed socket leak if HTTPConnection.getresponse() fails.
Original patch by Martin Panter.
- Issue #22609: Constructors and update methods of mapping classes in the
collections module now accept the self keyword argument.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment