Fix _logId() collision in SyncRepository with per-instance counter#8
Conversation
Co-authored-by: Harsh4114 <71206223+Harsh4114@users.noreply.github.com>
_logId() collision in SyncRepository with per-instance counter
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
814cbe9
into
codex/design-bloc-like-architecture-for-sync-engine
There was a problem hiding this comment.
Pull request overview
Fixes potential SyncRepository._logId() collisions by adding a per-instance counter to the generated log IDs, aligning the scheme with SyncManager’s timestamp+counter pattern.
Changes:
- Add per-instance
_logCounterstate toSyncRepository. - Update
_logId()to generatesync_log_<micros>_<counter>.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| int _logCounter = 0; | ||
|
|
There was a problem hiding this comment.
_logId() now guarantees uniqueness only within a single SyncRepository instance. If multiple repositories share the same SyncLogStore (or if a store persists across app restarts and the counter resets), collisions can still happen because the ID lacks a repository/device/component discriminator. Consider including a configurable prefix (e.g., repository/device ID) or passing a logIdNamespace/instanceId into the repository so IDs are unique across all writers to the same store.
| String _logId() { | ||
| _logCounter += 1; | ||
| final micros = DateTime.now().microsecondsSinceEpoch; | ||
| return 'sync_log_$micros'; | ||
| return 'sync_log_${micros}_$_logCounter'; |
There was a problem hiding this comment.
This change addresses a timing-resolution collision, but there’s no deterministic test covering it (calling add() in a tight loop may not reliably reproduce the original bug on all platforms). To make this behavior testable and prevent regressions, consider injecting a time provider/clock into SyncRepository (used by _logId() and SyncLog.timestamp) so a test can force identical timestamps and assert IDs remain unique.
SyncRepository._logId()used onlymicrosecondsSinceEpoch, allowing ID collisions when multiple logs are created within the same microsecond (e.g. rapid sequential ops, tight test loops).Changes
SyncRepository: Addedint _logCounter = 0;per-instance field and updated_logId()to append it, matching theSyncManager._generateOpId()pattern.Log IDs are now
sync_log_<microseconds>_<counter>, guaranteed unique within aSyncRepositoryinstance.💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.