Skip to content

feat(Poco): add sendNow, receiveNow and waitForSendOrReceive to plain, TLS and Silk sockets - #132376

Open
CheSema wants to merge 15 commits into
masterfrom
socket-send-receive-now
Open

CheSema wants to merge 15 commits into
masterfrom
socket-send-receive-now

Conversation

@CheSema

@CheSema CheSema commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

Steps 2 to 4 of #131084: non-blocking socket primitives for plain TCP, TLS and Silk fiber sockets. No behavior change and no caller yet; step 5 uses them in the HTTP body writer.

Poco::Net::SocketImpl gets waitForSendOrReceive, sendNow and receiveNow. With them, one thread sends data and watches for data from the peer at the same time, and nothing blocks. A connection error of sendNow is held until the caller has read what arrived, so a response that the server sent before a reset is not lost. A check before the send cannot close this window: #131102 (comment).

  • Plain TCP: the send and the receive use MSG_DONTWAIT, and the socket holds the error of a failed send.
  • TLS: after SSL_ERROR_WANT_WRITE, or after a failed transport write, OpenSSL allows no SSL_read. The first non-blocking call pushes NonBlockingFilterBIO on top of the transport BIO. In the non-blocking calls, the filter sends and receives through the primitives of the transport socket. It queues what does not fit, and it drops the bytes after a send error, which the transport socket holds. So OpenSSL never has a pending or a failed write. sendNow gives SSL_write one record at a time, and only when the queue is empty; the queue has a hard limit of 64 KiB. Blocking calls pass through as before, after the queued bytes.
  • Silk fiber sockets: the wait suspends the fiber through FiberScheduler::poll, not the thread. Silk TLS sockets use the same filter on top of their silk-fiber BIO.

One commit for each socket type, and one for the queue limit.

Related: #131084

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/132376]

… sockets

Step 2 of #131084. One thread can send and watch for data at the same time without blocking. A send error is held until the caller has read what arrived, so a response sent before a reset is not lost. Plain TCP only; TLS sockets and the wait of Silk fiber sockets throw until steps 3 and 4.
@clickhouse-gh

clickhouse-gh Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [425f75b]

Summary: ✅


AI Review

Summary

Adds non-blocking socket operations for plain TCP, TLS, and Silk, with deferred send errors and a bounded TLS ciphertext queue. The current code addresses the previous findings. No remaining actionable correctness or safety issue was found.

Missing context / blind spots
  • ⚠️ The new socket gtests were reviewed but not run locally: this checkout has no configured build and its dependency submodules are uninitialized. The current unit-test CI entry exposes no test results, so execution of these suites remains unverified.
Final Verdict

Status: ✅ Approve.

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Nov 17, 2026
…ceive` for TLS sockets

Step 3 of #131084. A filter BIO, installed by the first non-blocking call, queues the encrypted bytes that do not fit and holds a transport send error, so OpenSSL never has a pending or failed write and `SSL_read` stays allowed. Blocking calls pass through as before, after the queued bytes.
Step 4 of #131084. The wait suspends the fiber through `FiberScheduler::poll`, not the thread. Silk TLS sockets get the non-blocking calls through the filter BIO of step 3, on top of their `silk-fiber` BIO.
The callers keep the queue within one record and a few control records; the limit does not depend on that. A write over it fails the connection: a retry would leave OpenSSL with a pending write. No test: no real TLS peer can grow the queue past one record and its control records.
@CheSema CheSema changed the title feat(Poco): add sendNow, receiveNow and waitForSendOrReceive to sockets feat(Poco): add sendNow, receiveNow and waitForSendOrReceive to plain, TLS and Silk sockets Nov 17, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-external-dependencies Third-party deps updates in contrib/, vendored code, and platform base libraries. label Nov 17, 2026
@CheSema
CheSema marked this pull request as ready for review November 18, 2026 11:38
@clickhouse-gh

clickhouse-gh Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 425f75b42 with master 30ff53fe8 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 1d9af504a; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Object file sizes

55 object files changed (+99.35 KiB total), 1 added.

Object file Master PR Δ
base/poco/NetSSL_OpenSSL/CMakeFiles/_poco_net_ssl.dir/src/NonBlockingFilterBIO.cpp.o new 51.54 KiB +51.54 KiB
base/poco/NetSSL_OpenSSL/CMakeFiles/_poco_net_ssl.dir/src/SecureSocketImpl.cpp.o 115.61 KiB 133.50 KiB +17.88 KiB (+15.47%)

716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

1818 translation units recompiled, 3400 s compile time in total, 1817 of them have a recent master baseline.

Job report

A throttler wait inside a BIO callback can suspend a Silk fiber in the middle of an OpenSSL call, and the fiber can resume on another thread with another error queue. `SecureSocketImpl::sendNow` now waits before `SSL_write` and books the plaintext bytes, as `sendBytes` does; the filter sends with `sendNowUnthrottled`.
Comment thread base/poco/Net/src/SocketImpl.cpp Resolved
Comment thread base/poco/Net/src/SocketImpl.cpp Resolved
…OpenSSL

The wait sends the queued ciphertext also when the caller waits only for data, and a blocking receive uses `receiveNow` and the wait while bytes are queued. So a failed write of the queue never reaches OpenSSL, and an answer that has arrived is read even if the peer does not read any more. The blocking read of the filter no longer writes the queue.
OpenSSL takes a zero read for the end of the stream only when `BIO_eof` is true. The non-blocking read of the filter now sets `BIO_FLAGS_IN_EOF`, so `receiveNow` returns -1, as a blocking receive does.
clickhouse-gh[bot]
clickhouse-gh Bot previously approved these changes Nov 18, 2026
@clickhouse-gh
clickhouse-gh Bot dismissed their stale review November 19, 2026 13:16

AI review of a later commit does not approve the PR, see the AI Review comment.

TLS `receiveNow` books the plaintext before `SSL_read` and uses the returned bytes, as `receiveBytes` does, and the filter reads with `receiveNowUnthrottled`. A throttler error no longer fails the TLS connection, and the blocking fallback of `receiveBytes` settles its booked budget.
@clickhouse-gh clickhouse-gh Bot added comp-networking The connection layer under the protocols, on the server and on the client side: socket read / wri... and removed comp-external-dependencies Third-party deps updates in contrib/, vendored code, and platform base libraries. labels Nov 20, 2026
Comment on lines +502 to +505
/// While a non-blocking send has left queued bytes, the receive uses the non-blocking calls, and
/// the wait sends the bytes outside OpenSSL: OpenSSL must not see their failed write, and a peer
/// that has answered is read even if it does not read any more.
if (_pSocket->getBlocking() && (flags & MSG_DONTWAIT) == 0)

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.

AI findings:

The description no longer matches the TLS code. Update the TLS bullet and the last sentence.

  • "Blocking calls pass through as before, after the queued bytes" is now true only for blocking sends. While bytes are queued, a blocking receiveBytes uses readNow and waitForSendOrReceive here, and the blocking read of the filter no longer sends the queue first.
  • The TLS non-blocking calls now throttle the plaintext outside OpenSSL, and the filter sends and receives through sendNowUnthrottled and receiveNowUnthrottled. The description does not say this.
  • "One commit for each socket type, and one for the queue limit" no longer matches the 15 commits.

Comment on lines +589 to +593
bool queued = false;
{
NonBlockingFilterBIO::NonBlockingScope scope(filter);
queued = !filter.sendQueued();
}

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.

AI findings:
nit: this NonBlockingScope does nothing. sendQueued calls sendNowUnthrottled directly, and only the BIO callbacks look at the mode. The scope makes the reader look for a dependency on the mode that does not exist.

Suggested change
bool queued = false;
{
NonBlockingFilterBIO::NonBlockingScope scope(filter);
queued = !filter.sendQueued();
}
const bool queued = !filter.sendQueued();

int writeNonBlocking(const char * data, int length);
int readNonBlocking(BIO * bio, char * data, int length);

int writeQueuedToNext(BIO * bio);

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.

Maybe writeQueuedBlocking?
I don't have a strong opinion here

Comment on lines +682 to +684
ScopedLock lock(*_mutex);
if (length <= 0)
throw InvalidArgumentException("receiveNow needs a positive length");

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.

AI findings:

nit: check length before the lock, as waitForSendOrReceive now does with mode (line 576). With two orders in one class, the reader looks for a reason why they differ, and there is none. sendNow has the same order (line 624).

Suggested change
ScopedLock lock(*_mutex);
if (length <= 0)
throw InvalidArgumentException("receiveNow needs a positive length");
if (length <= 0)
throw InvalidArgumentException("receiveNow needs a positive length");
ScopedLock lock(*_mutex);

Comment on lines +624 to +626
ScopedLock lock(*_mutex);
if (length <= 0)
throw InvalidArgumentException("sendNow needs a positive length");

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.

AI findings:

nit: the same as in receiveNow: check length before the lock.

Suggested change
ScopedLock lock(*_mutex);
if (length <= 0)
throw InvalidArgumentException("sendNow needs a positive length");
if (length <= 0)
throw InvalidArgumentException("sendNow needs a positive length");
ScopedLock lock(*_mutex);

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

comp-networking The connection layer under the protocols, on the server and on the client side: socket read / wri... pr-not-for-changelog This PR should not be mentioned in the changelog

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants