Skip to content

Commit 7dd31ee

Browse files
authored
Prevent uninitialised performance counters escaping CreatePerformanceCounter (#3623) (#3629)
1 parent 5aae5e1 commit 7dd31ee

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Diagnostics/SqlClientMetrics.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -489,25 +489,25 @@ private void RemovePerformanceCounters()
489489

490490
private PerformanceCounter? CreatePerformanceCounter(string counterName, PerformanceCounterType counterType)
491491
{
492-
PerformanceCounter? instance = null;
493-
494492
_instanceName ??= GetInstanceName();
495493
try
496494
{
497-
instance = new PerformanceCounter();
495+
PerformanceCounter instance = new();
498496
instance.CategoryName = PerformanceCounterCategoryName;
499497
instance.CounterName = counterName;
500498
instance.InstanceName = _instanceName;
501499
instance.InstanceLifetime = PerformanceCounterInstanceLifetime.Process;
502500
instance.ReadOnly = false;
503501
instance.RawValue = 0; // make sure we start out at zero
502+
503+
return instance;
504504
}
505505
catch (InvalidOperationException e)
506506
{
507507
ADP.TraceExceptionWithoutRethrow(e);
508-
}
509508

510-
return instance;
509+
return null;
510+
}
511511
}
512512

513513
// SxS: this method uses GetCurrentProcessId to construct the instance name.

0 commit comments

Comments
 (0)