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.

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

    Issue actions