pouchdb-community / pouchdb-load

Load documents into CouchDB/PouchDB from a dumpfile
Apache License 2.0
120 stars 33 forks source link

"Database encountered an unknown error" during replication on IOS #40

Open kripper opened 8 years ago

kripper commented 8 years ago

Pouchdb 5.1 is failing to replicate on IOS with error:

{\"status\":500,\"name\":\"unknown_error\",\"message\":\"Database encountered an unknown error\"}"

I also get this CORS warning:

"PouchDB: the remote database may not have CORS enabled.If not please enable CORS: http://pouchdb.com/errors.html#no_access_control_allow_origin_header"

AFAIU, there shouldn't be CORS warnings since I'm using:

<access origin="*" />

Besides, other databases are pulled without errors (though they were reading and writing 0 docs).

I'm using PhoneGap with this user agent:

Mozilla/5.0 (iPhone; CPU iPhone OS 7_1_2 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Mobile/11D257 (400210848)

Same code works fine on Android, Chrome on Desktop.

Any hint? Should I try an older version? I'm using jsconsole and can run commands and do tests directly on the iPhone if necessary to debug.

kripper commented 8 years ago

I tested Safari on IOS and got the same error.

kripper commented 8 years ago

I tested Safari on Windows and also get the same error. I confirmed that sometimes replication works fine for the same databases that other times failed. Besides, I saw another error I'm not sure if it is related: ReferenceError: Can't find variable: WebKitBlobBuilder This is probably triggered because we have some docs with blob attachements.

kripper commented 8 years ago

I found out that the error is occurring because of the blob attachments. When I disable the replication of this particular DB containing blob attachments, everything works fine. Any workaround to use blobs on IOS?

kripper commented 8 years ago

The exception that is thrown by new Blob(parts, properties) in createBlob() is:

"'[object BlobConstructor]' is not a constructor (evaluating 'new Blob(parts, properties)')"
kripper commented 8 years ago

:-(

Disabling the replication of the DB with the blob attachment fixed the problem on Safary for Windows (which was throwing the same error as on IOS)...but I'm still getting the error on PhoneGap for IOS and Safary for IOS.

So, we have two issues here:

1) Replication of blobs are failing on Safary for Windows and Safary for IOS and PhoneGap for IOS. This causes "Database encountered an unknown error" for this and subsequent replications. When disabling the replication of the DB with blobs, replication works at least on Safary for Windows, but NOT on Safary for IOS and PhoneGap for IOS.

2) Safary for IOS and PhoneGap for IOS causes "Database encountered an unknown error", even when not replicating blobs.

kripper commented 8 years ago

Turned out that the app was doing concurrent replication (pulls) of the same database. This was causing the error on Safari and IOS, but not on Android. I would suggest to detect this situation on Pouch's side (even when it's the users fault, it will save anyone's time and avoid users reporting bugs caused by race conditions ). To solve, I added a flag on the pouch database object to prevent replicating twice while another replication is going on for the same DB. There are many other reported issues regarding the "Database encountered an unknown error" message which could be related to this problem.

kripper commented 8 years ago

BTW, blobs are still generating an error when they are pulled on Safaty for Windows, Safari for IOS and PhoneGap for IOS (works fine on Android and Chrome). But they are pushed correctly on PhoneGap for IOS.

nolanlawson commented 8 years ago

Hmm, I'm not really understanding what's going on here. Do you have a reproducible test case?

I do know that there's a bug in iOS Safari WebView due to file:// URLs. Maybe you're hitting that: #11