Skip to content

fix(vsock): flow-control guest->host data with credit updates - #570

Open
jacobgorm wants to merge 1 commit into
google:mainfrom
jacobgorm:pipefix
Open

jacobgorm wants to merge 1 commit into
google:mainfrom
jacobgorm:pipefix

Conversation

@jacobgorm

Copy link
Copy Markdown

This is an AI-generated change to a problem I noticed when running an rlogin-style connection over vsock. The same code works on crosvm, but on Alioth the pipe stalls in the VM-to-client direction after having transmitted 8192 bytes in total, until I hit a key on my keyboard to trigger sending a byte back from client-to-VM, which unblocks it. This in on MacOS.

I put an AI on the task of figuring out what was wrong, and it came up with the fix in this PR. I am not familiar with the alioth code, but the fix looks reasonable to me, and it does solve my problem. I am using custom tools to trigger this, but IIRC socat should be able to reproduce it too.

Here is the AI's description of the problem and fix:

The UDS vsock device advanced fwd_cnt by the full packet length as soon as it received guest->host data, and never sent a CREDIT_UPDATE back. So the guest filled its send window (the buf_alloc advertised in RESPONSE) and then stalled: whoever had opened the connection saw the first buf_alloc bytes and nothing more. That is the classic virtio-vsock credit deadlock.

Buffer guest->host bytes in Connection::pending and only credit fwd_cnt for bytes the host socket actually accepted. The host side is a non-blocking UnixStream, so a full send buffer (a slow reader on the other end) is a normal, retryable event:

  • On WouldBlock, register WRITABLE interest and retry from the mio event loop once the socket drains (flush_conn_pending).
  • After each drain, send CREDIT_UPDATE with the current fwd_cnt so the guest reopens its window. A missing RX buffer only defers the update and never fails the data descriptor that triggered it.
  • On host hang-up, mark eof and let process_rx_data reset the connection, as before.

Because fwd_cnt does not advance for bytes still queued in pending, the guest's window stays shut while the host is slow, so pending is bounded to about one window and cannot grow without limit.

Update the connection test to offer an RX buffer for the credit update and assert on its op, ports, and fwd_cnt.

docs(vsock): clarify flow-control lifecycles and test contract

  • flush_conn_pending: document that it is only reached from a WRITABLE event and depends on the READABLE|WRITABLE interest set earlier in transfer_tx_data; a WouldBlock here keeps that interest, and calling it from a path that had not registered WRITABLE would deadlock.
  • transfer_tx_data: note that the first flush happens inline so the common (accepting socket) path needs no event round-trip.
  • send_to_tx test helper: state its one-RX-irq-per-TX assumption, so a future change that emits two RX messages for one TX does not silently leak a stale irq into the next call.

@google-cla

google-cla Bot commented Nov 25, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@Lencerf

Lencerf commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

Hi @jacobgorm , thanks for the contribution, please add the signed-off-by line in the commit message or just reply that in comments.

The UDS vsock device advanced fwd_cnt by the full packet length as soon
as it received guest->host data, and never sent a CREDIT_UPDATE back. So
the guest filled its send window (the buf_alloc advertised in RESPONSE)
and then stalled: whoever had opened the connection saw the first
buf_alloc bytes and nothing more. That is the classic virtio-vsock
credit deadlock.

Buffer guest->host bytes in Connection::pending and only credit fwd_cnt
for bytes the host socket actually accepted. The host side is a
non-blocking UnixStream, so a full send buffer (a slow reader on the
other end) is a normal, retryable event:

- On WouldBlock, register WRITABLE interest and retry from the mio event
  loop once the socket drains (flush_conn_pending).
- After each drain, send CREDIT_UPDATE with the current fwd_cnt so the
  guest reopens its window. A missing RX buffer only defers the update
  and never fails the data descriptor that triggered it.
- On host hang-up, mark eof and let process_rx_data reset the
  connection, as before.

Because fwd_cnt does not advance for bytes still queued in pending, the
guest's window stays shut while the host is slow, so pending is bounded
to about one window and cannot grow without limit.

Update the connection test to offer an RX buffer for the credit update
and assert on its op, ports, and fwd_cnt.

docs(vsock): clarify flow-control lifecycles and test contract

- flush_conn_pending: document that it is only reached from a WRITABLE
  event and depends on the READABLE|WRITABLE interest set earlier in
  transfer_tx_data; a WouldBlock here keeps that interest, and calling it
  from a path that had not registered WRITABLE would deadlock.
- transfer_tx_data: note that the first flush happens inline so the
  common (accepting socket) path needs no event round-trip.
- send_to_tx test helper: state its one-RX-irq-per-TX assumption, so a
  future change that emits two RX messages for one TX does not silently
  leak a stale irq into the next call.

Signed-off-by: Jacob Gorm Hansen <jacobgorm@gmail.com>
@jacobgorm

Copy link
Copy Markdown
Author

Hi @jacobgorm , thanks for the contribution, please add the signed-off-by line in the commit message or just reply that in comments.

hi, I've amened the commit :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants