Skip to content

fix(activesync): keep the Mongo state lock on the document it was taken on - #118

Merged
ralflang merged 1 commit into
fix/state-device-change-key-seriesfrom
fix/state-mongo-locked-document
Nov 13, 2026
Merged

ralflang merged 1 commit into
fix/state-device-change-key-seriesfrom
fix/state-mongo-locked-document

Conversation

@TDannhauer

Copy link
Copy Markdown
Contributor

Summary

  • Horde_ActiveSync_State_Mongo now remembers which document its lock was taken on and releases the lock by that key, instead of deriving it from the state object's current sync key and collection.
  • A state saved under a new sync key is inserted as a new document; the lock on the document it was loaded from is released afterwards.
  • FOLDERSYNC state is looked up under the folder id it is stored with.

Fixes #117.

Builds on #116 (same methods in State/Mongo.php); this PR only adds the last commit.

Motivation

_stateRowLockQuery() builds the lock query from $this->_syncKey and $this->_collection['id']. Both change before the lock is released:

  • setNewSyncKey() sets the key of the state to be saved. The locked update in _saveSyncStateRow() then looked for the not yet existing document of the new key, matched nothing, and every save after a key advance failed with Error saving state. The lock on the loaded document stayed until the stale timeout.
  • loadState() sets the next sync key and collection before _loadState() releases the previous lock, so the lock of the previously loaded folder was never released (PING polling several folders).
  • For FOLDERSYNC the collection array is empty, so the query asked for sync_folderid = null while save() stores 'foldersync'.

Changes

lib/Horde/ActiveSync/State/Mongo.php

  • New property $_stateLockSyncKey, set in _acquireStateRowLock(), cleared on release.
  • _releaseStateRowLock() addresses the lock by _id = $_stateLockSyncKey and the lock token.
  • _saveSyncStateRow(): the single conditional update is kept for a save under the loaded key. A save under another key goes through the insert path (including _recoverFromFailedStateInsert() from fix(activesync): keep sync state of two devices apart after a device change #116) and then releases the lock; the existing error handling releases it when the insert fails.
  • New _stateFolderId(), used by _stateRowLockQuery() and _isOwnStateRow().

No new catch blocks.

Tests

  • New LockedDocumentTest.php: save under a new key, save under the loaded key, failed save under a new key, loading another folder, FOLDERSYNC load and save. Four of the five fail without the change.
  • InMemoryMongoCollection and InMemoryMongoDb move from DeviceChangeTest.php into their own files so both test classes can use them.
  • RowLockTest.php, CollectionLockTest.php: the fixtures that fake a held lock also set the locked key.

Not verified

None of this was run against a MongoDB server (none available). The defects were found by reading the code and reproduced against the in-memory stand-in for the legacy MongoCollection API; the stand-in only implements the query operators the state driver uses on HAS_state.

Test plan

  • test/integration/Horde/ActiveSync/StateTest/Mongo: 19 tests pass, 23 tests that need a MongoDB server are skipped
  • Full test/unit suite inside a Horde deployment: 425 tests, no new failures (4 existing, unrelated failures in AppointmentTest, FindRequestTest, Rfc822Test)
  • MongoDB deployment: Sync and FolderSync cycle with key advance

…en on

State_Mongo addressed its document lock through the current sync key and
collection of the state object. Both change before the lock is released:
setNewSyncKey() sets the key of the state to be saved, and loadState()
sets the next key and collection.

- Remember the sync key the lock was acquired on and release the lock by
  that key. The lock of the previously loaded folder is now released when
  the next folder is loaded.
- A state saved under a new sync key is inserted as a new document and
  the lock on the loaded document is released afterwards. Before, the
  locked update looked for the not yet existing document and every such
  save failed with "Error saving state."
- Look the FOLDERSYNC state up under the folder id save() stores it
  with, instead of the unset collection id.

Tests run against an in-memory stand-in for the MongoCollection API,
which moves into its own files.
@TDannhauer
TDannhauer requested a review from ralflang November 12, 2026 10:18
@ralflang
ralflang merged commit 6b7dcb0 into fix/state-device-change-key-series Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants