Fixed a newly-introduced race condition in updateDoc

If two tasks update a doc at the same time, one of them might acquire a
new sequence number first, but fail the race to save, and then store the
now-not-newest sequence number in the document. In this case, it will
now generate a new sequence number during the second attempt, rather
than store a lower value.

(Also added/improved some comments.)