File tree Expand file tree Collapse file tree 1 file changed +6
-1
lines changed Expand file tree Collapse file tree 1 file changed +6
-1
lines changed Original file line number Diff line number Diff line change @@ -61,7 +61,12 @@ void TaskScheduler_UnobservedTaskException(object? sender, UnobservedTaskExcepti
61
61
// Await for the continuation to actually run
62
62
_ = await tcs . Task ;
63
63
64
- // Some additional time to ensure the exception is propagated
64
+ // Wait for some additional time to ensure the exception is propagated. This is a bit counterintuitive, but the delay is
65
+ // not actually for the event to be raised, but to ensure the task that is throwing has had time to be scheduled and fail.
66
+ // The event is raised only when the exception wrapper inside that task is collected and its finalizer has run (that's where
67
+ // the logic to raise the event is executed), which is why we're then calling GC.Collect() and GC.WaitForPendingFinalizers().
68
+ // That is, we can't use a task completion source from that event, because that event is only guaranteed to actually be raised
69
+ // when the finalizer for that task run, which is why we're calling the GC after the delay here.
65
70
await Task . Delay ( 200 ) ;
66
71
67
72
GC . Collect ( ) ;
You can’t perform that action at this time.
0 commit comments