Skip to content
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
16 changes: 12 additions & 4 deletions src/core/Akka/Actor/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Akka.Dispatch;
using Akka.Event;
using Akka.Routing;
using Akka.Util;
using ConfigurationFactory = Akka.Configuration.ConfigurationFactory;

namespace Akka.Actor
Expand All @@ -27,7 +28,7 @@ public class Settings
{
private readonly Config _userConfig;
//internal static readonly Config AkkaDllConfig = ConfigurationFactory.FromResource<Settings>("Akka.Configuration.Pigeon.conf");
private Config _fallbackConfig;
private readonly AtomicReference<Config> _fallbackConfig;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, this is a much simpler fix than what I was imagining


private void RebuildConfig()
{
Expand All @@ -42,13 +43,20 @@ private void RebuildConfig()
/// <summary>
/// Injects a system config at the top of the fallback chain
/// </summary>
/// <param name="config">TBD</param>
/// <param name="config">The latest config to be added to the front of the <see cref="Settings.Config"/> fallback chain</param>
public void InjectTopLevelFallback(Config config)
{
if (Config.Contains(config))
return;

_fallbackConfig = config.SafeWithFallback(_fallbackConfig);
while(true)
{
var oldConfig = _fallbackConfig.Value;
var newConfig = config.SafeWithFallback(oldConfig);
if (_fallbackConfig.CompareAndSet(oldConfig, newConfig))
break;
}
Comment on lines +52 to +58
Copy link
Contributor Author

@Arkatufus Arkatufus Jul 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix: Do not assign value to naked field directly, this can cause race condition if multiple Akka extensions running in multiple threads tries to start at the same time.

Scenario:

  • Time 0
    • Extension "A" reads _fallbackConfig into configA
    • Extension "B" reads _fallbackConfig into configB
  • Time 1
    • Extension "A" injects "akka.cluster.tools.singleton" into configA
    • Extension "B" injects "akka.persistence.sql" into configB
  • Time 2
    • Extension "A" writes configA into _fallbackConfig
  • Time 3
    • Extension "B" writes configB into _fallbackConfig
  • Time 4
    • Extension "A" reads "akka.cluster.tools.singleton" from Settings.Config, gets NullReferenceException because it got clobbered by Extension "B"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM


RebuildConfig();
}

Expand Down Expand Up @@ -77,7 +85,7 @@ public Settings(ActorSystem system, Config config, ActorSystemSetup setup)
{
Setup = setup;
_userConfig = config;
_fallbackConfig = ConfigurationFactory.Default();
_fallbackConfig = new AtomicReference<Config>(ConfigurationFactory.Default());
RebuildConfig();

System = system;
Expand Down
Loading