Skip to content

Commit 3aaebeb

Browse files
authored
Disposed change token registration should not raise consumer (#79966)
1 parent 1b22d37 commit 3aaebeb

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/libraries/Microsoft.Extensions.Primitives/src/ChangeToken.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,12 @@ private void RegisterChangeTokenCallback(IChangeToken? token)
9898
{
9999
return;
100100
}
101-
102101
IDisposable registraton = token.RegisterChangeCallback(s => ((ChangeTokenRegistration<TState>?)s)!.OnChangeTokenFired(), this);
103-
102+
if (token.HasChanged && token.ActiveChangeCallbacks)
103+
{
104+
registraton?.Dispose();
105+
return;
106+
}
104107
SetDisposable(registraton);
105108
}
106109

src/libraries/Microsoft.Extensions.Primitives/tests/ChangeTokenTest.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,28 @@ public void DisposingChangeTokenRegistrationDoesNotRaiseConsumerCallbackStateOve
179179
Assert.Equal(5, count);
180180
}
181181

182+
[Fact]
183+
public void DisposingChangeTokenRegistrationDoesNotRaiseConsumerIfTokenProviderReturnsCancelledToken()
184+
{
185+
var provider = new ResettableChangeTokenProvider();
186+
Func<Func<IChangeToken>> changeTokenProviderFactory = () =>
187+
{
188+
int n = 0;
189+
return () =>
190+
{
191+
var token = provider.GetChangeToken();
192+
if (n++ is 0) provider.Changed();
193+
return token;
194+
};
195+
};
196+
int count = 0;
197+
var reg = ChangeToken.OnChange(changeTokenProviderFactory(), () => count++);
198+
reg.Dispose();
199+
provider.Changed();
200+
201+
Assert.Equal(1, count);
202+
}
203+
182204
[Fact]
183205
public void DisposingChangeTokenRegistrationDuringCallbackWorks()
184206
{

0 commit comments

Comments
 (0)