[master] 151f054d5 vtcp, vus: Consistently establish blocking connections

Dridi Boukelmoune dridi.boukelmoune at gmail.com
Wed Sep 3 13:28:08 UTC 2025


commit 151f054d528fbeafb074fccb0a2dcf6b04cdac66
Author: Stephane Cance <stephane.cance at varnish-software.com>
Date:   Thu Aug 14 16:21:14 2025 +0200

    vtcp,vus: Consistently establish blocking connections
    
    Establishing a connection is divided in two steps: initiating the
    connection in a non-blocking fashion and checking the connected state
    of the socket with VTCP_connected().
    
    In asynchronous mode, the caller is responsible for the second step,
    calling VTCP_connected() that will restore the blocking state.
    
    A synchronous connection involving a timeout will take care of the
    second step internally, while an indefinite synchronous connection will
    simply avoid the non-blocking detour altogether.
    
    In the timeout case, the first step would yield a non-blocking socket in
    the event of an immediate success. The connection becomes unreliable
    until the next explicit change to either blocking or non-blocking state,
    opening the door to unexpected ill effects like loops making blocking
    operations turning into tight EAGAIN loops.
    
    Refs #4203

diff --git a/lib/libvarnish/vtcp.c b/lib/libvarnish/vtcp.c
index d3aba491c..b15492117 100644
--- a/lib/libvarnish/vtcp.c
+++ b/lib/libvarnish/vtcp.c
@@ -298,6 +298,8 @@ VTCP_connect(const struct suckaddr *name, int msec)
 	AZ(setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &val, sizeof val));
 
 	i = connect(s, sa, sl);
+	if (i == 0 && msec > 0)
+		VTCP_blocking(s);
 	if (i == 0)
 		return (s);
 	if (errno != EINPROGRESS) {
diff --git a/lib/libvarnish/vus.c b/lib/libvarnish/vus.c
index 01187442f..f7711d864 100644
--- a/lib/libvarnish/vus.c
+++ b/lib/libvarnish/vus.c
@@ -160,6 +160,8 @@ VUS_connect(const char *path, int msec)
 		VTCP_nonblocking(s);
 
 	i = connect(s, (const void*)&uds, sl);
+	if (i == 0 && msec > 0)
+		VTCP_blocking(s);
 	if (i == 0)
 		return (s);
 	if (errno != EINPROGRESS) {


More information about the varnish-commit mailing list