Skip to content

Commit d8c59a4

Browse files
authored
MeterListener.Dispose always disables measurements (#102661)
Fix #102465
1 parent 0cc0791 commit d8c59a4

2 files changed

Lines changed: 63 additions & 3 deletions

File tree

src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Metrics/MeterListener.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,14 +258,17 @@ public void Dispose()
258258
s_allStartedListeners.Remove(this);
259259

260260
DiagNode<Instrument>? current = _enabledMeasurementInstruments.First;
261-
if (current is not null && measurementsCompleted is not null)
261+
if (current is not null)
262262
{
263-
callbacksArguments = new Dictionary<Instrument, object?>();
263+
if (measurementsCompleted is not null)
264+
{
265+
callbacksArguments = new Dictionary<Instrument, object?>();
266+
}
264267

265268
do
266269
{
267270
object? state = current.Value.DisableMeasurements(this);
268-
callbacksArguments.Add(current.Value, state);
271+
callbacksArguments?.Add(current.Value, state);
269272
current = current.Next;
270273
} while (current is not null);
271274

src/libraries/System.Diagnostics.DiagnosticSource/tests/MetricsTests.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,63 @@ public void ListenerDisposalsTest()
923923
}).Dispose();
924924
}
925925

926+
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
927+
public void ListenerWithoutMeasurementsCompletedDisposalsTest()
928+
{
929+
RemoteExecutor.Invoke(() => {
930+
Meter meter = new Meter("MinimalListenerDisposalsTest");
931+
932+
Counter<int> counter = meter.CreateCounter<int>("Counter");
933+
UpDownCounter<short> upDownCounter = meter.CreateUpDownCounter<short>("upDownCounter");
934+
Histogram<double> histogram = meter.CreateHistogram<double>("Histogram");
935+
ObservableCounter<long> observableCounter = meter.CreateObservableCounter("ObservableCounter", () => new Measurement<long>(10, new KeyValuePair<string, object?>[] { new KeyValuePair<string, object?>("Key", "value")}));
936+
ObservableGauge<decimal> observableGauge = meter.CreateObservableGauge("ObservableGauge", () => new Measurement<decimal>(5.7m, new KeyValuePair<string, object?>[] { new KeyValuePair<string, object?>("Key", "value")}));
937+
ObservableUpDownCounter<float> observableUpDownCounter = meter.CreateObservableUpDownCounter("ObservableUpDownCounter", () => new Measurement<float>(-5.7f, new KeyValuePair<string, object?>[] { new KeyValuePair<string, object?>("Key", "value")}));
938+
939+
MeterListener listener = new MeterListener();
940+
listener.InstrumentPublished = (theInstrument, theListener) => theListener.EnableMeasurementEvents(theInstrument, theInstrument);
941+
942+
int count = 0;
943+
944+
listener.SetMeasurementEventCallback<short>((inst, measurement, tags, state) => count++);
945+
listener.SetMeasurementEventCallback<int>((inst, measurement, tags, state) => count++);
946+
listener.SetMeasurementEventCallback<float>((inst, measurement, tags, state) => count++);
947+
listener.SetMeasurementEventCallback<double>((inst, measurement, tags, state) => count++);
948+
listener.SetMeasurementEventCallback<long>((inst, measurement, tags, state) => count++);
949+
listener.SetMeasurementEventCallback<decimal>((inst, measurement, tags, state) => count++);
950+
951+
listener.Start();
952+
953+
Assert.Equal(0, count);
954+
955+
counter.Add(1);
956+
Assert.Equal(1, count);
957+
958+
upDownCounter.Add(-1);
959+
Assert.Equal(2, count);
960+
961+
histogram.Record(1);
962+
Assert.Equal(3, count);
963+
964+
listener.RecordObservableInstruments();
965+
Assert.Equal(6, count);
966+
967+
listener.Dispose();
968+
969+
counter.Add(1);
970+
Assert.Equal(6, count);
971+
972+
upDownCounter.Add(-1);
973+
Assert.Equal(6, count);
974+
975+
histogram.Record(1);
976+
Assert.Equal(6, count);
977+
978+
listener.RecordObservableInstruments();
979+
Assert.Equal(6, count);
980+
}).Dispose();
981+
}
982+
926983
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
927984
public void MultipleListenersTest()
928985
{

0 commit comments

Comments
 (0)