Skip to content

Commit cd1a88c

Browse files
stefano-garzarellaNipaLocal
authored andcommitted
vsock: avoid timeout during connect() if the socket is closing
When a peer attempts to establish a connection, vsock_connect() contains a loop that waits for the state to be TCP_ESTABLISHED. However, the other peer can be fast enough to accept the connection and close it immediately, thus moving the state to TCP_CLOSING. When this happens, the peer in the vsock_connect() is properly woken up, but since the state is not TCP_ESTABLISHED, it goes back to sleep until the timeout expires, returning -ETIMEDOUT. If the socket state is TCP_CLOSING, waiting for the timeout is pointless. vsock_connect() can return immediately without errors or delay since the connection actually happened. The socket will be in a closing state, but this is not an issue, and subsequent calls will fail as expected. We discovered this issue while developing a test that accepts and immediately closes connections to stress the transport switch between two connect() calls, where the first one was interrupted by a signal (see Closes link). Reported-by: Luigi Leonardi <[email protected]> Closes: https://lore.kernel.org/virtualization/bq6hxrolno2vmtqwcvb5bljfpb7mvwb3kohrvaed6auz5vxrfv@ijmd2f3grobn/ Fixes: d021c34 ("VSOCK: Introduce VM Sockets") Signed-off-by: Stefano Garzarella <[email protected]> Acked-by: Paolo Abeni <[email protected]> Tested-by: Luigi Leonardi <[email protected]> Reviewed-by: Luigi Leonardi <[email protected]> Signed-off-by: NipaLocal <nipa@local>
1 parent 9de39b5 commit cd1a88c

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

net/vmw_vsock/af_vsock.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1551,7 +1551,11 @@ static int vsock_connect(struct socket *sock, struct sockaddr *addr,
15511551
timeout = vsk->connect_timeout;
15521552
prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
15531553

1554-
while (sk->sk_state != TCP_ESTABLISHED && sk->sk_err == 0) {
1554+
/* If the socket is already closing or it is in an error state, there
1555+
* is no point in waiting.
1556+
*/
1557+
while (sk->sk_state != TCP_ESTABLISHED &&
1558+
sk->sk_state != TCP_CLOSING && sk->sk_err == 0) {
15551559
if (flags & O_NONBLOCK) {
15561560
/* If we're not going to block, we schedule a timeout
15571561
* function to generate a timeout on the connection

0 commit comments

Comments
 (0)