Skip to content

CTS.TryReset() concurrency issue #60182

@sakno

Description

@sakno

Description

A full description of the issue is here: #60180

Reproduction Steps

  1. Create CTS
  2. Call CancelAfter with very small timeout (1-10 milliseconds)
  3. Call TryReset concurrently with CancelAfter

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions