You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In V's vendored mbedtls, MBEDTLS_THREADING_C (mbedtls's internal locking) is enabled only on Linux/FreeBSD/OpenBSD and #undef'd on Windows and macOS. This means
mbedtls does not mutex-protect its shared state (the ctr_drbg RNG, RSA key blinding,
and the library's global mutexes) on Windows/macOS, so any design that shares an mbedtls
object across threads is unsafe there.
Where it comes from
thirdparty/mbedtls/mbedtls.patch (V's local patch over mbedtls 3.6.x) contains:
mbedtls offers two locking backends: MBEDTLS_THREADING_PTHREAD (built-in, needs pthread.h) and MBEDTLS_THREADING_ALT (the embedder supplies the mutex callbacks via mbedtls_threading_set_alt() and defines mbedtls_threading_mutex_t in a threading_alt.h). The patch enables the pthread backend only where pthreads are present
and disables threading otherwise. Windows (MSVC) has no pthread.h and no THREADING_ALT
shim was written; macOS has pthreads but is simply not listed in the gate (apparent
oversight).
Why it matters
Today it is harmless: V never shares mutable mbedtls state across threads — client SSLConns each own their config + RNG, and the TLS server handshakes run serially on the
accept thread. But it blocks any future concurrency that shares an mbedtls config/RNG
across threads, in particular parallelizing TLS server handshakes across worker threads
(the deferred item #3 from #27433). On Windows/macOS that would be a data race on the
shared ctr_drbg and RSA key state.
Proposed fix
macOS: add || defined(__APPLE__) to the gate — macOS has pthreads, so MBEDTLS_THREADING_PTHREAD works as-is.
Windows: implement MBEDTLS_THREADING_ALT over Win32 CRITICAL_SECTION:
threading_alt.h defining mbedtls_threading_mutex_t (a CRITICAL_SECTION + valid
flag);
four C callbacks wrapping InitializeCriticalSection/EnterCriticalSection/ LeaveCriticalSection/DeleteCriticalSection;
define MBEDTLS_THREADING_C + MBEDTLS_THREADING_ALT on _WIN32;
call mbedtls_threading_set_alt(...) once at startup (V module init()), before any
TLS use;
rebuild the mbedtls objects.
Fold the change into mbedtls.patch so it survives the next mbedtls upgrade.
Caveats
Enables (does not require) cross-thread sharing; small per-lock overhead once on.
set_alt must run before mbedtls touches any global mutex (module-init() handles this).
Touches thirdparty/ and the prebuilt mbedtls objects (rebuild needed).
Plan
Implementing on a branch; macOS (pthread) + Windows (THREADING_ALT). macOS will be tested
on Apple Silicon (M-series). Prerequisite for safe parallel TLS handshakes (#27433 item #3).
Note
You can use the 👍 reaction to increase the issue's priority for developers.
Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.
Summary
In V's vendored mbedtls,
MBEDTLS_THREADING_C(mbedtls's internal locking) is enabledonly on Linux/FreeBSD/OpenBSD and
#undef'd on Windows and macOS. This meansmbedtls does not mutex-protect its shared state (the
ctr_drbgRNG, RSA key blinding,and the library's global mutexes) on Windows/macOS, so any design that shares an mbedtls
object across threads is unsafe there.
Where it comes from
thirdparty/mbedtls/mbedtls.patch(V's local patch over mbedtls 3.6.x) contains:mbedtls offers two locking backends:
MBEDTLS_THREADING_PTHREAD(built-in, needspthread.h) andMBEDTLS_THREADING_ALT(the embedder supplies the mutex callbacks viambedtls_threading_set_alt()and definesmbedtls_threading_mutex_tin athreading_alt.h). The patch enables the pthread backend only where pthreads are presentand disables threading otherwise. Windows (MSVC) has no
pthread.hand noTHREADING_ALTshim was written; macOS has pthreads but is simply not listed in the gate (apparent
oversight).
Why it matters
Today it is harmless: V never shares mutable mbedtls state across threads — client
SSLConns each own their config + RNG, and the TLS server handshakes run serially on theaccept thread. But it blocks any future concurrency that shares an mbedtls config/RNG
across threads, in particular parallelizing TLS server handshakes across worker threads
(the deferred item #3 from #27433). On Windows/macOS that would be a data race on the
shared
ctr_drbgand RSA key state.Proposed fix
|| defined(__APPLE__)to the gate — macOS has pthreads, soMBEDTLS_THREADING_PTHREADworks as-is.MBEDTLS_THREADING_ALTover Win32CRITICAL_SECTION:threading_alt.hdefiningmbedtls_threading_mutex_t(aCRITICAL_SECTION+ validflag);
InitializeCriticalSection/EnterCriticalSection/LeaveCriticalSection/DeleteCriticalSection;MBEDTLS_THREADING_C+MBEDTLS_THREADING_ALTon_WIN32;mbedtls_threading_set_alt(...)once at startup (V moduleinit()), before anyTLS use;
mbedtls.patchso it survives the next mbedtls upgrade.Caveats
set_altmust run before mbedtls touches any global mutex (module-init()handles this).thirdparty/and the prebuilt mbedtls objects (rebuild needed).Plan
Implementing on a branch; macOS (pthread) + Windows (
THREADING_ALT). macOS will be testedon Apple Silicon (M-series). Prerequisite for safe parallel TLS handshakes (#27433 item #3).
Note
You can use the 👍 reaction to increase the issue's priority for developers.
Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.