Skip to content

Commit

Permalink
Prevent infinite loop in client when first changed usn != lastUsn
Browse files Browse the repository at this point in the history
Anki expects the last change in the list to have the server's lastUsn.

Reference: https://github.com/ankitects/anki/blob/cca3fcb2418880d0430a5c5c2e6b81ba260065b7/anki/sync.py#L720
  • Loading branch information
tsudoko committed Jan 26, 2020
1 parent 2bfccf7 commit 3c44062
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ankisyncd/sync_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,10 @@ def mediaChanges(self, lastUsn):
for fname,usn,csum, in self.col.media.db.execute("select fname,usn,csum from media order by usn desc limit ?", server_lastUsn - lastUsn):
result.append([fname, usn, csum])

# anki assumes server_lastUsn == result[-1][1]
# ref: anki/sync.py:720 (commit cca3fcb2418880d0430a5c5c2e6b81ba260065b7)
result.reverse()

return {'data': result, 'err': ''}

def mediaSanity(self, local=None):
Expand Down
15 changes: 15 additions & 0 deletions tests/test_media.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,18 @@ def test_upgrade(self):
)
self.assertEqual(cm.lastUsn(), sm.lastUsn())
self.assertEqual(list(sm.db.execute("SELECT usn FROM media")), [(161,), (161,)])

def test_mediaChanges_lastUsn_order(self):
col = self.colutils.create_empty_col()
col.media = ankisyncd.media.ServerMediaManager(col)
mh = ankisyncd.sync_app.SyncMediaHandler(col)
mh.col.media.db.execute("""
INSERT INTO media (fname, usn, csum)
VALUES
('fileA', 101, '53059abba1a72c7aff34a3eaf7fef10ed65541ce'),
('fileB', 100, 'a5ae546046d09559399c80fa7076fb10f1ce4bcd')
""")

# anki assumes mh.col.media.lastUsn() == mh.mediaChanges()['data'][-1][1]
# ref: anki/sync.py:720 (commit cca3fcb2418880d0430a5c5c2e6b81ba260065b7)
self.assertEqual(mh.mediaChanges(lastUsn=99)['data'][-1][1], mh.col.media.lastUsn())

0 comments on commit 3c44062

Please sign in to comment.