Commit 6bc31a9f24 for openssl.org

commit 6bc31a9f24e030fdc3632fbf5303d7958b5d216f
Author: Alexandr Nedvedicky <sashan@openssl.org>
Date:   Tue Nov 24 10:46:15 2026 +0100

    quicapitest: failed handshake makes test quic server stuck

    currently the server thread assumes the execution can
    alwasy make a progres and is willing to keep the thread
    running indefinitely.

    This change allows tick_server() function to indicate
    failure so the server loop can break out of the infinite loop.

    The author of the fix is Mounir Idrassi.

    Fixes: 33338
    Assisted-by: Codex:gpt-6-astra
    Reviewed-by: Nikola Pajkovsky <nikolap@openssl.org>
    Reviewed-by: Andrew Dinh <andrewd@openssl.org>
    Reviewed-by: Mounir Idrassi <mounir.idrassi@idrix.fr>
    Merge-date: Thu Dec  3 10:18:13 2026
    Merged-from: https://github.com/openssl/openssl/pull/33947

diff --git a/test/helpers/quictestlib.c b/test/helpers/quictestlib.c
index 6df31c7283..36857f3d98 100644
--- a/test/helpers/quictestlib.c
+++ b/test/helpers/quictestlib.c
@@ -20,6 +20,7 @@
 #include "internal/quic_ssl.h"
 #include "internal/quic_channel.h"
 #include "internal/quic_port.h"
+#include "internal/quic_reactor.h"
 #include "internal/statem.h"
 #include "internal/quic_wire_pkt.h"
 #include "internal/quic_record_tx.h"
@@ -79,12 +80,6 @@ struct qtest_fault {
     struct noise_args_data_st noiseargs;
 };

-#if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
-static int client_ready = 0;
-static CRYPTO_CONDVAR *client_ready_cond = NULL;
-static CRYPTO_MUTEX *client_ready_mutex = NULL;
-#endif
-
 static void packet_plain_finish(void *arg);
 static void handshake_finish(void *arg);
 static void fault_on_new_channel(SSL *conn, QUIC_CHANNEL *ch, void *arg);
