Re: [PATCH net] vsock/virtio: Fix sock leak on close_timeout race with peer close

From: Hu Xinyao

Date: Tue Dec 01 2026 - 22:30:00 EST


On Tue, Dec 01, 2026 at 06:05:30PM +0100, Stefano Garzarella wrote:
> >diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c
> >index 1e762a4..9dade6e 100644
> >--- a/net/vmw_vsock/virtio_transport_common.c
> >+++ b/net/vmw_vsock/virtio_transport_common.c
> >@@ -1411,10 +1411,10 @@ static void virtio_transport_close_timeout(struct work_struct *work)
> > (void)virtio_transport_reset(vsk, NULL);
> >
> > virtio_transport_do_close(vsk, false);
> >+ } else {
> >+ virtio_transport_cancel_close_work(vsk, false);
> > }
> >
> >- vsk->close_work_scheduled = false;
>
> Why we are removing this?

Hi Stefano,

With the else branch added, both branches now call
virtio_transport_cancel_close_work(vsk, false) (the !SOCK_DONE branch
via virtio_transport_do_close(vsk, false), and the SOCK_DONE branch
directly).

In virtio_transport_cancel_close_work(vsk, false), !cancel_timeout is
unconditionally true, so whenever vsk->close_work_scheduled is set it
already clears vsk->close_work_scheduled = false (along with calling
virtio_transport_remove_sock(vsk) and sock_put(sk)), and if it was
already false it remains false. Previously, the trailing assignment only
had an effect when SOCK_DONE was already set and do_close() was skipped,
clearing the flag without releasing the socket reference or removing the
socket.

Sorry for not spelling that out in the commit message (since
virtio_transport_cancel_close_work() is outside the diff context).
Alternatively, we could avoid the else branch and just replace
"vsk->close_work_scheduled = false;" with an unconditional
"virtio_transport_cancel_close_work(vsk, false);" after the if
(!sock_flag(sk, SOCK_DONE)) block, which is a no-op if do_close()
already ran. Happy to send a v2 whichever way you prefer.

Thanks,
Hu Xinyao