Skip to content

Commit 2b77a66

Browse files
authored
Use Task.Run and unwrap scheduler tasks (#2256)
1 parent f97276c commit 2b77a66

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/Proto.Actor/Mailbox/Dispatcher.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ public SynchronousDispatcher(int throughput = DefaultThroughput)
6565

6666
public int Throughput { get; }
6767

68-
public void Schedule(Func<Task> runner) => runner().Wait();
68+
// Surface original exceptions instead of wrapping in AggregateException
69+
public void Schedule(Func<Task> runner) => runner().GetAwaiter().GetResult();
6970
}
7071

7172
/// <summary>
@@ -80,7 +81,8 @@ public ThreadPoolDispatcher(int throughput = DefaultThroughput)
8081
Throughput = throughput;
8182
}
8283

83-
public void Schedule(Func<Task> runner) => Task.Factory.StartNew(runner, TaskCreationOptions.None);
84+
// Run on the thread pool and intentionally ignore the returned Task
85+
public void Schedule(Func<Task> runner) => _ = Task.Run(runner);
8486

8587
public int Throughput { get; set; }
8688
}
@@ -101,7 +103,8 @@ public CurrentSynchronizationContextDispatcher(int throughput = DefaultThroughpu
101103
}
102104

103105
public void Schedule(Func<Task> runner) =>
104-
Task.Factory.StartNew(runner, CancellationToken.None, TaskCreationOptions.None, _scheduler);
106+
// Use TaskScheduler for the current sync context and unwrap to avoid Task<Task>
107+
_ = Task.Factory.StartNew(runner, CancellationToken.None, TaskCreationOptions.None, _scheduler).Unwrap();
105108

106109
public int Throughput { get; }
107110
}

0 commit comments

Comments
 (0)