Skip to content

Commit c1fd2a7

Browse files
committed
exit DispatchWasiEventLoop without polling if tasks have been canceled
If one or more tasks have been canceled during the call to `ThreadPoolWorkQueue.Dispatch`, one or more tasks of interest to the application may have completed, so we return control immediately without polling, allowing the app to exit if it chooses. A practical example of this is in the SharedLibrary smoke test. Without this patch, that test will take over 100 seconds to complete, whereas with this patch it completes in under a second. Signed-off-by: Joel Dice <[email protected]>
1 parent e989a63 commit c1fd2a7

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/libraries/System.Private.CoreLib/src/System/Threading/Wasi/WasiEventLoop.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ internal static class WasiEventLoop
1414
// it will be leaked and stay in this list forever.
1515
// it will also keep the Pollable handle alive and prevent it from being disposed
1616
private static readonly List<PollableHolder> s_pollables = new();
17+
private static bool s_tasksCanceled;
1718

1819
internal static Task RegisterWasiPollableHandle(int handle, CancellationToken cancellationToken)
1920
{
@@ -35,6 +36,11 @@ internal static Task RegisterWasiPollable(Pollable pollable, CancellationToken c
3536
internal static void DispatchWasiEventLoop()
3637
{
3738
ThreadPoolWorkQueue.Dispatch();
39+
if (s_tasksCanceled)
40+
{
41+
s_tasksCanceled = false;
42+
return;
43+
}
3844

3945
var holders = new List<PollableHolder>(s_pollables.Count);
4046
var pending = new List<Pollable>(s_pollables.Count);
@@ -114,6 +120,11 @@ private static void CancelAndDispose(object? s)
114120
return;
115121
}
116122

123+
// Tell event loop to call `ThreadPoolWorkQueue.Dispatch` again,
124+
// allowing any other tasks which might depend on this one to
125+
// make progress.
126+
s_tasksCanceled = true;
127+
117128
// it will be removed from s_pollables on the next run
118129
self.isDisposed = true;
119130
self.pollable.Dispose();

0 commit comments

Comments
 (0)