Skip to content

Make embedded PTY client classes portable across Linux/macOS/FreeBSD - #104436

Merged
alexey-milovidov merged 11 commits into
masterfrom
enable-pty-non-linux
May 12, 2026
Merged

alexey-milovidov merged 11 commits into
masterfrom
enable-pty-non-linux

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented May 8, 2026 •

Copy link
Copy Markdown
Member

Make base/base/openpty.h and the classes in src/Server/ClientEmbedded/ build on non-Linux platforms (macOS/FreeBSD).

openpty.h is rewritten to use POSIX posix_openpt / grantpt / unlockpt instead of the Linux-specific ioctl(TIOCSPTLCK/TIOCGPTN) approach. ptsname_r is used on Linux (where ptsname is not thread-safe) and ptsname on macOS/FreeBSD (where it uses thread-local storage and is thread-safe). The function is marked inline since it lives in a header.

#if defined(OS_LINUX) guards are dropped from ClientEmbedded.{h,cpp}, ClientEmbeddedRunner.{h,cpp}, IClientDescriptorSet.h, PipeClientDescriptorSet.h, and PtyClientDescriptorSet.{h,cpp}. The classes only rely on POSIX terminals and pipes.

The SSH protocol itself is still gated by OS_LINUX elsewhere; that is not changed here.

Extracted from #100277 to land the platform-portability piece on its own.

Changelog category (leave one):

  • Build/Testing/Packaging Improvement

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 / unlockpt and removing Linux-only #if guards.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.5.1.584

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]>
@clickhouse-gh

clickhouse-gh Bot commented May 8, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [9180a03]

Summary: ✅


AI Review

Summary

This PR makes openpty handling and embedded client descriptor classes portable across Linux/macOS/FreeBSD by switching to POSIX PTY APIs in base/base/openpty.cpp and removing Linux-only compile guards from src/Server/ClientEmbedded/*. The prior inline review concern about stale errno from ptsname_r is fixed in the current code, and I did not find remaining correctness, safety, or compatibility issues in the changed surface.

Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-build Pull request with build/testing/packaging improvement label May 8, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member Author

@groeneai, link the fix of test_distributed_frozen_replica here.

@groeneai

groeneai commented May 9, 2026

Copy link
Copy Markdown
Collaborator

@alexey-milovidov — test_distributed_frozen_replica fixes:

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.

Comment thread base/base/openpty.h Resolved
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.
Comment thread base/base/openpty.h Resolved
alexey-milovidov and others added 2 commits May 11, 2026 07:10
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]>
Comment thread base/base/openpty.h Resolved
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`.
Comment thread base/base/openpty.cpp Resolved
@thevar1able thevar1able self-assigned this May 12, 2026
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.
Comment thread base/base/openpty.cpp Resolved
alexey-milovidov and others added 2 commits May 12, 2026 18:47
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]>
@clickhouse-gh

clickhouse-gh Bot commented May 12, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.00% -0.10%
Functions 91.10% 91.10% +0.00%
Branches 76.50% 76.50% +0.00%

Changed lines: 0.00% (0/31) · Uncovered code

Full report · Diff report

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue May 12, 2026
Merged via the queue into master with commit 9974541 May 12, 2026
165 checks passed
@alexey-milovidov
alexey-milovidov deleted the enable-pty-non-linux branch May 12, 2026 22:33
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label May 12, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Jun 28, 2026
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]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-build Pull request with build/testing/packaging improvement 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.

4 participants