Skip to content

Commit 253caf8

Browse files
authored
Fixed potential torn reads in EventCounters (#26630)
1 parent d00a228 commit 253caf8

File tree

4 files changed

+20
-20
lines changed

4 files changed

+20
-20
lines changed

src/Hosting/Hosting/src/Internal/HostingEventSource.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,23 +87,23 @@ protected override void OnEventCommand(EventCommandEventArgs command)
8787
// This is the convention for initializing counters in the RuntimeEventSource (lazily on the first enable command).
8888
// They aren't disabled afterwards...
8989

90-
_requestsPerSecondCounter ??= new IncrementingPollingCounter("requests-per-second", this, () => _totalRequests)
90+
_requestsPerSecondCounter ??= new IncrementingPollingCounter("requests-per-second", this, () => Volatile.Read(ref _totalRequests))
9191
{
9292
DisplayName = "Request Rate",
9393
DisplayRateTimeScale = TimeSpan.FromSeconds(1)
9494
};
9595

96-
_totalRequestsCounter ??= new PollingCounter("total-requests", this, () => _totalRequests)
96+
_totalRequestsCounter ??= new PollingCounter("total-requests", this, () => Volatile.Read(ref _totalRequests))
9797
{
9898
DisplayName = "Total Requests",
9999
};
100100

101-
_currentRequestsCounter ??= new PollingCounter("current-requests", this, () => _currentRequests)
101+
_currentRequestsCounter ??= new PollingCounter("current-requests", this, () => Volatile.Read(ref _currentRequests))
102102
{
103103
DisplayName = "Current Requests"
104104
};
105105

106-
_failedRequestsCounter ??= new PollingCounter("failed-requests", this, () => _failedRequests)
106+
_failedRequestsCounter ??= new PollingCounter("failed-requests", this, () => Volatile.Read(ref _failedRequests))
107107
{
108108
DisplayName = "Failed Requests"
109109
};

src/Middleware/ConcurrencyLimiter/src/ConcurrencyLimiterEventSource.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,12 @@ protected override void OnEventCommand(EventCommandEventArgs command)
8787
{
8888
if (command.Command == EventCommand.Enable)
8989
{
90-
_rejectedRequestsCounter ??= new PollingCounter("requests-rejected", this, () => _rejectedRequests)
90+
_rejectedRequestsCounter ??= new PollingCounter("requests-rejected", this, () => Volatile.Read(ref _rejectedRequests))
9191
{
9292
DisplayName = "Rejected Requests",
9393
};
9494

95-
_queueLengthCounter ??= new PollingCounter("queue-length", this, () => _queueLength)
95+
_queueLengthCounter ??= new PollingCounter("queue-length", this, () => Volatile.Read(ref _queueLength))
9696
{
9797
DisplayName = "Queue Length",
9898
};

src/Servers/Kestrel/Core/src/Internal/Infrastructure/KestrelEventSource.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -318,54 +318,54 @@ protected override void OnEventCommand(EventCommandEventArgs command)
318318
// This is the convention for initializing counters in the RuntimeEventSource (lazily on the first enable command).
319319
// They aren't disabled afterwards...
320320

321-
_connectionsPerSecondCounter ??= new IncrementingPollingCounter("connections-per-second", this, () => _totalConnections)
321+
_connectionsPerSecondCounter ??= new IncrementingPollingCounter("connections-per-second", this, () => Volatile.Read(ref _totalConnections))
322322
{
323323
DisplayName = "Connection Rate",
324324
DisplayRateTimeScale = TimeSpan.FromSeconds(1)
325325
};
326326

327-
_totalConnectionsCounter ??= new PollingCounter("total-connections", this, () => _totalConnections)
327+
_totalConnectionsCounter ??= new PollingCounter("total-connections", this, () => Volatile.Read(ref _totalConnections))
328328
{
329329
DisplayName = "Total Connections",
330330
};
331331

332-
_tlsHandshakesPerSecondCounter ??= new IncrementingPollingCounter("tls-handshakes-per-second", this, () => _totalTlsHandshakes)
332+
_tlsHandshakesPerSecondCounter ??= new IncrementingPollingCounter("tls-handshakes-per-second", this, () => Volatile.Read(ref _totalTlsHandshakes))
333333
{
334334
DisplayName = "TLS Handshake Rate",
335335
DisplayRateTimeScale = TimeSpan.FromSeconds(1)
336336
};
337337

338-
_totalTlsHandshakesCounter ??= new PollingCounter("total-tls-handshakes", this, () => _totalTlsHandshakes)
338+
_totalTlsHandshakesCounter ??= new PollingCounter("total-tls-handshakes", this, () => Volatile.Read(ref _totalTlsHandshakes))
339339
{
340340
DisplayName = "Total TLS Handshakes",
341341
};
342342

343-
_currentTlsHandshakesCounter ??= new PollingCounter("current-tls-handshakes", this, () => _currentTlsHandshakes)
343+
_currentTlsHandshakesCounter ??= new PollingCounter("current-tls-handshakes", this, () => Volatile.Read(ref _currentTlsHandshakes))
344344
{
345345
DisplayName = "Current TLS Handshakes"
346346
};
347347

348-
_failedTlsHandshakesCounter ??= new PollingCounter("failed-tls-handshakes", this, () => _failedTlsHandshakes)
348+
_failedTlsHandshakesCounter ??= new PollingCounter("failed-tls-handshakes", this, () => Volatile.Read(ref _failedTlsHandshakes))
349349
{
350350
DisplayName = "Failed TLS Handshakes"
351351
};
352352

353-
_currentConnectionsCounter ??= new PollingCounter("current-connections", this, () => _currentConnections)
353+
_currentConnectionsCounter ??= new PollingCounter("current-connections", this, () => Volatile.Read(ref _currentConnections))
354354
{
355355
DisplayName = "Current Connections"
356356
};
357357

358-
_connectionQueueLengthCounter ??= new PollingCounter("connection-queue-length", this, () => _connectionQueueLength)
358+
_connectionQueueLengthCounter ??= new PollingCounter("connection-queue-length", this, () => Volatile.Read(ref _connectionQueueLength))
359359
{
360360
DisplayName = "Connection Queue Length"
361361
};
362362

363-
_httpRequestQueueLengthCounter ??= new PollingCounter("request-queue-length", this, () => _httpRequestQueueLength)
363+
_httpRequestQueueLengthCounter ??= new PollingCounter("request-queue-length", this, () => Volatile.Read(ref _httpRequestQueueLength))
364364
{
365365
DisplayName = "Request Queue Length"
366366
};
367367

368-
_currrentUpgradedHttpRequestsCounter ??= new PollingCounter("current-upgraded-requests", this, () => _currentUpgradedHttpRequests)
368+
_currrentUpgradedHttpRequestsCounter ??= new PollingCounter("current-upgraded-requests", this, () => Volatile.Read(ref _currentUpgradedHttpRequests))
369369
{
370370
DisplayName = "Current Upgraded Requests (WebSockets)"
371371
};

src/SignalR/common/Http.Connections/src/Internal/HttpConnectionsEventSource.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,19 @@ protected override void OnEventCommand(EventCommandEventArgs command)
9090
// This is the convention for initializing counters in the RuntimeEventSource (lazily on the first enable command).
9191
// They aren't disabled afterwards...
9292

93-
_connectionsStartedCounter ??= new PollingCounter("connections-started", this, () => _connectionsStarted)
93+
_connectionsStartedCounter ??= new PollingCounter("connections-started", this, () => Volatile.Read(ref _connectionsStarted))
9494
{
9595
DisplayName = "Total Connections Started",
9696
};
97-
_connectionsStoppedCounter ??= new PollingCounter("connections-stopped", this, () => _connectionsStopped)
97+
_connectionsStoppedCounter ??= new PollingCounter("connections-stopped", this, () => Volatile.Read(ref _connectionsStopped))
9898
{
9999
DisplayName = "Total Connections Stopped",
100100
};
101-
_connectionsTimedOutCounter ??= new PollingCounter("connections-timed-out", this, () => _connectionsTimedOut)
101+
_connectionsTimedOutCounter ??= new PollingCounter("connections-timed-out", this, () => Volatile.Read(ref _connectionsTimedOut))
102102
{
103103
DisplayName = "Total Connections Timed Out",
104104
};
105-
_currentConnectionsCounter ??= new PollingCounter("current-connections", this, () => _currentConnections)
105+
_currentConnectionsCounter ??= new PollingCounter("current-connections", this, () => Volatile.Read(ref _currentConnections))
106106
{
107107
DisplayName = "Current Connections",
108108
};

0 commit comments

Comments
 (0)