Skip to content

Commit

Permalink
[viosock]: Take care of recv requests stucks after socket shutdown
Browse files Browse the repository at this point in the history
When an other end (peer) of the socket reports a shutdown, the recv request queue is not processed. This means that some recv calls may get stuck forever. This commit changes the behavior -- when a shutdown from its peer is received, the recv queue gets processed and all requests that would get stuck are completed with STATUS_SUCCESS (which usually ends in receiving zero bytes).

Signed-off-by: Martin Drab <[email protected]>
  • Loading branch information
Martin Drab authored and YanVugenfirer committed Jan 30, 2024
1 parent e175594 commit 091c35f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
16 changes: 15 additions & 1 deletion viosock/sys/Rx.c
Original file line number Diff line number Diff line change
Expand Up @@ -1383,7 +1383,21 @@ VIOSockReadDequeueCb(
}
else
{
bRequeue = TRUE;
// The socket is connected, however, it might
// receive a shutdown notification from the other end.
// In such a case, we need to complete the recv request with success,
// possibly reporting zero data received. If the shutdown is here,
// the request must not be requeued since that may cause it being
// in the queue forever.
status = VIOSockStateValidate(pSocket, FALSE);
if (!NT_SUCCESS(status))
{
bStop = TRUE;
if (status == STATUS_LOCAL_DISCONNECT ||
status == STATUS_REMOTE_DISCONNECT)
status = STATUS_SUCCESS;
}
else bRequeue = TRUE;
}
}

Expand Down
14 changes: 12 additions & 2 deletions viosock/sys/Socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -1553,9 +1553,19 @@ VIOSockShutdownFromPeer(
pSocket->PeerShutdown |= uFlags;
bRes = (pSocket->PeerShutdown & VIRTIO_VSOCK_SHUTDOWN_MASK) == VIRTIO_VSOCK_SHUTDOWN_MASK;
WdfSpinLockRelease(pSocket->StateLock);
if (uDrain & VIRTIO_VSOCK_SHUTDOWN_SEND)
{
// Let's empty the recv() request queue. We need to
// complete pending requests for which there are no
// data. SInce VIOSockReadProcessDequeueCb may process
// one such request per call, the loop is needed.
//
// New recv() requests should not be an issue since they
// get removed by queue's Ready Notify callback.
while ((WdfIoQueueGetState(pSocket->ReadQueue, NULL, NULL) & WdfIoQueueNoRequests) == 0)
VIOSockReadProcessDequeueCb(pSocket);
}

// if (uDrain & VIRTIO_VSOCK_SHUTDOWN_SEND)
// VIOSockReadDequeueCb(pSocket, WDF_NO_HANDLE);
// if (uDrain & VIRTIO_VSOCK_SHUTDOWN_RCV)
// {
// //TODO: dequeue requests for current socket only
Expand Down

0 comments on commit 091c35f

Please sign in to comment.