Skip to content

Avoid busy-spinning during TLS client handshakes - #655

Open
bsergean wants to merge 3 commits into
masterfrom
fix/tls-client-handshake-wait
Open

bsergean wants to merge 3 commits into
masterfrom
fix/tls-client-handshake-wait

Conversation

@bsergean

Copy link
Copy Markdown
Collaborator

TLS clients currently busy-spin on WANT_READ/WANT_WRITE during the handshake. Against a connected peer that stalls, OpenSSL, Mbed TLS and SecureTransport consume about 48 ms of client-thread CPU during a 50 ms wait. This contributes directly to handshake/reconnect CPU spikes like #525.

  • Wait for the requested socket readiness with a 10 ms cancellation interval.
  • Share Mbed TLS's existing readiness-aware handshake between client and server, removing the duplicate client loop.
  • Remember the last SecureTransport I/O callback direction because its would-block result does not distinguish reads from writes.
  • Report readiness failures immediately and accept an empty cancellation callback consistently.

Validation: deterministic in-memory tests fail on the old retry loops and pass after the fix, covering read/write backpressure without sleeps, network requests or timeout waits. Adapter suites pass: OpenSSL 238 assertions, Mbed TLS 270, SecureTransport 20. Targeted ThreadSanitizer runs pass, along with three repeated SOCKS5 runs. Full OpenSSL suite: 32/32; Mbed TLS SOCKS5 and TLS-server integration tests also pass. Real handshakes and HTTP responses against a local OpenSSL peer succeed with all three client backends. Production builds as strict C++11 with warnings as errors (legacy SecureTransport deprecation warnings suppressed only in that build).

In the controlled 50 ms stalled-peer profile, warmed client-thread CPU falls from about 48 ms to 0.4–1.4 ms. Cancellation completes within the additional 10 ms readiness interval. The packet capture attached to #525 contains HTTPS connections, but a packet capture alone cannot identify every source of application CPU use; this change fixes the independently reproduced client-handshake spin.

Refs #525. Includes the same narrowly scoped Darwin errno/TSan workaround already validated on #651 and #653, keeping this branch's sanitizer runs reliable.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant