Re: [PATCH net v2] vsock: only undo SS_CONNECTING when connect() fails
From: Bobby Eshleman
Date: Mon Nov 30 2026 - 19:40:57 EST
On Thu, Nov 26, 2026 at 10:25:41AM +0100, Stefano Garzarella wrote:
> On Tue, Nov 17, 2026 at 05:34:09PM -0800, Bobby Eshleman wrote:
> > vsock_connect() sleeps with the socket unlocked. Meanwhile the transport
> > can complete the handshake, setting sock->state to SS_CONNECTED, and then
> > reset the connection, e.g. in vhost_vsock_reset_orphans(),
> > virtio_vsock_reset_sock() or vmci_transport_handle_detach(). When
> > connect() gets the lock back, it finds sk_err set and moves the socket
> > back to SS_UNCONNECTED. Userspace can then connect() the socket again,
> > and a connect() to a CID on another transport makes
> > vsock_assign_transport() release the transport the socket was connected
> > on.
> >
> > Only move the socket back to SS_UNCONNECTED if it is still
> > SS_CONNECTING. A socket that got connected stays SS_CONNECTED, and
> > another connect() fails with -EISCONN.
> >
> > Likewise, only move sk_state to TCP_CLOSE if it is still TCP_SYN_SENT.
> > Otherwise a second thread blocked in connect() on the same socket can
> > time out after the first one failed and called listen(), and overwrite
> > TCP_LISTEN.
> >
> > Fixes: d021c344051a ("VSOCK: Introduce VM Sockets")
> > Reported-by: Kyle Zeng <kylebot@xxxxxxxxxx>
> > Link:
> > https://lore.kernel.org/netdev/20260909-vsock-connect-reset-closing-v1-1-50298b9ccfbf@xxxxxxx/
> > Suggested-by: Michal Luczaj <mhal@xxxxxxx>
> > Signed-off-by: Bobby Eshleman <bobbyeshleman@xxxxxxxx>
> > ---
> > Changes in v2:
> > - Only reset sk_state if it is still TCP_SYN_SENT, so a concurrent
> > connect() caller that times out late doesn't overwrite TCP_LISTEN
> > (Sashiko).
> > - Drop Michal's Reviewed-by since the code changed.
> > - Link to v1: https://lore.kernel.org/r/20261116-vsock-connect-close-v1-1-43fb125973dd@xxxxxxxx
> > ---
> > net/vmw_vsock/af_vsock.c | 22 ++++++++++++++++------
> > 1 file changed, 16 insertions(+), 6 deletions(-)
> >
> > diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c
> > index 1d7d53542724..ce958b48295b 100644
> > --- a/net/vmw_vsock/af_vsock.c
> > +++ b/net/vmw_vsock/af_vsock.c
> > @@ -1704,6 +1704,19 @@ static void vsock_connect_timeout(struct work_struct *work)
> > sock_put(sk);
> > }
> >
> > +/* Avoid breaking the socket when another thread has changed its state while
> > + * we were asleep.
> > + */
>
> Should we make this comment a bit more clear to explain why we are doing
> this?
>
> Something like this (IIUC, please check and correct it)
>
> /* Undo only the state this connect() attempt set. While we slept, the
> * transport may have connected and then reset the socket (it is still in
> * vsock_connected_table, so it must not go back to SS_UNCONNECTED), or
> * another thread may have turned it into a listener.
> */
>
> The rest LGTM!
>
> Thanks,
> Stefano
Your wording is accurate, I'll just use it as is!
Best,
Bobby