This is an automated email from the ASF dual-hosted git repository.

blankensteiner pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar-dotpulsar.git


The following commit(s) were added to refs/heads/master by this push:
     new d8299ad  Fixed potential torn reads in EventCounters (#59)
d8299ad is described below

commit d8299ad32229194a6d4154c15312b45b456091d6
Author: Günther Foidl <[email protected]>
AuthorDate: Fri Oct 9 13:10:56 2020 +0200

    Fixed potential torn reads in EventCounters (#59)
    
    * Fixed potential torn reads in EventCounters
    
    The backing fields for the counters are `long`, so there was potential torn 
read on 32-bit platforms.
    `Volatile.Read` ensures that the value is read in a atomic way, so no torn 
reads can occur.
    
    * Use Interlocked.Read instead of Volatile.Read
    
    The consensus from the discussion in the dotnet/docs-repo is to use 
Interlocked.
---
 src/DotPulsar/Internal/DotPulsarEventSource.cs | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/src/DotPulsar/Internal/DotPulsarEventSource.cs 
b/src/DotPulsar/Internal/DotPulsarEventSource.cs
index 3042270..9f0e9d6 100644
--- a/src/DotPulsar/Internal/DotPulsarEventSource.cs
+++ b/src/DotPulsar/Internal/DotPulsarEventSource.cs
@@ -54,52 +54,52 @@ namespace DotPulsar.Internal
 
         public DotPulsarEventSource() : base("DotPulsar")
         {
-            _totalClientsCounter = new PollingCounter("total-clients", this, 
() => _totalClients)
+            _totalClientsCounter = new PollingCounter("total-clients", this, 
() => Interlocked.Read(ref _totalClients))
             {
                 DisplayName = "Total number of clients"
             };
 
-            _currentClientsCounter = new PollingCounter("current-clients", 
this, () => _currentClients)
+            _currentClientsCounter = new PollingCounter("current-clients", 
this, () => Interlocked.Read(ref _currentClients))
             {
                 DisplayName = "Current number of clients"
             };
 
-            _totalConnectionsCounter = new PollingCounter("total-connections", 
this, () => _totalConnections)
+            _totalConnectionsCounter = new PollingCounter("total-connections", 
this, () => Interlocked.Read(ref _totalConnections))
             {
                 DisplayName = "Total number of connections"
             };
 
-            _currentConnectionsCounter = new 
PollingCounter("current-connections", this, () => _currentConnections)
+            _currentConnectionsCounter = new 
PollingCounter("current-connections", this, () => Interlocked.Read(ref 
_currentConnections))
             {
                 DisplayName = "Current number of connections"
             };
 
-            _totalConsumersCounter = new PollingCounter("total-consumers", 
this, () => _totalConsumers)
+            _totalConsumersCounter = new PollingCounter("total-consumers", 
this, () => Interlocked.Read(ref _totalConsumers))
             {
                 DisplayName = "Total number of consumers"
             };
 
-            _currentConsumersCounter = new PollingCounter("current-consumers", 
this, () => _currentConsumers)
+            _currentConsumersCounter = new PollingCounter("current-consumers", 
this, () => Interlocked.Read(ref _currentConsumers))
             {
                 DisplayName = "Current number of consumers"
             };
 
-            _totalProducersCounter = new PollingCounter("total-producers", 
this, () => _totalProducers)
+            _totalProducersCounter = new PollingCounter("total-producers", 
this, () => Interlocked.Read(ref _totalProducers))
             {
                 DisplayName = "Total number of producers"
             };
 
-            _currentProducersCounter = new PollingCounter("current-producers", 
this, () => _currentProducers)
+            _currentProducersCounter = new PollingCounter("current-producers", 
this, () => Interlocked.Read(ref _currentProducers))
             {
                 DisplayName = "Current number of producers"
             };
 
-            _totalReadersCounter = new PollingCounter("total-readers", this, 
() => _totalReaders)
+            _totalReadersCounter = new PollingCounter("total-readers", this, 
() => Interlocked.Read(ref _totalReaders))
             {
                 DisplayName = "Total number of readers"
             };
 
-            _currentReadersCounter = new PollingCounter("current-readers", 
this, () => _currentReaders)
+            _currentReadersCounter = new PollingCounter("current-readers", 
this, () => Interlocked.Read(ref _currentReaders))
             {
                 DisplayName = "Current number of readers"
             };

Reply via email to