Skip to content

Cache Concurrency Guard #117

Closed
0 of 1 issue completed
Closed

Description

@JeremyCaney

Background

Lazy loading introduces a concurrency bug into the shared CachedTopicRepository singleton where reading a topic's children (or other properties) used to be a safe read, but now triggers an in-place merge with no synchronization, so two concurrent requests hitting the same unloaded topic can throw a InvalidOperationException or corrupt the state.

Solution

The fix is a per-topic async gate (not a global lock) that serializes loads of the same topic while leaving unrelated topics fully concurrent, with the gate safely reclaimed once a topic is fully loaded (relying on the fact these boundaries only ever progress from LoadState.NotLoaded to LoadState.Loaded).

  • Create unit test to expose the issue
  • Per-topic SemaphoreSlim gate with double-checked-locking pattern in EnsureLoaded()

Note: This explicitly excludes associations (i.e., relationships and references), since their LoadState is dynamic; those will be handled separately (#128).

Activity

  1. added this to the OnTopic 6.0.0 milestone on Jul 25, 2026
  2. self-assigned this
    on Jul 25, 2026
  3. added
    Type: BugBehavior that is inconsistent with documented or expected behavior.
    Area: RepositoriesRelates to the `ITopicRepository` interface or one of its implementations.
    Type: ImprovementImproves the functionality or interface of an existing feature.
    on Jul 25, 2026
  4. added theissue type on Jul 25, 2026
  5. added a commit that references this issue on Aug 7, 2026
  6. added
    Status 5: CompleteTask is considered complete, and ready for deployment.
    and removed on Aug 9, 2026
  7. JeremyCaney commented on Aug 9, 2026

    @JeremyCaney
    MemberAuthor

    Merged with 362b9fb:

    • Create unit test to expose the issue
      • Establish BlockingStubLazyLoadingTopicRepository to test concurrency issues (2875725)
      • Introduced concurrency unit test (14fb36e)
    • Per-topic SemaphoreSlim gate with double-checked-locking pattern in EnsureLoaded()
      • Added EnsureLoaded() concurrency gate per topic (5779261)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Area: RepositoriesRelates to the `ITopicRepository` interface or one of its implementations.Priority: 1Severity 2: MajorStatus 5: CompleteTask is considered complete, and ready for deployment.Type: BugBehavior that is inconsistent with documented or expected behavior.Type: ImprovementImproves the functionality or interface of an existing feature.

Type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions