Repository navigation
Make embedded PTY client classes portable across Linux/macOS/FreeBSD - #104436
Conversation
Rewrite `base/base/openpty.h` to use POSIX `posix_openpt`/`grantpt`/ `unlockpt` instead of the Linux-specific `ioctl(TIOCSPTLCK/TIOCGPTN)` approach. Pick `ptsname_r` on Linux (where `ptsname` is not thread-safe) and `ptsname` on macOS/FreeBSD (where it uses thread-local storage). The function is now `inline` since it is used from a single translation unit but lives in a header. Drop the `#if defined(OS_LINUX)` guards from the files in `src/Server/ClientEmbedded/`. The classes themselves only depend on POSIX terminals and pipes and are now buildable on non-Linux. The SSH protocol itself is still gated by `OS_LINUX` elsewhere, which is unchanged here. Extracted from #100277. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
|
Workflow [PR], commit [9180a03] Summary: ✅ AI ReviewSummaryThis PR makes Final Verdict
|
|
@groeneai, link the fix of |
|
@alexey-milovidov —
Both are merged on master; if the failure on #104436's CI ran on a base older than 2026-05-06, a rebase will pick up #104167's fix. |
The previous implementation truncated the path to 19 bytes via `strncpy(name, slave_name_buf, 19)`, which broke the conventional `openpty` contract on layouts where the slave path is longer than 19 characters. Use `strcpy` to copy the full null-terminated path, matching glibc's `openpty` behavior. The actual ClickHouse caller (`PtyClientDescriptorSet.cpp`) passes `nullptr` for `name`, so behavior is unchanged for in-tree use.
Rewrite `base/base/openpty.h` using ClickHouse code style: 4-space indentation, camelCase function name `openPty`, `Type & name` reference parameters, `///` doc comment, no `NOLINT` block. The unused `name` and `tio` parameters are dropped from the signature. Removing `name` eliminates the dangerous `strcpy` into a caller-provided buffer of unknown size — the buffer pointer carries no length, so any caller that passed a path longer than its buffer could overflow. Update the single caller `PtyClientDescriptorSet` to the new signature and drop the file from the style-check exclusion list. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Move the body of `openPty` out of `base/base/openpty.h` into a new `base/base/openpty.cpp` and drop the `inline` qualifier. The header now just declares the function and forward-uses `winsize` via `<sys/ioctl.h>`. Add `<sys/ioctl.h>` and `<termios.h>` to `PtyClientDescriptorSet.cpp`, which previously relied on the transitive includes from `openpty.h`. Wire `openpty.cpp` into `base/base/CMakeLists.txt`.
Both Linux and FreeBSD ship a thread-safe `ptsname_r` (FreeBSD added it in 12.0). The previous code routed FreeBSD through `ptsname`, which is not thread-safe on FreeBSD - so concurrent `openPty` calls could observe another thread's slave-path buffer and open the wrong PTY. Use `ptsname_r` on `OS_LINUX || OS_FREEBSD` and keep the `ptsname` fallback only for platforms (macOS) where `ptsname_r` is unavailable but `ptsname` is thread-safe via TLS.
On Linux, glibc's `ptsname_r` returns 0 on success or a positive `errno` value on failure and does not set `errno` itself. The previous code returned `-1` from `openPty` with whatever stale value `errno` happened to hold, so callers wrapping the failure in `ErrnoException` could report the wrong reason. Capture the return value and assign it to `errno` before closing the master and returning. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
LLVM Coverage Report
Changed lines: 0.00% (0/31) · Uncovered code |
The lambda argument name `__table1.` (a backquoted identifier ending in a dot) makes the targeted AST fuzzer mutate the test table into a column whose ALIAS default type mismatches, which forces error-message formatting to convert the lambda back to an AST. IdentifierNode::toASTImpl then builds an ASTIdentifier whose name parts include an empty trailing part (from splitting `__table1.` on the dot), tripping chassert(!part.empty()) in the ASTIdentifier constructor (pre-existing engine assertion, unrelated to this PR's fix; also seen on unrelated PRs ClickHouse#104436 and ClickHouse#99877). The `__table1.y` lambda-arg cases already cover the same code path in buildShardCollapseFanOut (an unquoted lambda-arg name that looks like a `__tableN.` qualifier but whose tail is not a real column, so its numbering must be left intact), without producing an empty identifier part. Keep those and drop the redundant trailing-dot variant so the test no longer surfaces the unrelated assertion under fuzzing. Co-Authored-By: Claude Opus 4.8 <[email protected]>
The lambda argument name `__table1.` (a backquoted identifier ending in a dot) makes the targeted AST fuzzer mutate the test table into a column whose ALIAS default type mismatches, which forces error-message formatting to convert the lambda back to an AST. IdentifierNode::toASTImpl then builds an ASTIdentifier whose name parts include an empty trailing part (from splitting `__table1.` on the dot), tripping chassert(!part.empty()) in the ASTIdentifier constructor (pre-existing engine assertion, unrelated to this PR's fix; also seen on unrelated PRs ClickHouse#104436 and ClickHouse#99877). The `__table1.y` lambda-arg cases already cover the same code path in buildShardCollapseFanOut (an unquoted lambda-arg name that looks like a `__tableN.` qualifier but whose tail is not a real column, so its numbering must be left intact), without producing an empty identifier part. Keep those and drop the redundant trailing-dot variant so the test no longer surfaces the unrelated assertion under fuzzing. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Make
base/base/openpty.hand the classes insrc/Server/ClientEmbedded/build on non-Linux platforms (macOS/FreeBSD).openpty.his rewritten to use POSIXposix_openpt/grantpt/unlockptinstead of the Linux-specificioctl(TIOCSPTLCK/TIOCGPTN)approach.ptsname_ris used on Linux (whereptsnameis not thread-safe) andptsnameon macOS/FreeBSD (where it uses thread-local storage and is thread-safe). The function is markedinlinesince it lives in a header.#if defined(OS_LINUX)guards are dropped fromClientEmbedded.{h,cpp},ClientEmbeddedRunner.{h,cpp},IClientDescriptorSet.h,PipeClientDescriptorSet.h, andPtyClientDescriptorSet.{h,cpp}. The classes only rely on POSIX terminals and pipes.The SSH protocol itself is still gated by
OS_LINUXelsewhere; that is not changed here.Extracted from #100277 to land the platform-portability piece on its own.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Make the embedded-client and PTY descriptor classes build on macOS and FreeBSD by using portable POSIX
posix_openpt/grantpt/unlockptand removing Linux-only#ifguards.Documentation entry for user-facing changes
Version info
26.5.1.584