diff --git a/cypress/e2e/SessionApi.spec.js b/cypress/e2e/SessionApi.spec.js index 9287360df77..c06c932ecb5 100644 --- a/cypress/e2e/SessionApi.spec.js +++ b/cypress/e2e/SessionApi.spec.js @@ -349,5 +349,16 @@ describe('The session Api', function() { }) }) + // Failed with a probability of ~ 50% initially + it('ignores steps stored after close cleaned up', function() { + cy.pushAndClose({ connection, steps: [messages.update], version }) + cy.createTextSession(undefined, { filePath: '', shareToken }) + .then(con => { + connection = con + }) + .its('state.documentSource') + .should('eql', '## Hello world\n') + }) + }) }) diff --git a/cypress/support/sessions.js b/cypress/support/sessions.js index 3774e384d1d..2f243bb20a7 100644 --- a/cypress/support/sessions.js +++ b/cypress/support/sessions.js @@ -51,3 +51,14 @@ Cypress.Commands.add('syncSteps', (connection, options = { version: 0 }) => { return connection.sync(options) .then(response => response.data) }) + +// Used to test for race conditions between the last push and the close request +Cypress.Commands.add('pushAndClose', ({ connection, steps, version, awareness = '' }) => { + cy.log('Race between push and close') + .then(() => { + const push = connection.push({ steps, version, awareness }) + .catch(e => e) // handle 403 gracefully + const close = connection.close() + return Promise.all([push, close]) + }) +}) diff --git a/lib/Service/ApiService.php b/lib/Service/ApiService.php index 176267b10ab..b85c8069d96 100644 --- a/lib/Service/ApiService.php +++ b/lib/Service/ApiService.php @@ -113,12 +113,12 @@ public function create($fileId = null, $filePath = null, $token = null, $guestNa $readOnly = $this->documentService->isReadOnly($file, $token); $this->sessionService->removeInactiveSessions($file->getId()); - $activeSessions = $this->sessionService->getActiveSessions($file->getId()); + $remainingSessions = $this->sessionService->getAllSessions($file->getId()); $freshSession = false; - if ($forceRecreate || count($activeSessions) === 0) { + if ($forceRecreate || count($remainingSessions) === 0) { + $freshSession = true; try { $this->documentService->resetDocument($file->getId(), $forceRecreate); - $freshSession = true; } catch (DocumentHasUnsavedChangesException $e) { } }