Skip to content

Can SnapshottingCollection.GetSnapshot() call CreateSnapshot() multiple times? #41663

Open
@omajid

Description

@omajid

This code checks that snapshot == null before taking a lock, but never checks it again:

protected TCollection GetSnapshot()
{
TCollection localSnapshot = snapshot;
if (localSnapshot == null)
{
lock (Collection)
{
snapshot = CreateSnapshot(Collection);
localSnapshot = snapshot;
}
}
return localSnapshot;
}

I think this sequence of events can result in multiple calls to CreateSnapshot():

  1. Thread 1 calls GetSnapshot() and see localSnapshot = snapshot && localSnapshot == null
  2. Thread 2 calls GetSnapshot() and see localSnapshot = snapshot && localSnapshot == null
  3. Thread 1 enters the lock block, and runs CreateSnapshot, and updates snapshot
  4. Thread 2 enters the lock block, runs CreateSnapshot again, and updates snapshot again.

Activity

ghost added
untriagedRequest triage from a team member
on Jun 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @omajid

        Issue actions

          Can SnapshottingCollection.GetSnapshot() call CreateSnapshot() multiple times? · Issue #41663 · dotnet/sdk