Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to write revision with attachment #802

Closed
ryuzmukhametov opened this issue Jul 2, 2015 · 8 comments
Closed

Failed to write revision with attachment #802

ryuzmukhametov opened this issue Jul 2, 2015 · 8 comments

Comments

@ryuzmukhametov
Copy link

In my app sometimes when a document with attachment is pulled I get next warning:

19:06:15.987‖ WARNING: CBL_Puller[http://localhost:5984/a] failed to write {my_doc_id #4-a0716cf34b240d1dec1ef7ada408063e}: status=491

As result the pull replication is stopped. Moreover it is impossible to start pull replication even it uses start or restart methods.

Next steps show how reproduce it in 'CBL iOS Test App' (sha commit: 9aa3bce)

  • Configure the app with syncpoint
  • Launch the application and create a document with attachment (see 'createDocument With Attachment' below). The document is pushed to the server.
  • Stop application
  • Change document many times in the server (more than one time)
  • Start the app

When the app start it can't pull revision from remote server.

Sample code for create document with attachment (I call it from viewWillAppear):

- (void)createDocumentWithAttachment
{
    NSString *docId = @"my_doc_id";
    CBLDatabase *db = self.database;
    CBLDocument *doc = [db documentWithID:docId];
    CBLSavedRevision *rev = [doc putProperties:@{@"key":@"value"} error:nil];
    NSLog(@"rev = %@", rev.revisionID);
    NSString* str = @"teststring";
    NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];
    NSString *contentType = @"text/plain";
    CBLSavedRevision *currentRevision = rev;
    CBLUnsavedRevision *unsavedRevision = [currentRevision createRevision];
    [unsavedRevision setAttachmentNamed:@"my_attachment" withContentType:contentType content:data];
    currentRevision = [unsavedRevision save:nil];
    NSLog(@"%@", currentRevision.revisionID);
}

It occurs when I use SQLite and ForestDB data storage.

@ryuzmukhametov
Copy link
Author

Here is a second time launch application log (use CBLDatabase, ChangeTracker, RemoteRequest, Sync log chanels) https://gist.github.com/ryuzmukhametov/cad86667b86d993348be

@snej
Copy link
Contributor

snej commented Jul 2, 2015

491 is kCBLStatusBadAttachment so something's going wrong saving the attachment.

What is the exact server you're using?

@snej
Copy link
Contributor

snej commented Jul 2, 2015

Thanks for the logs. From the server URL http://localhost:5984 I'm guessing you are replicating with CouchDB. (What version?)

This sounds like #786 except you're not getting the same warning message. But it's probably also related to the fact that recent versions of CouchDB have started including MIME headers in their attachment multipart bodies.

@ryuzmukhametov
Copy link
Author

I use Apache CouchDB version 1.6.1

@snej
Copy link
Contributor

snej commented Jul 2, 2015

OK, I can reproduce it (with the latest master branch). The error occurs at CBLDatabase+Attachments.m:350:

                    if (status == kCBLStatusOK || status == kCBLStatusNotFound)
                        status = kCBLStatusBadAttachment;

The attachment being processed is a stub but the local db doesn't have the parent revision, so it fails. It should be looking farther back in the rev history, since in this case there are several missing revs between the one being inserted and the last one that's known locally.

@snej snej added this to the 1.2 milestone Jul 2, 2015
@snej snej self-assigned this Jul 2, 2015
@snej snej closed this as completed in a20e014 Jul 2, 2015
@snej
Copy link
Contributor

snej commented Jul 2, 2015

That was a nasty little edge case — thanks for reporting it. Only happened when pulling from CouchDB, because CouchDB identifies attachments with MD5 digests instead of SHA-1, so our code can't look up the attachment by its digest.

@snej
Copy link
Contributor

snej commented Jul 2, 2015

FYI, you should be able to cherry-pick this fix onto the 1.1 branch. At this point that would be safer than trying to use the master branch (too many brand-new changes.) You'll also want to pick up the other CouchDB compatibility fix (see #786.)

@snej snej modified the milestones: 1.2, 1.1.1 Jul 2, 2015
@ryuzmukhametov
Copy link
Author

Thank you, Jens, now it works for me!

ryuzmukhametov added a commit to ryuzmukhametov/couchbase-lite-ios that referenced this issue Jul 3, 2015
Nasty edge case where the attachment is unchanged, but the parent rev
isn't available (doc was changed multiple times remotely), _and_ the
attachment metadata doesn't contain a usable digest (CouchDB's digests
are MD5 not SHA-1.)
Fixes couchbase#802
snej added a commit that referenced this issue Sep 2, 2015
Nasty edge case where the attachment is unchanged, but the parent rev
isn't available (doc was changed multiple times remotely), _and_ the
attachment metadata doesn't contain a usable digest (CouchDB's digests
are MD5 not SHA-1.)
Fixes #802
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants