Skip to content

Commit

Permalink
fix race connection. also close #105
Browse files Browse the repository at this point in the history
make sure we are releasing the socket and eventually read the rest of
the body.
  • Loading branch information
benoitc committed Aug 1, 2012
1 parent 43ac8e5 commit 63f7bb0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion restkit/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ def perform(self, request):

except NoMoreData, e:
if conn is not None:
conn.close()
conn.release(True)

request.maybe_rewind(msg=str(e))
if tries >= self.max_tries:
Expand Down
13 changes: 13 additions & 0 deletions restkit/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ def __init__(self, resp, connection):
self.resp = resp
self.body = resp._body
self.connection = connection
self._closed = False
self.eof = False

def __enter__(self):
return self
Expand All @@ -160,7 +162,14 @@ def __exit__(self, exc_type, exc_val, traceback):

def close(self):
""" release connection """
if self._closed:
return

if not self.eof:
self.body.read()

This comment has been minimized.

Copy link
@RonnyPfannschmidt

RonnyPfannschmidt Aug 3, 2012

this will create quite some trouble for discarded chunked responses
i sense a potential for surprising "deadlocks"

This comment has been minimized.

Copy link
@benoitc

benoitc Aug 3, 2012

Author Owner

Most client do that though I'm not sure I like to close the response here, but this is still the question.

This comment has been minimized.

Copy link
@RonnyPfannschmidt

RonnyPfannschmidt Aug 3, 2012

i think its a good heuristics to drop it if its chucked
also if more than just a bit of the body is missing, drop could end up less expensive than consume

This comment has been minimized.

Copy link
@krisb78

krisb78 Jan 24, 2015

This seems to be the cause of:

benoitc/couchdbkit#147


self.connection.release(self.resp.should_close)
self._closed = True

def __iter__(self):
return self
Expand All @@ -169,24 +178,28 @@ def next(self):
try:
return self.body.next()
except StopIteration:
self.eof = True
self.close()
raise

def read(self, n=-1):
data = self.body.read(n)
if not data:
self.eof = True
self.close()
return data

def readline(self, limit=-1):
line = self.body.readline(limit)
if not line:
self.eof = True
self.close()
return line

def readlines(self, hint=None):
lines = self.body.readlines(hint)
if self.body.close:
self.eof = True
self.close()
return lines

Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ def main():
'couchdb_proxy = restkit.contrib.wsgi_proxy:make_couchdb_proxy',
]},
install_requires = [
'http-parser>=0.7.5',
'socketpool>=0.4.2',
'http-parser>=0.7.7',
'socketpool>=0.5.0',
'nose',
'webob'],
test_suite = 'nose.collector'
Expand Down

0 comments on commit 63f7bb0

Please sign in to comment.