Skip to content

fix concurrent collection modification issue #62

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

Merged
merged 1 commit into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion Analytics-CSharp/Analytics-CSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Coroutine.NET" Version="1.3.0" />
<PackageReference Include="Serialization.NET" Version="1.3.0" />
<PackageReference Include="Serialization.NET" Version="1.4.0" />
<PackageReference Include="Sovran.NET" Version="1.3.0" />
</ItemGroup>
<ItemGroup>
Expand Down
3 changes: 2 additions & 1 deletion Analytics-CSharp/Segment/Analytics/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Segment.Analytics.Policies;
using Segment.Analytics.Utilities;
using Segment.Concurrent;
using Segment.Serialization;

namespace Segment.Analytics
{
Expand Down Expand Up @@ -94,7 +95,7 @@ public Configuration(string writeKey,
AnalyticsErrorHandler = analyticsErrorHandler;
StorageProvider = storageProvider ?? new DefaultStorageProvider();
HttpClientProvider = httpClientProvider ?? new DefaultHTTPClientProvider();
FlushPolicies = flushPolicies ?? new List<IFlushPolicy>();
FlushPolicies = flushPolicies == null ? new ConcurrentList<IFlushPolicy>() : new ConcurrentList<IFlushPolicy>(flushPolicies);
if (FlushPolicies.Count == 0)
{
FlushPolicies.Add(new CountFlushPolicy(flushAt));
Expand Down
15 changes: 8 additions & 7 deletions Analytics-CSharp/Segment/Analytics/Timeline.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Concurrent;
using System.Reflection;
using global::System;
using global::System.Collections.Generic;
Expand All @@ -12,15 +13,15 @@ namespace Segment.Analytics
/// </summary>
public class Timeline
{
internal Dictionary<PluginType, Mediator> _plugins;
internal IDictionary<PluginType, Mediator> _plugins;

public Timeline() => _plugins = new Dictionary<PluginType, Mediator>()
public Timeline() => _plugins = new ConcurrentDictionary<PluginType, Mediator>
{
{ PluginType.Before, new Mediator() },
{ PluginType.Enrichment, new Mediator() },
{ PluginType.Destination, new Mediator() },
{ PluginType.After, new Mediator() },
{ PluginType.Utility, new Mediator() }
[PluginType.Before] = new Mediator(),
[PluginType.Enrichment] = new Mediator(),
[PluginType.Destination] = new Mediator(),
[PluginType.After] = new Mediator(),
[PluginType.Utility] = new Mediator()
};

/// <summary>
Expand Down
1 change: 0 additions & 1 deletion Analytics-CSharp/Segment/Analytics/Utilities/UserPrefs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,6 @@ private long CommitToMemory()
if (!copyToDisk.ContainsKey(k))
{
continue;
;
}

copyToDisk.Remove(k);
Expand Down
8 changes: 5 additions & 3 deletions Samples/UnitySample/LifecyclePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ public void OnError(Exception error)
/// </summary>
public class Lifecycle : Singleton<Lifecycle>, IObservable<Lifecycle.State>
{
private readonly List<IObserver<State>> _observers = new List<IObserver<State>>();
// use Segment's ConcurrentList to avoid modification during enumeration
// or you have to make a copy for iterating the observers.
private readonly IList<IObserver<State>> _observers = new ConcurrentList<IObserver<State>>();

private const string AppVersionKey = "app_version";

Expand Down Expand Up @@ -122,10 +124,10 @@ public IDisposable Subscribe(IObserver<State> observer)

private class Unsubscriber : IDisposable
{
private List<IObserver<State>> _observers;
private IList<IObserver<State>> _observers;
private IObserver<State> _observer;

public Unsubscriber(List<IObserver<State>> observers, IObserver<State> observer)
public Unsubscriber(IList<IObserver<State>> observers, IObserver<State> observer)
{
_observers = observers;
_observer = observer;
Expand Down
6 changes: 3 additions & 3 deletions Samples/UnitySample/UnitySample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="Coroutine.NET, Version=1.1.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\Coroutine.NET.1.1.0\lib\netstandard2.0\Coroutine.NET.dll</HintPath>
<HintPath>..\..\packages\Coroutine.NET.1.3.0\lib\netstandard2.0\Coroutine.NET.dll</HintPath>
</Reference>
<Reference Include="Serialization.NET">
<HintPath>..\..\..\..\.nuget\packages\serialization.net\1.2.0\lib\netstandard2.0\Serialization.NET.dll</HintPath>
<HintPath>..\..\..\..\.nuget\packages\serialization.net\1.4.0\lib\netstandard2.0\Serialization.NET.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand All @@ -59,7 +59,7 @@
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
Expand Down