Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,16 @@ private static unsafe IList<int> BeforePollHook(object? state)
switch (socket->state.tag)
{
case tcp_socket_state_tag.TCP_SOCKET_STATE_CONNECTING:
case tcp_socket_state_tag.TCP_SOCKET_STATE_LISTENING:
pollableHandles.Add(EnsurePollable(SubscribeKindTcpSocket, socket->socket.handle, ref socket->socket_pollable.handle));
break;
case tcp_socket_state_tag.TCP_SOCKET_STATE_LISTENING:
// A listener with a queued connection stays ready until it's accepted, so only wait on it
// while an accept is pending. Otherwise it keeps every poll from blocking.
if (asyncContext._readQueue.IsWaiting)
{
pollableHandles.Add(EnsurePollable(SubscribeKindTcpSocket, socket->socket.handle, ref socket->socket_pollable.handle));
}
break;
case tcp_socket_state_tag.TCP_SOCKET_STATE_CONNECTED:
// Only wait on directions that have a pending operation. An idle connected socket's
// output stream is almost always writable, so always subscribing to it would keep
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ internal static class WasiEventLoop
private static bool s_checkScheduled;
private static Pollable? s_resolvedPollable;
private static Task? s_mainTask;
private static int s_consecutiveNonBlockingPolls;
private const int NonBlockingPollsBeforeFreshPollable = 64;

internal static Task RegisterWasiPollableHandle(int handle, bool ownsPollable, CancellationToken cancellationToken)
{
Expand Down Expand Up @@ -155,16 +157,33 @@ internal static void CheckPollables(object? _)
// if there is CPU-bound work to do, we should not block on PollInterop.Poll below
// so we will append pollable resolved in 0ms
// in effect, the PollInterop.Poll would not block us
Pollable? freshPollable = null;
if (ThreadPool.PendingWorkItemCount > 0 || (s_mainTask != null && s_mainTask.IsCompleted))
{
s_resolvedPollable ??= MonotonicClockInterop.SubscribeDuration(0);
resolvedPollableIndex = pending.Count;
pending.Add(s_resolvedPollable);
// wasmtime only processes host I/O readiness when a poll can't complete immediately, so an
// already-resolved pollable in every poll can keep socket readiness from ever being reported.
// Periodically use a new zero-duration pollable instead, which isn't ready on its first check.
if (++s_consecutiveNonBlockingPolls % NonBlockingPollsBeforeFreshPollable == 0)
{
freshPollable = MonotonicClockInterop.SubscribeDuration(0);
pending.Add(freshPollable);
}
else
{
s_resolvedPollable ??= MonotonicClockInterop.SubscribeDuration(0);
pending.Add(s_resolvedPollable);
}
}
else
{
s_consecutiveNonBlockingPolls = 0;
}

// this could block, this is blocking WASI API call
// FIXME: this will also block soft-debugger ability to pause the execution. Solutions: A) upgrade to WASIp3 B) register debugger connection's pollable
var readyIndexes = IPollImports.Poll(pending);
freshPollable?.Dispose();

var holdersCount = holders.Count;
for (int i = 0; i < readyIndexes.Length; i++)
Expand Down
Loading