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

[BUG] Thread safety in ObservableCache.Connect (regression) #538

Closed
pmg23 opened this issue Dec 17, 2021 · 3 comments · Fixed by #539
Closed

[BUG] Thread safety in ObservableCache.Connect (regression) #538

pmg23 opened this issue Dec 17, 2021 · 3 comments · Fixed by #539

Comments

@pmg23
Copy link
Contributor

pmg23 commented Dec 17, 2021

The thread safety of ObservableCache.Connect (previously fixed in #377) is broken again by #472.

It is imperative to hold the lock on _locker while fetching the initial changeset and subscribing to changes. Otherwise any changes made from another thread between the point when, for a new subscription, InternalEx.Return computes the initial state, and later the Concat impl subscribes to _changes, will be lost.

public IObservable<IChangeSet<TObject, TKey>> Connect(Func<TObject, bool>? predicate = null) =>
Observable.Create<IChangeSet<TObject, TKey>>(
observer =>
{
var initial = InternalEx.Return(() =>
{
// lock getting initial changes and rely on a combination of Concat
// + _changes being synchronized to produce thread safety (I hope!)
lock (_locker)
{
return (IChangeSet<TObject, TKey>)GetInitialUpdates(predicate);
}
});
var changes = Observable.Defer(() => initial).Concat(_changes);
if (predicate != null)
{
changes = changes.Filter(predicate);
}
else
{
changes = changes.NotEmpty();
}
return changes.SubscribeSafe(observer);
});

(Observed in 7.4.3, but should affect 7.1.16 onwards.)

@RolandPheasant
Copy link
Collaborator

Hopefully the PR will fix it.

glennawatson added a commit that referenced this issue Dec 18, 2021
Co-authored-by: Glenn <5834289+glennawatson@users.noreply.github.com>
@pmg23
Copy link
Contributor Author

pmg23 commented Dec 20, 2021

Looks good. Thanks for the fast turnaround.

@github-actions
Copy link

github-actions bot commented Jan 4, 2022

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 4, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants