Merge pull request #2551 from overleaf/bg-fix-race-condition-in-open

fix worst case of race condition in EditorManager.openNewDocument

GitOrigin-RevId: 24cbc87de2e420c35ffd6a82a2cd761229fd03e1
This commit is contained in:
Brian Gough 2020-01-29 15:06:29 +00:00 committed by Copybot
parent 4bbc745f4a
commit 327f8979b6

View file

@ -145,6 +145,9 @@ define([
} }
} }
// If we already have the document open we can return at this point.
// Note: only use forceReopen:true to override this when the document is
// is out of sync and needs to be reloaded from the server.
if (doc.id === this.$scope.editor.open_doc_id && !options.forceReopen) { if (doc.id === this.$scope.editor.open_doc_id && !options.forceReopen) {
// automatically update the file tree whenever the file is opened // automatically update the file tree whenever the file is opened
this.ide.fileTreeManager.selectEntity(doc) this.ide.fileTreeManager.selectEntity(doc)
@ -154,6 +157,7 @@ define([
return return
} }
// We're now either opening a new document or reloading a broken one.
this.$scope.editor.open_doc_id = doc.id this.$scope.editor.open_doc_id = doc.id
this.$scope.editor.open_doc_name = doc.name this.$scope.editor.open_doc_name = doc.name
@ -188,14 +192,19 @@ define([
} }
sl_console.log('[_openNewDocument] Opening...') sl_console.log('[_openNewDocument] Opening...')
const current_sharejs_doc = this.$scope.editor.sharejs_doc const current_sharejs_doc = this.$scope.editor.sharejs_doc
if (current_sharejs_doc != null) { const new_sharejs_doc = Document.getDocument(this.ide, doc.id)
// Leave the current document only when we are opening a different new
// one, to avoid race conditions between leaving and joining the same
// document.
if (
current_sharejs_doc != null &&
current_sharejs_doc !== new_sharejs_doc
) {
sl_console.log('[_openNewDocument] Leaving existing open doc...') sl_console.log('[_openNewDocument] Leaving existing open doc...')
current_sharejs_doc.leaveAndCleanUp() current_sharejs_doc.leaveAndCleanUp()
this._unbindFromDocumentEvents(current_sharejs_doc) this._unbindFromDocumentEvents(current_sharejs_doc)
} }
const new_sharejs_doc = Document.getDocument(this.ide, doc.id)
return new_sharejs_doc.join(error => { return new_sharejs_doc.join(error => {
if (error != null) { if (error != null) {
return callback(error) return callback(error)