Skip to content

Commit

Permalink
exit DispatchWasiEventLoop without polling if tasks have been canceled
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
dicej committed Aug 29, 2024
1 parent e989a63 commit 98bf4dc
Showing 1 changed file with 11 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ internal static class WasiEventLoop
// it will be leaked and stay in this list forever.
// it will also keep the Pollable handle alive and prevent it from being disposed
private static readonly List<PollableHolder> s_pollables = new();
private static bool s_tasksCanceled;

internal static Task RegisterWasiPollableHandle(int handle, CancellationToken cancellationToken)
{
Expand All @@ -35,6 +36,11 @@ internal static Task RegisterWasiPollable(Pollable pollable, CancellationToken c
internal static void DispatchWasiEventLoop()
{
ThreadPoolWorkQueue.Dispatch();
if (s_tasksCanceled)
{
s_tasksCanceled = false;
return;
}

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

// Tell event loop to exit early, giving the application a
// chance to quit if the task(s) it is interested in have
// completed.
s_tasksCanceled = true;

// it will be removed from s_pollables on the next run
self.isDisposed = true;
self.pollable.Dispose();
Expand Down

0 comments on commit 98bf4dc

Please sign in to comment.