From 8c53bd3ccf668698c472b475603ff8f3ea42d300 Mon Sep 17 00:00:00 2001 From: Andrey Kornich Date: Thu, 10 Mar 2022 19:49:30 -0800 Subject: [PATCH 1/4] fix: ref #623 --- .../Infrastructure/RollbarQueueController.cs | 18 +++++++- Rollbar/RollbarInfrastructure.cs | 42 ++++++++++++------- .../Telemetry/RollbarTelemetryCollector.cs | 19 ++++++--- SdkCommon.csproj | 6 +-- 4 files changed, 61 insertions(+), 24 deletions(-) diff --git a/Rollbar/Infrastructure/RollbarQueueController.cs b/Rollbar/Infrastructure/RollbarQueueController.cs index 35c59ba4..bc7d1fc5 100644 --- a/Rollbar/Infrastructure/RollbarQueueController.cs +++ b/Rollbar/Infrastructure/RollbarQueueController.cs @@ -41,6 +41,11 @@ internal sealed class RollbarQueueController { #region singleton implementation + //private static RollbarQueueController _singleton; + + private static readonly Lazy lazy = + new Lazy(() => new RollbarQueueController()); + /// /// Gets the instance. /// @@ -49,7 +54,15 @@ public static RollbarQueueController? Instance { get { - return NestedSingleInstance.TheInstance; + //return NestedSingleInstance.TheInstance; + + //if (_singleton == null) + //{ + // _singleton = new RollbarQueueController(); + //} + //return _singleton; + + return RollbarInfrastructure.Instance.IsInitialized ? lazy.Value : null; } } @@ -85,7 +98,8 @@ private NestedSingleInstance() /// /// The trace source /// - private static readonly TraceSource traceSource = new TraceSource(typeof(RollbarQueueController).FullName ?? "RollbarQueueController"); + private static readonly TraceSource traceSource = + new TraceSource(typeof(RollbarQueueController).FullName ?? "RollbarQueueController"); /// /// Enum PayloadTraceSources diff --git a/Rollbar/RollbarInfrastructure.cs b/Rollbar/RollbarInfrastructure.cs index 533d79c1..9ce23f28 100644 --- a/Rollbar/RollbarInfrastructure.cs +++ b/Rollbar/RollbarInfrastructure.cs @@ -24,18 +24,25 @@ public class RollbarInfrastructure : IRollbarInfrastructure , IDisposable { - private static readonly TraceSource traceSource = new TraceSource(typeof(RollbarInfrastructure).FullName ?? "RollbarInfrastructure"); + private static readonly TraceSource traceSource = + new TraceSource(typeof(RollbarInfrastructure).FullName ?? "RollbarInfrastructure"); - internal readonly TimeSpan _sleepInterval = TimeSpan.FromMilliseconds(25); + internal readonly TimeSpan _sleepInterval = + TimeSpan.FromMilliseconds(25); private readonly object _syncLock = new object(); - private bool _isInitialized = false; + private bool _initializedOnce = false; private IRollbarInfrastructureConfig? _config; #region singleton implementation + //private static RollbarInfrastructure _singleton; + + private static readonly Lazy lazy = + new Lazy(() => new RollbarInfrastructure()); + /// /// Gets the instance. /// @@ -44,7 +51,15 @@ public static RollbarInfrastructure Instance { get { - return NestedSingleInstance.TheInstance; + //return NestedSingleInstance.TheInstance; + + //if (_singleton == null) + //{ + // _singleton = new RollbarQueueController(); + //} + //return _singleton; + + return lazy.Value; } } @@ -85,7 +100,7 @@ public bool IsInitialized { get { - return this._isInitialized; + return this._initializedOnce; } } @@ -153,7 +168,7 @@ public void Init(IRollbarInfrastructureConfig config) lock(this._syncLock) { - if(this._isInitialized) + if(this._initializedOnce) { string msg = $"{typeof(RollbarInfrastructure).Name} can not be initialized more than once!"; traceSource.TraceInformation(msg); @@ -163,16 +178,15 @@ public void Init(IRollbarInfrastructureConfig config) ); } - this._config = config; - this.ValidateConfiguration(); - this._config.Reconfigured += _config_Reconfigured; - - this._isInitialized = true; - // now, since the basic infrastructure seems to be good and initialized, // let's initialize all the dependent services of the infrastructure: try { + this._config = config; + this.ValidateConfiguration(); + this._config.Reconfigured += _config_Reconfigured; + this._initializedOnce = true; + RollbarQueueController.Instance!.Init(config); RollbarTelemetryCollector.Instance!.Init(config.RollbarTelemetryOptions); // NOTE: RollbarConfig @@ -183,7 +197,7 @@ public void Init(IRollbarInfrastructureConfig config) } catch(Exception ex) { - this._isInitialized = false; + this._initializedOnce = false; throw new RollbarException( InternalRollbarError.InfrastructureError, @@ -197,7 +211,7 @@ public void Init(IRollbarInfrastructureConfig config) private void ValidateConfiguration() { - if(this._isInitialized) + if(this._initializedOnce) { return; } diff --git a/Rollbar/Telemetry/RollbarTelemetryCollector.cs b/Rollbar/Telemetry/RollbarTelemetryCollector.cs index b2ae7072..017e5f74 100644 --- a/Rollbar/Telemetry/RollbarTelemetryCollector.cs +++ b/Rollbar/Telemetry/RollbarTelemetryCollector.cs @@ -16,20 +16,29 @@ internal class RollbarTelemetryCollector { private static readonly TraceSource traceSource = new TraceSource(typeof(RollbarTelemetryCollector).FullName ?? "RollbarTelemetryCollector"); - + #region singleton implementation + private static readonly Lazy lazy = + new Lazy(() => new RollbarTelemetryCollector()); + /// /// Gets the instance. /// - /// - /// The instance. - /// + /// The instance. public static RollbarTelemetryCollector? Instance { get { - return NestedSingleInstance.TheInstance; + //return NestedSingleInstance.TheInstance; + + //if (_singleton == null) + //{ + // _singleton = new RollbarQueueController(); + //} + //return _singleton; + + return RollbarInfrastructure.Instance.IsInitialized ? lazy.Value : null; } } diff --git a/SdkCommon.csproj b/SdkCommon.csproj index 161afa07..6786317d 100644 --- a/SdkCommon.csproj +++ b/SdkCommon.csproj @@ -24,14 +24,14 @@ - 5.1.1 + 5.1.2 false - - fix: resolve GH #615 - ConnectivityMonitor tests wrong thing + fix: resolve #623 - NullReference exception while initializing RollbarInfrastructure - +