Skip to content

Commit 210a89c

Browse files
authored
Fix ActivityTagsCollection no-op in Reset() function (#138299)
The existing `((IEnumerator)_enumerator).Reset()` code creates a copy (it's a struct), and then resets the copy, which is discarded. To fix, we need to assign it back to `_enumerator`. The explicit cast to `IEnumerator` is required because Reset is an explicitly implemented function, not a part of the `List<>` API. So `_enumerator.Reset();` does not work. But casting alone creates a copy. Minimal reproduction project: [activitytags-enumerator-reset.zip](https://github.com/user-attachments/files/35424457/activitytags-enumerator-reset.zip) --- AI usage disclaimer: I encountered these .NET runtime bugs when using an AI to work on an unrelated project. I had it fix the bugs as it went, and now I am submitting fixes to each after manually reviewing the changes, and fixing them as needed. Most of these lines of code are AI-generated, including the tests and the MRP. The PR description and commit message are entirely human-written, as is always the case with every PR I open.
1 parent d07456f commit 210a89c

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

‎src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/ActivityTagsCollection.cs‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,14 @@ public struct Enumerator : IEnumerator<KeyValuePair<string, object?>>, IEnumerat
275275
object IEnumerator.Current => ((IEnumerator)_enumerator).Current;
276276
public void Dispose() => _enumerator.Dispose();
277277
public bool MoveNext() => _enumerator.MoveNext();
278-
void IEnumerator.Reset() => ((IEnumerator)_enumerator).Reset();
278+
void IEnumerator.Reset()
279+
{
280+
// Reset is explicit on List<T>.Enumerator, so this boxes a copy.
281+
// Copy it back to preserve the reset state.
282+
IEnumerator enumerator = _enumerator;
283+
enumerator.Reset();
284+
_enumerator = (List<KeyValuePair<string, object?>>.Enumerator)enumerator;
285+
}
279286
}
280287
}
281288
}

‎src/libraries/System.Diagnostics.DiagnosticSource/tests/ActivityTagsCollectionTests.cs‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Collections;
45
using System.Collections.Generic;
56
using System.Linq;
67
using System.Text;
@@ -158,6 +159,47 @@ public void TestRemove()
158159
Assert.True(tags.Remove(new KeyValuePair<string, object>("Key3", 3)));
159160
}
160161

162+
[Theory]
163+
[InlineData(0)]
164+
[InlineData(1)]
165+
[InlineData(2)]
166+
[InlineData(3)]
167+
public void TestEnumeratorReset(int advances)
168+
{
169+
var expected = new KeyValuePair<string, object>[]
170+
{
171+
new KeyValuePair<string, object>("first", 1),
172+
new KeyValuePair<string, object>("second", 2)
173+
};
174+
var tags = new ActivityTagsCollection(expected);
175+
IEnumerator enumerator = tags.GetEnumerator();
176+
for (int i = 0; i < advances; i++)
177+
{
178+
enumerator.MoveNext();
179+
}
180+
181+
for (int repeat = 0; repeat < 2; repeat++)
182+
{
183+
enumerator.Reset();
184+
foreach (KeyValuePair<string, object> item in expected)
185+
{
186+
Assert.True(enumerator.MoveNext());
187+
Assert.Equal(item, enumerator.Current);
188+
}
189+
Assert.False(enumerator.MoveNext());
190+
}
191+
}
192+
193+
[Fact]
194+
public void TestEnumeratorResetAfterModification()
195+
{
196+
var tags = new ActivityTagsCollection { { "first", 1 } };
197+
IEnumerator enumerator = tags.GetEnumerator();
198+
Assert.True(enumerator.MoveNext());
199+
tags.Add("second", 2);
200+
Assert.Throws<InvalidOperationException>(() => enumerator.Reset());
201+
}
202+
161203
[Fact]
162204
public void TestEnumeration()
163205
{

0 commit comments

Comments
 (0)