@@ -156,23 +151,6 @@ static int create_quic_objects(OSSL_LIB_CTX *libctx, SSL_CTX *clientctx,
     int own_bdata = 1;
     SSL_CTX *sctx = NULL;

-#if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
-    if (client_ready_cond == NULL) {
-        client_ready_cond = ossl_crypto_condvar_new();
-        if (client_ready_cond == NULL)
-            return 0;
-    }
-
-    if (client_ready_mutex == NULL) {
-        client_ready_mutex = ossl_crypto_mutex_new();
-        if (client_ready_mutex == NULL) {
-            ossl_crypto_condvar_free(&client_ready_cond);
-            client_ready_cond = NULL;
-            return 0;
-        }
-    }
-#endif
-
     bdata = OPENSSL_zalloc(sizeof(QTEST_DATA));
     if (bdata == NULL)
         return 0;
@@ -558,27 +536,6 @@ int qtest_supports_blocking(void)

 #define MAXLOOPS 1000

-#if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
-static int globserverret = 0;
-static TSAN_QUALIFIER int abortserverthread = 0;
-static QUIC_TSERVER *globtserv;
-static SSL *globlistener;
-static SSL **globsconn;
-static const thread_t thread_zero;
-static int create_quic_connection(QUIC_TSERVER *qtserv, SSL *listener,
-    SSL *clientssl, SSL **sconn, int wanterr);
-static void run_server_thread(void)
-{
-    /*
-     * This will operate in a busy loop because the server does not block,
-     * but should be acceptable because it is local and we expect this to be
-     * fast
-     */
-    globserverret = create_quic_connection(globtserv, globlistener, NULL,
-        globsconn, SSL_ERROR_NONE);
-}
-#endif
-
 static int wait_for_timeout(SSL *s, QUIC_TSERVER *qtserv, SSL *listener)
 {
     struct timeval tv;
@@ -643,172 +600,249 @@ int qtest_wait_for_timeout_ssl(SSL *s, SSL *listener)
     return wait_for_timeout(s, NULL, listener);
 }

-static void tick_server(QUIC_TSERVER *qtserv, SSL *listener)
+static int tick_server(QUIC_TSERVER *qtserv, SSL *listener, SSL **sconn)
 {
+    uint64_t events;
+
     if (qtserv != NULL)
-        ossl_quic_tserver_tick(qtserv);
-    else
-        (void)SSL_handle_events(listener);
+        return ossl_quic_tserver_tick(qtserv);
+
+    if (!SSL_handle_events(listener))
+        return 0;
+    if (*sconn == NULL)
+        *sconn = SSL_accept_connection(listener, SSL_ACCEPT_CONNECTION_NO_BLOCK);
+
+    /* An empty accept queue and a failed port can both give us NULL. */
+    if (!ossl_quic_conn_poll_events(listener, SSL_POLL_EVENT_EL, 0, &events))
+        return 0;
+    if ((events & SSL_POLL_EVENT_EL) != 0) {
+        TEST_info("QUIC listener failed during handshake");
+        return 0;
+    }
+    return 1;
 }

-/* Both servers NULL means the server is ticked by another thread */
-static int create_quic_connection(QUIC_TSERVER *qtserv, SSL *listener,
-    SSL *clientssl, SSL **sconn, int wanterr)
+/* The caller is the only thread driving the server. */
+static int server_handshake_status(QUIC_TSERVER *qtserv, SSL **sconn)
 {
-    int retc = -1, rets = 0, abortctr = 0, ret = 0;
-    int clienterr = 0, servererr = 0;
-#if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
+    QUIC_CHANNEL *ch;

-    /*
-     * Pointless initialisation to avoid bogus compiler warnings about using
-     * t uninitialised
-     */
-    thread_t t = thread_zero;
+    if (qtserv != NULL)
+        ch = ossl_quic_tserver_get_channel(qtserv);
+    else if (*sconn == NULL)
+        return 0;
+    else
+        ch = ossl_quic_conn_get_channel(*sconn);

-    if (clientssl != NULL)
-        abortserverthread = 0;
+    if (ossl_quic_channel_is_term_any(ch))
+        return -1;
+    return ossl_quic_channel_is_handshake_confirmed(ch);
+}

-    /*
-     * Only set the client_ready flag to zero if we are the client
-     */
-    if (clientssl != NULL) {
-        ossl_crypto_mutex_lock(client_ready_mutex);
-        client_ready = 0;
-        ossl_crypto_mutex_unlock(client_ready_mutex);
+static int connect_client(SSL *clientssl, int wanterr, int *paused)
+{
+    int ret = SSL_connect(clientssl);
+    int err;
+
+    if (ret > 0)
+        return 1;
+    err = SSL_get_error(clientssl, ret);
+    if (err == wanterr) {
+        *paused = 1;
+        return 1;
     }
-#endif
+    if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE)
+        return 0;
+    TEST_info("SSL_connect() failed %d, %d", ret, err);
+    TEST_openssl_errors();
+    return -1;
+}

-    if (!TEST_true(qtserv != NULL || listener != NULL)) {
-        goto err;
-    } else if (clientssl == NULL) {
-        retc = 1;
-    } else if (SSL_get_blocking_mode(clientssl) > 0) {
 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
-        /*
-         * clientssl is blocking. We will need a thread to complete the
-         * connection
-         */
-        globtserv = qtserv;
-        globlistener = listener;
-        globsconn = sconn;
-        if (!TEST_true(run_thread(&t, run_server_thread)))
-            goto err;
+#define BLOCKING_HANDSHAKE_TIMEOUT_MS 10000
+#define BLOCKING_HANDSHAKE_POLL_MS 25

-        qtserv = NULL;
-        listener = NULL;
-        rets = 1;
-#else
-        TEST_error("No thread support in this build");
-        goto err;
-#endif
-    }
+struct blocking_handshake {
+    QUIC_TSERVER *qtserv;
+    SSL *listener;
+    SSL **sconn;
+    QUIC_CHANNEL *client;
+    OSSL_TIME deadline, tick_deadline;
+    CRYPTO_MUTEX *mutex; /* Protects stop and server_failed. */
+    int stop, server_failed;
+    int client_aborted; /* Protected by the client engine mutex. */
+    void (*tick_cb)(QUIC_TICK_RESULT *res, void *arg, uint32_t flags);
+    void *tick_arg;
+};

-    do {
-        if (!clienterr && retc <= 0) {
-            int err;
+static CRYPTO_THREAD_RETVAL run_server_thread(void *arg)
+{
+    struct blocking_handshake *h = arg;
+    int ret = 0;

-#if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
-            ossl_crypto_mutex_lock(client_ready_mutex);
-            client_ready = 1;
-            ossl_crypto_condvar_broadcast(client_ready_cond);
-            ossl_crypto_mutex_unlock(client_ready_mutex);
-#endif
-            retc = SSL_connect(clientssl);
-            if (retc <= 0) {
-                err = SSL_get_error(clientssl, retc);
+    for (;;) {
+        int stop, status;

-                if (err == wanterr) {
-                    retc = 1;
-#if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
-                    if (qtserv == NULL && listener == NULL && rets > 0)
-                        tsan_store(&abortserverthread, 1);
-                    else
-#endif
-                        rets = 1;
-                } else {
-                    if (err != SSL_ERROR_WANT_READ
-                        && err != SSL_ERROR_WANT_WRITE) {
-                        TEST_info("SSL_connect() failed %d, %d", retc, err);
-                        TEST_openssl_errors();
-                        clienterr = 1;
-                    }
-                }
-            }
+        if (ossl_time_compare(ossl_time_now(), h->deadline) >= 0) {
+            TEST_info("QUIC test server handshake timed out");
+            break;
         }
-
         qtest_add_time(1);
-        if (clientssl != NULL)
-            SSL_handle_events(clientssl);
-        if (qtserv != NULL || listener != NULL) {
-#if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
-            ossl_crypto_mutex_lock(client_ready_mutex);
-            for (;;) {
-                if (client_ready == 1)
-                    break;
-                ossl_crypto_condvar_wait(client_ready_cond, client_ready_mutex);
-            }
-            ossl_crypto_mutex_unlock(client_ready_mutex);
-#endif
-            tick_server(qtserv, listener);
-            if (listener != NULL && *sconn == NULL)
-                *sconn = SSL_accept_connection(listener, 0);
-        }
+        if (!tick_server(h->qtserv, h->listener, h->sconn))
+            break;
+        status = server_handshake_status(h->qtserv, h->sconn);
+        if (status < 0)
+            break;

-        if (!servererr && rets <= 0) {
-            if (qtserv != NULL) {
-                servererr = ossl_quic_tserver_is_term_any(qtserv);
-                if (!servererr)
-                    rets = ossl_quic_tserver_is_handshake_confirmed(qtserv);
-            } else if (listener != NULL && *sconn != NULL) {
-                QUIC_CHANNEL *ch = ossl_quic_conn_get_channel(*sconn);
-
-                servererr = ossl_quic_channel_is_term_any(ch);
-                if (!servererr)
-                    rets = ossl_quic_channel_is_handshake_confirmed(ch);
-            }
+        /* Finish this tick and accept before returning a paused connection. */
+        ossl_crypto_mutex_lock(h->mutex);
+        stop = h->stop;
+        ossl_crypto_mutex_unlock(h->mutex);
+        if (stop || status > 0) {
+            ret = 1;
+            break;
         }
+        OSSL_sleep(1);
+    }

-        if (clienterr && servererr)
-            goto err;
+    if (ret == 0) {
+        ossl_crypto_mutex_lock(h->mutex);
+        h->server_failed = 1;
+        ossl_crypto_mutex_unlock(h->mutex);
+    }
+    OPENSSL_thread_stop();
+    return ret;
+}

-        if (clientssl != NULL && ++abortctr == MAXLOOPS) {
-            TEST_info("No progress made");
-            goto err;
-        }
+/* Keep SSL_connect() blocking but check the server result between OS waits. */
+static void tick_blocking_client(QUIC_TICK_RESULT *res, void *arg,
+    uint32_t flags)
+{
+    struct blocking_handshake *h = arg;
+    OSSL_TIME now = ossl_time_now();
+    OSSL_TIME next_check = ossl_time_add(now,
+        ossl_ms2time(BLOCKING_HANDSHAKE_POLL_MS));
+    int failed;
+
+    ossl_crypto_mutex_lock(h->mutex);
+    failed = h->server_failed;
+    ossl_crypto_mutex_unlock(h->mutex);
+    /* The reactor already holds the client engine lock here. */
+    if (failed || ossl_time_compare(now, h->deadline) >= 0) {
+        h->client_aborted = 1;
+        ossl_quic_channel_local_close(h->client, 0,
+            failed ? "QUIC test server handshake failed"
+                   : "QUIC test client handshake timed out");
+    }
+
+    h->tick_cb(res, h->tick_arg, flags);
+    h->tick_deadline = res->tick_deadline;
+    res->tick_deadline = ossl_time_min(res->tick_deadline,
+        ossl_time_min(h->deadline, next_check));
+}

-        if ((retc <= 0 && !clienterr) || (rets <= 0 && !servererr)) {
-            if (!wait_for_timeout(clientssl, qtserv, listener))
-                goto err;
+static int create_quic_connection_blocking(QUIC_TSERVER *qtserv, SSL *listener,
+    SSL *clientssl, SSL **sconn, int wanterr)
+{
+    struct blocking_handshake h = { 0 };
+    QUIC_REACTOR *reactor;
+    CRYPTO_THREAD *worker;
+    CRYPTO_THREAD_RETVAL server_ok;
+    int ret = 0, paused = 0, i;
+
+    h.qtserv = qtserv;
+    h.listener = listener;
+    h.sconn = sconn;
+    if (!TEST_ptr(h.client = ossl_quic_conn_get_channel(clientssl))
+        || !TEST_ptr(h.mutex = ossl_crypto_mutex_new()))
+        return 0;
+    h.deadline = ossl_time_add(ossl_time_now(),
+        ossl_ms2time(BLOCKING_HANDSHAKE_TIMEOUT_MS));
+    reactor = ossl_quic_channel_get_reactor(h.client);
+    worker = ossl_crypto_thread_native_start(run_server_thread, &h, 1);
+    if (!TEST_ptr(worker)) {
+        ossl_crypto_mutex_free(&h.mutex);
+        return 0;
+    }
+
+    ossl_crypto_mutex_lock(reactor->mutex);
+    h.tick_cb = reactor->tick_cb;
+    h.tick_arg = reactor->tick_cb_arg;
+    h.tick_deadline = reactor->tick_deadline;
+    reactor->tick_cb = tick_blocking_client;
+    reactor->tick_cb_arg = &h;
+    ossl_crypto_mutex_unlock(reactor->mutex);
+
+    for (i = 0; i < MAXLOOPS && ret == 0; ++i) {
+        ret = connect_client(clientssl, wanterr, &paused);
+        if (ret >= 0) {
+            qtest_add_time(1);
+            if (!SSL_handle_events(clientssl))
+                ret = -1;
         }
-    } while ((retc <= 0 && !clienterr)
-        || (rets <= 0 && !servererr
-#if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
-            && !tsan_load(&abortserverthread)
+    }
+    if (ret == 0)
+        TEST_info("No blocking handshake progress made");
+
+    ossl_crypto_mutex_lock(reactor->mutex);
+    if (h.client_aborted)
+        ret = -1;
+    reactor->tick_cb = h.tick_cb;
+    reactor->tick_cb_arg = h.tick_arg;
+    reactor->tick_deadline = h.tick_deadline;
+    ossl_crypto_mutex_unlock(reactor->mutex);
+
+    if (ret <= 0 || paused) {
+        ossl_crypto_mutex_lock(h.mutex);
+        h.stop = 1;
+        ossl_crypto_mutex_unlock(h.mutex);
+    }
+
+    /* The worker still borrows the listener and |sconn| until joined. */
+    if (!ossl_crypto_thread_native_join(worker, &server_ok))
+        OPENSSL_die("Couldn't join QUIC test server thread", __FILE__, __LINE__);
+    ossl_crypto_thread_native_clean(worker);
+    ossl_crypto_mutex_free(&h.mutex);
+    return ret > 0 && server_ok;
+}
 #endif
-                ));

-    if (qtserv == NULL && listener == NULL && rets > 0) {
+static int create_quic_connection(QUIC_TSERVER *qtserv, SSL *listener,
+    SSL *clientssl, SSL **sconn, int wanterr)
+{
+    int retc = 0, rets = 0, paused = 0, i;
+
+    if (!TEST_ptr(clientssl))
+        return 0;
+    if (SSL_get_blocking_mode(clientssl) > 0) {
 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
-        /*
-         * Make sure we unblock the server before we wait on completion here
-         * in case it didn't happen in the connect loop above
-         */
-        ossl_crypto_mutex_lock(client_ready_mutex);
-        client_ready = 1;
-        ossl_crypto_condvar_broadcast(client_ready_cond);
-        ossl_crypto_mutex_unlock(client_ready_mutex);
-        if (!TEST_true(wait_for_thread(t)) || !TEST_true(globserverret))
-            goto err;
+        return create_quic_connection_blocking(qtserv, listener, clientssl,
+            sconn, wanterr);
 #else
-        TEST_error("Should not happen");
-        goto err;
+        TEST_error("No thread support in this build");
+        return 0;
 #endif
     }

-    if (!clienterr && !servererr)
-        ret = 1;
-err:
-    return ret;
+    for (i = 0; i < MAXLOOPS; ++i) {
+        if (retc == 0)
+            retc = connect_client(clientssl, wanterr, &paused);
+        qtest_add_time(1);
+        if (!SSL_handle_events(clientssl)
+            || !tick_server(qtserv, listener, sconn))
+            return 0;
+        /* An expected client error can end the handshake before the server. */
+        if (paused)
+            return rets >= 0;
+        if (rets == 0)
+            rets = server_handshake_status(qtserv, sconn);
+        if (retc != 0 && rets != 0)
+            return retc > 0 && rets > 0;
+        if (!wait_for_timeout(clientssl, qtserv, listener))
+            return 0;
+    }
+    TEST_info("No progress made");
+    return 0;
 }

 int qtest_create_quic_connection_ex(QUIC_TSERVER *qtserv, SSL *clientssl,
@@ -842,6 +876,9 @@ int qtest_create_quic_connection_ssl(SSL *listener, SSL *clientssl,
 }

 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
+static QUIC_TSERVER *globtserv;
+static SSL *globlistener;
+static const thread_t thread_zero;
 static TSAN_QUALIFIER int shutdowndone;

 static void run_server_shutdown_thread(void)
@@ -851,8 +888,10 @@ static void run_server_shutdown_thread(void)
      * but should be acceptable because it is local and we expect this to be
      * fast
      */
+    SSL *sconn = NULL;
+
     do {
-        tick_server(globtserv, globlistener);
+        (void)tick_server(globtserv, globlistener, &sconn);
     } while (!tsan_load(&shutdowndone));
 }
 #endif
@@ -868,11 +907,6 @@ static int shutdown_client(QUIC_TSERVER *qtserv, SSL *listener, SSL *clientssl)
      * t uninitialised
      */
     thread_t t = thread_zero;
-
-    ossl_crypto_condvar_free(&client_ready_cond);
-    client_ready_cond = NULL;
-    ossl_crypto_mutex_free(&client_ready_mutex);
-    client_ready_mutex = NULL;
 #endif

     if (SSL_get_blocking_mode(clientssl) > 0) {
@@ -906,8 +940,11 @@ static int shutdown_client(QUIC_TSERVER *qtserv, SSL *listener, SSL *clientssl)
         if (rc < 0)
             break;

-        if (tickserver)
-            tick_server(qtserv, listener);
+        if (tickserver) {
+            SSL *sconn = NULL;
+
+            (void)tick_server(qtserv, listener, &sconn);
+        }
     }

 #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
diff --git a/test/helpers/quictestlib.h b/test/helpers/quictestlib.h
index 04c045f6d0..5395d4979b 100644
--- a/test/helpers/quictestlib.h
+++ b/test/helpers/quictestlib.h
@@ -100,7 +100,9 @@ int qtest_create_quic_connection(QUIC_TSERVER *qtserv, SSL *clientssl);
  * @brief Run the handshake between the client and a listener.
  * @param listener listener
  * @param clientssl client
- * @param sconn receives the accepted server connection; caller frees it
+ * @param sconn set *sconn to NULL on the first call. Keep the returned server
+ *              SSL in that slot when resuming the same handshake. The caller
+ *              frees it even if the helper returns failure.
  * @returns 1 on success, 0 on failure
  */
 int qtest_create_quic_connection_ssl(SSL *listener, SSL *clientssl,
@@ -132,7 +134,7 @@ int qtest_create_quic_connection_ex(QUIC_TSERVER *qtserv, SSL *clientssl,
  * client reports SSL_get_error() value |wanterr|.
  * @param listener listener
  * @param clientssl client
- * @param sconn receives the accepted server connection; caller frees it
+ * @param sconn same as for qtest_create_quic_connection_ssl()
  * @param wanterr SSL_ERROR_* value to stop at
  * @returns 1 on success, 0 on failure
  */
diff --git a/test/quicapitest.c b/test/quicapitest.c
index c4100b1f97..f741f3406f 100644
--- a/test/quicapitest.c
+++ b/test/quicapitest.c
@@ -1369,6 +1369,52 @@ err:
     return testresult;
 }

+/* A failure before accept must return with the worker already joined. */
+static int test_blocking_quic_connect_failure(int idx)
+{
+    SSL_CTX *cctx = NULL;
+    SSL *listener = NULL, *client = NULL, *server = NULL;
+    int ret = 0;
+
+    if (!qtest_supports_blocking())
+        return TEST_skip("Blocking tests not supported in this build");
+
+    if (!TEST_ptr(cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method()))
+        || !TEST_true(qtest_create_quic_objects_ex(libctx, cctx, NULL, cert,
+            privkey, QTEST_FLAG_BLOCK, &listener, &client, NULL, NULL))
+        || !TEST_true(SSL_get_blocking_mode(client)))
+        goto end;
+
+    if (idx == 0) {
+        /* Fail SSL_connect() locally without sending an Initial. */
+        if (!TEST_true(SSL_set1_initial_peer_addr(client, NULL)))
+            goto end;
+    } else {
+        /* Fail the listener before it has a channel to report the error. */
+        ossl_quic_port_raise_net_error(ossl_quic_listener_get_port(listener),
+            NULL);
+        ERR_clear_error();
+    }
+
+    if (!TEST_false(qtest_create_quic_connection_ssl(listener, client, &server))
+        || !TEST_ptr_null(server))
+        goto end;
+    if (idx == 1) {
+        SSL_CONN_CLOSE_INFO info;
+
+        if (!TEST_true(SSL_get_conn_close_info(client, &info, sizeof(info)))
+            || !TEST_str_eq(info.reason, "QUIC test server handshake failed"))
+            goto end;
+    }
+    ret = 1;
+end:
+    SSL_free(server);
+    SSL_free(listener);
+    SSL_free(client);
+    SSL_CTX_free(cctx);
+    return ret;
+}
+
 static int use_session_cb_cnt = 0;
 static int find_session_cb_cnt = 0;
 static const char *pskid = "Identity";
@@ -6276,6 +6322,7 @@ int setup_tests(void)
     ADD_TEST(test_back_pressure);
     ADD_TEST(test_multiple_dgrams);
     ADD_ALL_TESTS(test_non_io_retry, 2);
+    ADD_ALL_TESTS(test_blocking_quic_connect_failure, 2);
     ADD_TEST(test_quic_psk);
     ADD_ALL_TESTS(test_client_auth, 3);
     ADD_ALL_TESTS(test_alpn, 2);