Description
Description
A full description of the issue is here: #60180
Reproduction Steps
- Create CTS
- Call
CancelAfter
with very small timeout (1-10 milliseconds) - Call
TryReset
concurrently withCancelAfter
Expected behavior
TryReset
should normally handle the concurrency with timer cancellation and return true/false.
Actual behavior
System.ObjectDisposedException : Cannot access a disposed object.
Stack Trace:
at System.Threading.TimerQueueTimer.Change(UInt32 dueTime, UInt32 period)
at System.Threading.CancellationTokenSource.TryReset()
Regression?
No, TryReset
is introduced in .NET 6.
Known Workarounds
Do not use TryReset
at all and re-create CTS every time when needed.
or
catch ObjectDisposedException
by the caller:
static bool TryResetSafe(CancellationTokenSource source)
{
try
{
return source.TryReset();
}
catch (ObjectDisposedException)
{
return false;
}
}
Configuration
The environment is not relevant. .NET SDK version is 6.0.100-rc.1.21458.32
Other information
No response