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 address terminated topic atomic updates (#4306) #4307

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
30 changes: 12 additions & 18 deletions src/core/Akka/Event/AddressTerminatedTopic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using Akka.Actor;
using Akka.Util;

Expand Down Expand Up @@ -35,7 +37,7 @@ public override AddressTerminatedTopic CreateExtension(ExtendedActorSystem syste
/// </summary>
internal sealed class AddressTerminatedTopic : IExtension
{
private readonly AtomicReference<HashSet<IActorRef>> _subscribers = new AtomicReference<HashSet<IActorRef>>(new HashSet<IActorRef>());
Copy link
Member

Choose a reason for hiding this comment

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

Another ancient bug - looks like we tried to get too cute by half here. I agree that a lock should be a relatively simple fix for this.

private readonly HashSet<IActorRef> _subscribers = new HashSet<IActorRef>();

/// <summary>
/// Retrieves the extension from the specified actor system.
Expand All @@ -53,13 +55,8 @@ public static AddressTerminatedTopic Get(ActorSystem system)
/// <param name="subscriber">The actor that is registering for notifications.</param>
public void Subscribe(IActorRef subscriber)
{
while (true)
{
var current = _subscribers;
if (!_subscribers.CompareAndSet(current, new HashSet<IActorRef>(current.Value) {subscriber}))
continue;
break;
}
lock (_subscribers)
_subscribers.Add(subscriber);
}

/// <summary>
Expand All @@ -68,15 +65,8 @@ public void Subscribe(IActorRef subscriber)
/// <param name="subscriber">The actor that is unregistering for notifications.</param>
public void Unsubscribe(IActorRef subscriber)
{
while (true)
{
var current = _subscribers;
var newSet = new HashSet<IActorRef>(_subscribers.Value);
newSet.Remove(subscriber);
if (!_subscribers.CompareAndSet(current, newSet))
continue;
break;
}
lock (_subscribers)
_subscribers.Remove(subscriber);
}

/// <summary>
Expand All @@ -85,7 +75,11 @@ public void Unsubscribe(IActorRef subscriber)
/// <param name="msg">The message that is sent to all subscribers.</param>
public void Publish(AddressTerminated msg)
{
foreach (var subscriber in _subscribers.Value)
List<IActorRef> subscribers;
lock(_subscribers)
subscribers = _subscribers.ToList();

foreach (var subscriber in subscribers)
{
subscriber.Tell(msg, ActorRefs.NoSender);
}
Expand Down