Skip to content

fix(Silk): honor O_NONBLOCK in the fiber TLS BIO - #111852

Merged
CheSema merged 8 commits into
masterfrom
silk-fiber-bio-nonblocking
Aug 14, 2026
Merged

CheSema merged 8 commits into
masterfrom
silk-fiber-bio-nonblocking

Conversation

@CheSema

@CheSema CheSema commented Jul 24, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

...


This is a latent-interaction bug that appears only when three components are combined — none is wrong on its own:

  1. Silk fiber sockets (Reapply "Add silk fibers aware socket implementations" #107680): the fiber-aware OpenSSL BIO silkBioRead/silkBioWrite submits io_uring I/O and parks the caller for the socket's send/receive timeout. That is correct for a blocking read — but the BIO never consults the fd's O_NONBLOCK flag, unlike OpenSSL's default socket BIO, which returns EAGAIN immediately when the flag is set.
  2. TLS (USE_SSL): the affected path is the TLS BIO. The plain-socket staleness check uses a raw recv(MSG_PEEK | MSG_DONTWAIT) and never touches this code, so the problem is TLS-only.
  3. The connection pool's SSL_peek staleness probe (Detect HTTP keep-alive peer disconnect with MSG_PEEK/SSL_peek, not poll #110402): before reusing a pooled connection, DB::getSocketState flips the fd non-blocking via ScopedNonBlocking (a raw fcntl(F_SETFL, O_NONBLOCK), behind Poco's back) and calls SSL_peek, expecting an immediate EAGAIN — its comment reads "The socket is non-blocking, so this never blocks." Detect HTTP keep-alive peer disconnect with MSG_PEEK/SSL_peek, not poll #110402 introduced this probe, replacing the previous poll-based keep-alive disconnect check.

Only with all three present does that non-blocking SSL_peek route through the silk BIO, which ignores O_NONBLOCK and blocks for the receive timeout left on the socket by the previous request. Each component is individually correct; the fix lands on the silk BIO because it is the one whose behavior diverges from OpenSSL's socket-BIO contract (honoring O_NONBLOCK), while the TLS layer and the #110402 probe are behaving as intended.

On master today the silk BIO has no wired production consumer — it is infrastructure — so this three-way combination is not yet reachable in a shipped server; it was reproduced with downstream work that routes object-storage-disk connections through silk fiber sockets.

Impact. Every borrow of a pooled TLS connection to an object-storage disk pays a timeout it should not. A server loading tables from an HTTPS object-store disk at startup does many such borrows and stalls — a deterministic ~13.5 s in a local reproduction, and an unbounded boot hang (never reaching "Ready for connections", no error logged) with production timeouts or a zero/unset receive timeout, where the wait becomes a deadline-less future.wait().

Root-cause evidence (local TLS-MinIO reproduction): a server-side request trace showed each request arriving only after its wait expired; the ~13.5 s decomposed exactly into the adaptive per-method receive timeouts paid in sequence (GET 500 ms + PUT 3000 ms + DELETE 10000 ms, ConnectionTimeouts.cpp); ss showed frozen bytes_sent through each stall; and a live backtrace was parked at silkBioRead ← SSL_peek ← getSslSocketState ← isStale ← getConnection. A control with silk sockets disabled does the same step in <15 ms. The plain-HTTP staleness probe uses a raw recv(MSG_PEEK|MSG_DONTWAIT) and is unaffected — the bug is TLS-specific.

Fix. When the fd is non-blocking, silkBioRead/silkBioWrite do a direct recv/send with MSG_DONTWAIT and set the BIO retry flags (immediate EAGAIN → SSL_ERROR_WANT_READ), matching OpenSSL's default BIO. The fiber/io_uring path is unchanged for blocking sockets. The non-blocking state is read fresh from the fd on each call via fcntl(F_GETFL), because Poco::Net::SocketImpl::getBlocking() is a cached flag the raw-fcntl probe never updates (and silk sockets reject setBlocking(false) outright).

Adds a regression test (SilkFiberSecureSocketTest.NonBlockingPeekDoesNotBlockOnIdleConnection) that drives the real getSocketState path against an idle TLS connection with a 5 s receive timeout and asserts it returns in under 500 ms; without the fix it blocks the full timeout.

Not for changelog: the silk fiber BIO is infrastructure with no in-tree production consumer yet, so no released user is affected.

Related: #107680
Related: #110402

silkBioRead/silkBioWrite always submitted io_uring I/O and parked the
caller for the socket's send/receive timeout, ignoring the fd's O_NONBLOCK
flag. The HTTP connection pool's staleness probe (getSslSocketState ->
SSL_peek) sets the fd non-blocking and expects an immediate EAGAIN; with
the silk BIO it instead blocked for the timeout left on the socket by the
previous request. Every pooled-connection borrow then paid that timeout -
a server loading tables from an HTTPS object-store disk at startup stalls
(a deterministic 13.5s here, an unbounded boot hang with production
timeouts).

