Cysharp / Cysharp/UniTask

Calling `ToCancellationToken` on completed task throws `ObjectDisposedException`

Open Beginner friendly
#724 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
11.2k
Forks
1k
PR merge metrics
No merged PRs in 30d

Description

When calling ToCancellationToken on a completed task, an exception occurred

UniTask.CompletedTask.ToCancellationToken();
// System.ObjectDisposedException: The CancellationTokenSource has been disposed.

The source code of ToCancellationToken is

var cts = new CancellationTokenSource();
Core(task, cts).Forget();
return cts.Token;

If the task is completed, cts will be disposed synchronously in Core() and then accessing cts.Token causes ObjectDisposeException

Is this a bug or by design?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the ToCancellationToken implementation shown in the issue and reproduce it with UniTask.CompletedTask. Trace the synchronous completion and disposal path, then verify that calling ToCancellationToken on a completed task returns a usable token without throwing.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.