This repository was archived by the owner on Jan 23, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +14
-4
lines changed
src/System.Threading.Tasks
src/System/Threading/Tasks Expand file tree Collapse file tree 2 files changed +14
-4
lines changed Original file line number Diff line number Diff line change @@ -215,14 +215,16 @@ private static CancellationToken ExtractCancellationToken(Task task)
215
215
try
216
216
{
217
217
task . GetAwaiter ( ) . GetResult ( ) ;
218
+ Debug . Fail ( "Waiting on the canceled task should always result in an OCE, even if it's manufactured at the time of the wait." ) ;
219
+ return new CancellationToken ( true ) ;
218
220
}
219
221
catch ( OperationCanceledException oce )
220
222
{
221
- CancellationToken ct = oce . CancellationToken ;
222
- if ( ct . IsCancellationRequested )
223
- return ct ;
223
+ // This token may not have cancellation requested; that's ok.
224
+ // That can happen if, for example, the Task is canceled with
225
+ // TaskCompletionSource<T>.SetCanceled(), without a token.
226
+ return oce . CancellationToken ;
224
227
}
225
- return new CancellationToken ( true ) ;
226
228
}
227
229
228
230
/// <summary>Dummy type to use as a void TResult.</summary>
Original file line number Diff line number Diff line change @@ -470,6 +470,10 @@ public static IEnumerable<object[]> CompletedNonGenericTasks
470
470
yield return new object [ ] { Task . CompletedTask } ;
471
471
yield return new object [ ] { Task . FromCanceled ( CreateCanceledToken ( ) ) } ;
472
472
yield return new object [ ] { Task . FromException ( new FormatException ( ) ) } ;
473
+
474
+ var tcs = new TaskCompletionSource < int > ( ) ;
475
+ tcs . SetCanceled ( ) ; // cancel task without a token
476
+ yield return new object [ ] { tcs . Task } ;
473
477
}
474
478
}
475
479
@@ -481,6 +485,10 @@ public static IEnumerable<object[]> CompletedStringTasks
481
485
yield return new object [ ] { Task . FromResult ( "Tasks" ) } ;
482
486
yield return new object [ ] { Task . FromCanceled < string > ( CreateCanceledToken ( ) ) } ;
483
487
yield return new object [ ] { Task . FromException < string > ( new FormatException ( ) ) } ;
488
+
489
+ var tcs = new TaskCompletionSource < string > ( ) ;
490
+ tcs . SetCanceled ( ) ; // cancel task without a token
491
+ yield return new object [ ] { tcs . Task } ;
484
492
}
485
493
}
486
494
You can’t perform that action at this time.
0 commit comments