Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply locking to fix join race conditions. Fixes #787 #788

Merged
merged 3 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/DynamicData.Tests/Cache/InnerJoinFixtureRaceCondition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,25 @@ public void LetsSeeWhetherWeCanRandomlyHitARaceCondition()
ids.InnerJoin(itemsCache.Connect(), x => x.Id, (_, thing) => thing).Subscribe((z) => { }, ex => { }, () => { });
}

// See https://github.com/reactivemarbles/DynamicData/issues/787
[Fact(Skip = "Heavyweight test so probably best run manually if this issue comes up again")]
public void LetsSeeWhetherWeCanRandomlyHitADifferentRaceCondition()
{
var leftCache = new SourceCache<Thing, long>(x => x.Id);
var rightCache = new SourceCache<Thing, long>(x => x.Id);
var joined = leftCache.Connect().InnerJoin(rightCache.Connect(), x => x.Id,
(keys, leftThing, rightThing) =>
new Thing { Id = keys.leftKey, Name = $"{leftThing.Name} x {rightThing.Name}" });

IDisposable StartUpdating(ISourceCache<Thing, long> sourceCache) =>
Observable.Range(1, 1_000_000, Scheduler.Default)
JakenVeina marked this conversation as resolved.
Show resolved Hide resolved
.Subscribe(x => sourceCache.AddOrUpdate(new Thing { Id = x, Name = $"{x}" }));

using var leftUpdater = StartUpdating(leftCache);
using var rightUpdater = StartUpdating(rightCache);
using var subscription = joined.Subscribe();
}

public class Thing
{
public long Id { get; set; }
Expand Down
5 changes: 4 additions & 1 deletion src/DynamicData/Cache/Internal/FullJoin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ public IObservable<IChangeSet<TDestination, TLeftKey>> Run() => Observable.Creat
return joinedCache.CaptureChanges();
});

return new CompositeDisposable(leftLoader.Merge(rightLoader).SubscribeSafe(observer), leftCache, rightCache);
lock (locker)
JakenVeina marked this conversation as resolved.
Show resolved Hide resolved
{
return new CompositeDisposable(leftLoader.Merge(rightLoader).SubscribeSafe(observer), leftCache, rightCache);
}
});
}
9 changes: 6 additions & 3 deletions src/DynamicData/Cache/Internal/InnerJoin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ internal class InnerJoin<TLeft, TLeftKey, TRight, TRightKey, TDestination>(IObse
{
foreach (var change in changes.ToConcreteType())
{
var leftCurent = change.Current;
var leftCurrent = change.Current;
var rightLookup = rightGrouped.Lookup(change.Key);

if (rightLookup.HasValue)
Expand All @@ -52,7 +52,7 @@ internal class InnerJoin<TLeft, TLeftKey, TRight, TRightKey, TDestination>(IObse
case ChangeReason.Update:
foreach (var keyvalue in rightLookup.Value.KeyValues)
{
joinedCache.AddOrUpdate(_resultSelector((change.Key, keyvalue.Key), leftCurent, keyvalue.Value), (change.Key, keyvalue.Key));
joinedCache.AddOrUpdate(_resultSelector((change.Key, keyvalue.Key), leftCurrent, keyvalue.Value), (change.Key, keyvalue.Key));
}

break;
Expand Down Expand Up @@ -117,6 +117,9 @@ internal class InnerJoin<TLeft, TLeftKey, TRight, TRightKey, TDestination>(IObse
return joinedCache.CaptureChanges();
});

return new CompositeDisposable(leftLoader.Merge(rightLoader).SubscribeSafe(observer), leftCache, rightCache, rightShare.Connect());
lock (locker)
{
return new CompositeDisposable(leftLoader.Merge(rightLoader).SubscribeSafe(observer), leftCache, rightCache, rightShare.Connect());
}
});
}
5 changes: 4 additions & 1 deletion src/DynamicData/Cache/Internal/LeftJoin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ public IObservable<IChangeSet<TDestination, TLeftKey>> Run() => Observable.Creat
return joined.CaptureChanges();
});

return new CompositeDisposable(leftLoader.Merge(rightLoader).SubscribeSafe(observer), leftCache, rightCache);
lock (locker)
{
return new CompositeDisposable(leftLoader.Merge(rightLoader).SubscribeSafe(observer), leftCache, rightCache);
}
});
}