Skip to content

Commit b3b0286

Browse files
authored
Merge pull request #272 from timothycoleman/fix-cancellation-token-multiplexer-pooling
Fix CancellationTokenMultiplexer return to pool
2 parents 8813788 + 3306785 commit b3b0286

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/DotNext.Tests/Threading/CancellationTokenMultiplexerTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,23 @@ public static void CheckPooling()
4040
}
4141
}
4242

43+
[Fact]
44+
public async Task CheckPoolingNonInterference()
45+
{
46+
var multiplexer = new CancellationTokenMultiplexer();
47+
48+
using var cts = new CancellationTokenSource();
49+
50+
await multiplexer.Combine([cts.Token, cts.Token, cts.Token]).DisposeAsync();
51+
52+
// same source is reused from pool, but should now not be associated with cts.
53+
await using var combined = multiplexer.Combine([new(), new(), new()]);
54+
55+
cts.Cancel();
56+
57+
False(combined.Token.IsCancellationRequested);
58+
}
59+
4360
[Fact]
4461
public static void ExtraListOverflow()
4562
{

src/DotNext.Threading/Threading/CancellationTokenMultiplexer.CTS.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ private void Add(CancellationTokenRegistration registration)
3737
private ref CancellationTokenRegistration FirstInlinedRegistration
3838
=> ref Unsafe.As<InlinedTokenList, CancellationTokenRegistration>(ref inlinedList);
3939

40-
public int Count => inlinedTokenCount + extraTokens?.Count ?? 0;
40+
public int Count => inlinedTokenCount + (extraTokens?.Count ?? 0);
4141

4242
public ref CancellationTokenRegistration this[int index]
4343
{

0 commit comments

Comments
 (0)