Skip to content

Backport #116692 to 26.3: Prevent stale OpenSSL errors from breaking asynchronous TLS connections - #122201

Merged
PedroTadim merged 1 commit into
26.3from
backport/26.3/116692
Sep 25, 2026
Merged

PedroTadim merged 1 commit into
26.3from
backport/26.3/116692

Conversation

@robot-ch-test-poll3

@robot-ch-test-poll3 robot-ch-test-poll3 commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Original pull-request #116692
Cherry-pick pull-request #122024

This pull-request is a last step of an automated backporting.
Treat it as a standard pull-request: look at the checks and resolve conflicts.
Merge it only if you intend to backport changes to the target branch, otherwise just close it.

The PR source

The PR is created in the CI job

Version info

  • Merged into: 26.3.33.154

@robot-ch-test-poll3 robot-ch-test-poll3 added pr-backport Changes, backported to release branch. Do not use manually - automated use only! pr-bugfix Pull request with bugfix, not backported by default labels Sep 25, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [BackportPR], commit [dfdfbf8]

Summary: ✅

@PedroTadim
PedroTadim merged commit e6fea4f into 26.3 Sep 25, 2026
36 checks passed
@PedroTadim
PedroTadim deleted the backport/26.3/116692 branch September 25, 2026 13:38
pull Bot pushed a commit to AnotherGenZ/ClickHouse that referenced this pull request Sep 26, 2026
Closing a TLS socket whose last write timed out blocked for another
max(send_timeout, receive_timeout) inside SecureSocketImpl::shutdown() and then
threw Poco::TimeoutException. Measured on an INSERT INTO FUNCTION
url('https://...') against a peer that accepts, handshakes and never reads, with
http_send_timeout=1 and http_receive_timeout=9: 27.3 s before this change,
18.3 s after, i.e. exactly one max(send, receive) removed from socket teardown,
and the delta scales with that setting (7.4 s -> 5.6 s at receive_timeout=2).
Every direct shutdown() caller reachable from src/ already catches
Poco::TimeoutException, so the consequence is latency rather than a new escaping
exception.

An abandoned SSL_write leaves the OpenSSL record layer with wpend_tot > 0 and
rwstate == SSL_WRITING (contrib/openssl/ssl/record/rec_layer_s3.c:497), and
neither sendBytes timeout exit sets _fatalError, so shutdown() enters its retry
loop with a fresh getMaxTimeoutOrLimit() budget. There, ssl3_send_alert refuses
to dispatch close_notify while RECORD_LAYER_write_pending() is true
(contrib/openssl/ssl/s3_msg.c:68) and returns without performing any I/O, so
SSL_get_error reports SSL_ERROR_WANT_WRITE off the stale rwstate and Poco polls
SELECT_WRITE against a peer whose TCP window is zero until the budget is gone.

Retrying in that state can never succeed. A second SSL_shutdown reaches
ssl3_dispatch_alert, whose pending-write arm only retries when alert_dispatch is
SSL_ALERT_DISPATCH_RETRY; it is SSL_ALERT_DISPATCH_PENDING here, so it takes the
"API misuse?? Give up" branch (s3_msg.c:107-116). Only re-calling SSL_write with
the identical buffer could flush the pending record, and Poco has already
discarded it. A third call would fall into ssl_read_bytes and wait for the peer's
close_notify, the wait the comment above this block says Poco deliberately
avoids.

SSL_want_write() is the public spelling of rwstate == SSL_WRITING, and rwstate is
only set to SSL_WRITING on the same ossl_tls_handle_rlayer_return retry path that
assigns wpend_tot, so the predicate implies a pending record write and cannot
disable a retry that could have worked. The backpressure retry is preserved:
SilkFiberSecureSocketTest.BlockingShutdownRetriesWantWrite still asserts two
SSL_shutdown writes and a close_notify observed by the peer. Everything after the
loop is unchanged, so handleError still returns ERR_SSL_WANT_WRITE without
setting _fatalError and the transport is still shut down. That is the observable
behaviour of 26.3, 26.7, 26.8 and 26.9, none of which carry the retry loop.

The loop condition dates from 001fb05 (ClickHouse#116692, merged 2026-09-24), so no
released version is affected and no changelog entry is required. That PR's
backports ClickHouse#122201 (26.3) and ClickHouse#122197 (26.8) are still open and would import the
regression.

A successful read before the close resets rwstate while wpend_tot stays positive,
so the stall persists in that narrower shape. The exact predicate,
RECORD_LAYER_write_pending(), is declared in the private header
ssl/record/record.h and is not reachable from base/poco.

The new test goes in the existing src/Common/tests/gtest_ssl_send_timeout.cpp,
whose SSLSocketTimeout suite already builds the precondition: a never-reading TLS
peer and a sendBytes that throws. It requires that timeout first, then times
shutdown() with the receive timeout raised to 10 s, and also asserts that
SSL_SENT_SHUTDOWN is set so that skipping the orderly shutdown altogether cannot
pass. Without the guard it fails with elapsed 10009 ms and Poco::TimeoutException;
with it the whole test takes 0.5 s. All 38 TLS and socket unit tests pass, 50x
repeated.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backport Changes, backported to release branch. Do not use manually - automated use only! pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants