vsock: avoid timeout during connect() if the socket is closing
Subsystems (4)
Stefano Garzarella
maintainerPATCH 1/1 vsock: avoid timeout during connect() if the socket is closing
From: Stefano Garzarella <[email protected]>
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).
--- net/vmw_vsock/af_vsock.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
net/vmw_vsock/af_vsock.c +5 -1
@@ -1551,7 +1551,11 @@ static int vsock_connect(struct socket *sock, struct sockaddr *addr, timeout = vsk->connect_timeout;prepare_to_wait (sk_sleep (sk), &wait,TASK_INTERRUPTIBLE ); -while (sk->sk_state !=TCP_ESTABLISHED && sk->sk_err ==0 ) { +/* If the socket is already closing or it is in an error state, there +* is no point in waiting. +*/ +while (sk->sk_state !=TCP_ESTABLISHED && + sk->sk_state !=TCP_CLOSING && sk->sk_err ==0 ) {if (flags &O_NONBLOCK ) {/* If we're not going to block, we schedule a timeout * function to generate a timeout on the connection
Signature · 1 line
2.49.0
Quoted 21 lines · Stefano Garzarella
From: Stefano Garzarella <[email protected]>
When a peer attempts to establish a connection,
vsock_connect()contains a loop that waits for the state to beTCP_ESTABLISHED. However, the other peer can be fast enough to accept the connection and close it immediately, thus moving the state toTCP_CLOSING.When this happens, the peer in the
vsock_connect()is properly woken up, but since the state is notTCP_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).
Quoted 4 lines
Reported-by Luigi Leonardi <[email protected]>Closes https://lore.kernel.org/virtualization/bq6hxrolno2vmtqwcvb5bljfpb7mvwb3kohrvaed6auz5vxrfv@ijmd2f3grobn/ open hereFixes d021c344051a ("VSOCK: Introduce VM Sockets")Signed-off-by Stefano Garzarella <[email protected]>
Quoted 49 lines · Stefano Garzarella
From: Stefano Garzarella <[email protected]>
When a peer attempts to establish a connection,
vsock_connect()contains a loop that waits for the state to beTCP_ESTABLISHED. However, the other peer can be fast enough to accept the connection and close it immediately, thus moving the state toTCP_CLOSING.When this happens, the peer in the
vsock_connect()is properly woken up, but since the state is notTCP_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/ open hereFixes d021c344051a ("VSOCK: Introduce VM Sockets")Signed-off-by Stefano Garzarella <[email protected]>---
net/vmw_vsock/af_vsock.c| 6 +++++-1 file changed, 5 insertions(+), 1 deletion(-)net/vmw_vsock/af_vsock.c +5
-1@@ -1551,7 +1551,11 @@ static int vsock_connect(struct socket *sock, struct sockaddr *addr, timeout = vsk->connect_timeout;prepare_to_wait (sk_sleep (sk), &wait,TASK_INTERRUPTIBLE ); -while (sk->sk_state !=TCP_ESTABLISHED && sk->sk_err ==0 ) { +/* If the socket is already closing or it is in an error state, there +* is no point in waiting. +*/ +while (sk->sk_state !=TCP_ESTABLISHED && + sk->sk_state !=TCP_CLOSING && sk->sk_err ==0 ) {if (flags &O_NONBLOCK ) {/* If we're not going to block, we schedule a timeout * function to generate a timeout on the connection Signature · 1 line
2.49.0
Just tested and fixes the issue! Thanks Stefano!
This patch was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This patch was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:
Quoted 8 lines · On Fri, 28 Mar 2025 15:15:28 +0100 you wrote:
From: Stefano Garzarella <[email protected]>
When a peer attempts to establish a connection,
vsock_connect()contains a loop that waits for the state to beTCP_ESTABLISHED. However, the other peer can be fast enough to accept the connection and close it immediately, thus moving the state toTCP_CLOSING.[...]
Here is the summary with links:
- [net] vsock: avoid timeout during connect() if the socket is closing
https://git.kernel.org/netdev/net/c/fccd2b711d96
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html