lanto03 / couchdb-python

Automatically exported from code.google.com/p/couchdb-python
Other
0 stars 0 forks source link

Spurious "document update conflict" due to httplib bug? #93

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
I was attempting to write the following code:

>>> db = couchdb.client.Server(url)[dbname]
>>> key in db
False
>>> db[key] = {"foo": "bar"}
>>> key in db
True

Unfortunately that doesn't work as expected -- db[key]={...} raises a
ResourceConflict ('Document update conflict.') exception. It's entirely
reproducible, and appears to be due to an httplib bug -- basically httplib
keeps the connection open after the HEAD query for "key in db", but after
sending the PUT request for db[key]=..., decides it's in a bad state and
raises an exception rather than returning the response. At this point
httplib2 decides to have another go, closes and reopens the connection and
resends the PUT request to httplib, which receives the conflict response
which gets passed back to python-couchdb.

As far as I can tell, the Python httplib code just doesn't support keeping
a connection open for multiple requests, but I might be misunderstanding
the code -- it seems like quite a mess to me. In any event, the easiest
solution seems to be to force the connection to close by adding the
"Connection: close" header.

--- /usr/share/pyshared/couchdb/client.py   2009-10-02 02:54:10.000000000 +1000
+++ client.py   2009-10-02 02:54:00.000000000 +1000
@@ -992,6 +992,7 @@
                  **params):
         from couchdb import __version__
         headers = headers or {}
+        headers.setdefault('Connection', 'close')
         headers.setdefault('Accept', 'application/json')
         headers.setdefault('User-Agent', 'couchdb-python %s' % __version__)
         body = None

FWIW, I'm running couchdb on Debian squeeze, versions are:

    couchdb         0.9.0-2+b1
    python-couchdb  0.6-1

HTH.

Original issue reported on code.google.com by anthony....@gmail.com on 1 Oct 2009 at 4:55

Attachments:

GoogleCodeExporter commented 8 years ago
Yes, I've experienced this as well. httplib2 0.4 works fine, but I think 
httplib2 0.5 has caching enabled or 
something like that. Getting compatible with 0.5 would be nice since it plays 
nicer with Python 2.6 whereas 0.4 
throws warnings because of md5 library usage.

Original comment by dnolen.l...@gmail.com on 2 Oct 2009 at 1:49

GoogleCodeExporter commented 8 years ago
Yep, this is an httplib2 0.5 bug. See issue 85 
(http://code.google.com/p/couchdb-
python/issues/detail?id=85&sort=-id#c11) for more detail and fix.

Also, please go and star the httplib2 issue, 
http://code.google.com/p/httplib2/issues/detail?id=67. Perhaps they'll get a 
release 
out sometime soon the more people track it.

Original comment by matt.goo...@gmail.com on 2 Oct 2009 at 3:40

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago

Original comment by matt.goo...@gmail.com on 2 Oct 2009 at 3:41

GoogleCodeExporter commented 8 years ago
Thanks for update. I starred the issue at the httplib2 project.

Original comment by dnolen.l...@gmail.com on 2 Oct 2009 at 4:55

GoogleCodeExporter commented 8 years ago
Also causes double insert of new documents. Patch did fix it for me and I 
starred the
issue with httplib2. Thanks, guys!

Original comment by luke.a...@gmail.com on 19 Oct 2009 at 6:56