When the fd is non-blocking, do a direct recv/send with MSG_DONTWAIT and
set the BIO retry flags; the fiber path is unchanged for blocking sockets.
Adds a regression test asserting the non-blocking peek returns promptly on
an idle TLS connection.
@clickhouse-gh

clickhouse-gh Bot commented Jul 24, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [6a5cb11]

Summary: ❌

job_name test_name status info comment
AST fuzzer (amd_debug, targeted) FAIL
Logical error: Invalid number of columns in chunk pushed to OutputPort. Expected A, found B (STID: 2270-3258) FAIL cidb, issue

AI Review

Summary

This PR teaches the silk TLS BIO to honor the temporary non-blocking probe used by getSocketState, so idle pooled HTTPS connections stop waiting on the socket timeout during SSL_peek. The implementation now matches the intended SocketPeerClosed contract for the in-tree callers I traced, but the review is still blocked on one missing regression that leaves part of the changed behavior unproven.

Tests
  • ⚠️ src/IO/tests/gtest_silk_fiber_stream_socket.cpp:392: the new regressions only prove the SSL_peek -> SSL_ERROR_WANT_READ path. This PR also changes silkBioWrite, and LLVM coverage still shows src/IO/SilkSecureFiberStreamSocketImpl.cpp:121-132 completely uncovered, so a regression in the SSL_ERROR_WANT_WRITE / post-handshake path would still slip through. Add one focused secure-socket test that forces SSL_peek to take the write side and verifies it returns quickly instead of waiting for getSendTimeout().
Final Verdict

Changes requested: add focused coverage for the write-side non-blocking BIO path before merge.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.70% 86.70% +0.00%
Functions 92.00% 92.00% +0.00%
Branches 79.10% 79.10% +0.00%

Changed lines: Changed C/C++ lines covered: 135/151 (89.40%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Jul 24, 2026
A lower-level companion to NonBlockingPeekDoesNotBlockOnIdleConnection:
drives SSL_peek directly on a non-blocking silk TLS socket - no
getSocketState / connection-pool helper - and asserts it returns
SSL_ERROR_WANT_READ in under 500ms, one level below the pool's
staleness probe that the other test exercises.
/// since silk sockets reject `Socket::setBlocking(false)`, so O_NONBLOCK is set on the raw fd
/// directly and only OpenSSL's BIO (`silkBioRead`) ever observes it. Pre-fix, `silkBioRead`
/// ignores the flag and parks the caller for the full receive timeout.
TEST_F(SilkFiberSecureSocketTest, NonBlockingSslPeekReturnsWantReadImmediately)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both added regressions only prove the SSL_peek -> SSL_ERROR_WANT_READ path. This PR also changes silkBioWrite, and getSslSocketState already treats SSL_ERROR_WANT_WRITE as a real outcome because SSL_peek can hit it while processing post-handshake traffic such as KeyUpdate.

Without one focused case that actually drives SSL_peek into WANT_WRITE, a regression in the new write-side O_NONBLOCK branch will still leave the pool probe hanging while these tests stay green. Please add a secure-socket test that forces the write side and asserts the call returns quickly instead of waiting for getSendTimeout().

@mstetsyuk

Copy link
Copy Markdown
Member

@CheSema, PTAL: 26eb7ab, 33366b7.

Arguably, setting the raw socket to blocking instead of calling setBlocking is a bit of a hack because Poco sockets use the _blocking flag in quite a few places. It could even be a bug.

And for silk sockets, it's cleaner to explicitly call setDontWait and then call getDontWait in BIO, than having BIO do fcntl to find out if the OS socket is blocking and if it is, call raw ::recv with MSG_DONTWAIT.

Resolved a conflict in src/IO/SocketPeerClosed.cpp: master renamed
`getSslSocketState` to `getSSLSocketState` while this branch restructured
the secure-socket probe (silk `setDontWait` branch + Poco-based
`ScopedNonBlocking`); kept the branch structure with master's names.

Co-Authored-By: Claude Fable 5 <[email protected]>
Comment thread src/IO/SocketPeerClosed.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.50% 86.50% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 78.70% 78.70% +0.00%

Changed lines: Changed C/C++ lines covered: 127/143 (88.81%) · Uncovered code

Full report · Diff report

@clickhouse-gh

clickhouse-gh Bot commented Aug 13, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 6a5cb11d3 with master bf580ecb3 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes
Binary Master PR Δ
programs/clickhouse-stripped 691.82 MiB 688.83 MiB -2.99 MiB (-0.43%)

Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction.

Compile time of recompiled translation units

9 translation units recompiled, 9 s compile time in total, 8 of them have a recent master baseline.

Job report

@CheSema
CheSema added this pull request to the merge queue Aug 14, 2026
Merged via the queue into master with commit ff291b3 Aug 14, 2026
179 of 181 checks passed
@CheSema
CheSema deleted the silk-fiber-bio-nonblocking branch August 14, 2026 09:57
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-not-for-changelog This PR should not be mentioned in the changelog pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants