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

Fix PersistenceQuery ReadJournalFor thread safety issue #6859

Merged
merged 2 commits into from
Jul 31, 2023
Merged
Changes from all commits
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
15 changes: 13 additions & 2 deletions src/core/Akka.Persistence.Query/PersistenceQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public sealed class PersistenceQuery : IExtension
private readonly ExtendedActorSystem _system;
private readonly ConcurrentDictionary<string, IReadJournal> _readJournalPluginExtensionIds = new();
private ILoggingAdapter _log;
private readonly object _lock = new ();

public static PersistenceQuery Get(ActorSystem system)
{
Expand All @@ -34,8 +35,18 @@ public PersistenceQuery(ExtendedActorSystem system)

public TJournal ReadJournalFor<TJournal>(string readJournalPluginId) where TJournal : IReadJournal
{
var plugin = _readJournalPluginExtensionIds.GetOrAdd(readJournalPluginId, path => CreatePlugin(path, GetDefaultConfig<TJournal>()).GetReadJournal());
return (TJournal)plugin;
if(_readJournalPluginExtensionIds.TryGetValue(readJournalPluginId, out var plugin))
return (TJournal)plugin;

lock (_lock)
{
if (_readJournalPluginExtensionIds.TryGetValue(readJournalPluginId, out plugin))
return (TJournal)plugin;

plugin = CreatePlugin(readJournalPluginId, GetDefaultConfig<TJournal>()).GetReadJournal();
_readJournalPluginExtensionIds[readJournalPluginId] = plugin;
return (TJournal)plugin;
}
}

private IReadJournalProvider CreatePlugin(string configPath, Config config)
Expand Down