Skip to content

Do not refuse to start over a Filesystem database whose path cannot be accessed - #126772

Merged
PedroTadim merged 5 commits into
ClickHouse:masterfrom
groeneai:groeneai/database-filesystem-unreachable-path-on-load
Oct 21, 2026
Merged

PedroTadim merged 5 commits into
ClickHouse:masterfrom
groeneai:groeneai/database-filesystem-unreachable-path-on-load

Conversation

@groeneai

@groeneai groeneai commented Oct 19, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #121705
Related: #118684

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fixed a server or clickhouse-local --path refusing to start with Code: 1001 STD_EXCEPTION when the directory of a Filesystem database cannot be accessed, for example because it became a symlink loop or lost traverse permission. Such a database now loads with no tables and logs a warning, the same as a missing directory. CREATE DATABASE ... ENGINE = Filesystem(...) over such a path, including one with a component longer than NAME_MAX, now fails with BAD_ARGUMENTS instead of STD_EXCEPTION. Closes #121705.

Description

On 26.9.19.9, a server or clickhouse-local --path store holding a Filesystem database whose directory became a symlink loop or lost traverse permission refuses to start with Code: 1001 STD_EXCEPTION, and the database cannot be dropped. #118684 fixed only a missing directory.

Root cause: the DatabaseFilesystem constructor probes its path with the throwing fs::exists and, on a server, with pathStartsWith (fs::relative). libc++ reports only ENOENT/ENOTDIR as "not found". Every other stat error (ELOOP, EACCES, ENAMETOOLONG) throws before the startup-replay guard, and loadMetadata stops at the first exception.

Change: probe once with fs::exists(path, ec). If stat works, containment keeps the canonical pathStartsWith check. If it fails, where the path leads cannot be resolved, so it is treated like a missing path without a containment check: the startup replay logs Cannot access path X: <errno> and attaches the database with no tables, and a user statement gets BAD_ARGUMENTS with the errno (master reports the same errno as Code: 1001). Each table resolution still checks its own path against user_files before touching a file. Missing and existing paths behave as before.

Validation: 05136_database_filesystem_missing_path_on_load gains a symlink-loop restart sequence (clickhouse-local) and a server arm with a component over NAME_MAX. They fail on master and pass with the fix, 50/50 under randomized settings. I also restarted a server over ELOOP, EACCES, a user_files_path configured through a symlink, and a symlink from outside user_files into a directory that lost traverse permission: master does not start, the fix starts and the database drops.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.11.1.478-master (included in 26.11 and later)

groeneai and others added 3 commits October 19, 2026 17:07
…e accessed

The DatabaseFilesystem constructor probed its path with the throwing
fs::exists and, on a server, with pathStartsWith (fs::relative, which
canonicalizes). libc++ maps only ENOENT/ENOTDIR to "not found", so any
other stat error (ELOOP, EACCES, ESTALE, EIO, ENAMETOOLONG) escaped as
Code: 1001 STD_EXCEPTION before the startup-replay guard of ClickHouse#118684.
loadMetadata stops at the first exception, so a server or a
clickhouse-local --path store holding such a database refused to start,
and the database could not be dropped.

Probe once with fs::exists(path, ec). When stat works, containment keeps
the canonical pathStartsWith check. When it fails, the path cannot be
canonicalized, so containment is checked lexically with
fileOrSymlinkPathStartsWith, the check every table resolution already
uses, and a path outside user_files is still refused on replay. An
unreachable path is then handled like a missing one: the replay logs the
errno and attaches the database with no tables, and a user statement
gets BAD_ARGUMENTS with the errno instead of STD_EXCEPTION. Missing and
existing paths behave exactly as before.

05136 gains a symlink-loop restart sequence for clickhouse-local and two
server arms with a component over NAME_MAX, inside and outside
user_files.

Closes ClickHouse#121705

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…user_files when stat fails

When stat of the path fails for a reason other than "not found", the
containment check compared the stored spelling with the canonical
user_files lexically. A root spelled through a symlink to user_files
(for example the configured path on a server whose data directory is a
symlink) passed CREATE, which canonicalizes, but was refused on replay
once the root became unreachable, so the server still did not start.

Canonicalize the part of the path that resolves and keep the failing
component as written, as weakly_canonical does for a missing path, and
check containment on that. A path outside user_files is still refused.

05136: the server arms now assert the error code and message, and a new
arm spells the root through /proc/self/root.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Shorten the canonicalizeResolvablePrefix doc comment to one line and drop the sentence narrating the containment fallback.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 3 findings, then 0 on the re-review of the fix round) plus my own review.

# Sev Finding Verdict Evidence / action
1 ❌ A root spelled through a symlink to user_files (e.g. the configured path on a symlinked data dir) is still refused on replay once stat fails AGREE, fixed @ 06e374b the unreachable branch canonicalizes the part of the path that resolves, as weakly_canonical does for a missing path; new alias arm; restart over such a spelling validated
2 ❌ The fallback can attach a root symlinked to an inaccessible path outside user_files DISAGREE file() reads through the same symlink on master under the same READ grant (StorageFile keeps symlinks in user_files unresolved by design), and a root removed at startup then re-created as a symlink reaches the same state
3 ⚠️ Server arms assert the message without the error code AGREE, fixed @ 06e374b arms assert Code: 36 ... BAD_ARGUMENTS; a stateless test cannot restart the server, so restarts (ELOOP, EACCES, alias spelling) are validated locally
4 💡 A query of a table of an unreachable database still reports the stat error as STD_EXCEPTION DISAGREE (noted) per-query probe that keeps the documented "broken is not missing" contract of existsOrFileNameTooLong, reports the real errno and never stops loading

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are
terminal per finding.

Session id: cron:clickhouse-review-slot-11:20261019-182400

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. Create a Filesystem database over a directory, replace the directory with a symlink to itself, start again: master clickhouse-local --path (and the official 26.9.19.9 / 26.8.28.14 images) die with Code: 1001, every time.
b Root cause explained? The constructor's fs::exists and pathStartsWith (fs::relative) throw for every stat error other than ENOENT/ENOTDIR, before the startup-replay guard, and loadMetadata stops at the first exception.
c Fix matches root cause? One non-throwing fs::exists(path, ec) probe. No try/catch, and no error becomes a silent "does not exist": the replay logs the errno, a user statement gets BAD_ARGUMENTS with it.
d Test intent preserved / new tests added? 05136 keeps its 4 arms and gains 6: a symlink-loop restart sequence (load, user replay, drop) and three server NAME_MAX arms that assert Code: 36 and the message: inside user_files, outside it, and inside it spelled through a symlink.
e Both directions demonstrated? The new arms fail on master and pass with the fix. 50/50 under randomized settings. A server restart over ELOOP, over EACCES, and over a user_files_path configured through a symlink fails on master and succeeds with the fix. Each of 5 mutations of the change reddens the test.
f Fix is general across code paths? Both throwing probes in the constructor are fixed. Per-table probes keep their documented contract and never abort loading. The sibling database engines' constructors do not probe the filesystem.
g Fix generalizes across inputs (params/datatypes/wrappers)? ELOOP, EACCES and ENAMETOOLONG were measured, with relative and absolute paths, and with a root spelled through a symlink to user_files. The missing-path behavior is unchanged. No column types are involved.
h Backward compatible? (maintainer-approved exception only) Yes. No setting, format or default changes. For an existing or missing path every message and code is unchanged. Only stat failures change, from 1001 to BAD_ARGUMENTS (user statement) or warn-and-attach (replay).
i Invariants and contracts preserved? user_files containment stays fail-closed: canonical when stat works, and otherwise checked on the canonicalized part of the path that resolves, as weakly_canonical does for a missing path. A mutation that drops that check reddens the outside arm. The replay returns a fully constructed empty database, as for a missing path.

Session id: cron:clickhouse-impl-slot-7:20261019-175300

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @tuanpach @tavplubix, could you review this and add the can be tested label so CI runs? The Filesystem database constructor probed its path with throwing fs::exists/fs::relative, so any stat error other than ENOENT (symlink loop, EACCES) escaped as STD_EXCEPTION and stopped metadata loading; it now probes once with fs::exists(path, ec) and treats an unreachable path like a missing one, keeping user_files containment fail-closed.

@clickhouse-gh

clickhouse-gh Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [b503d56]

Summary: ✅


AI Review

Summary

Reviewed commit b503d56449e4452de4fd10b1a45c6c9d301addf8. The PR allows startup metadata replay to attach a Filesystem database whose directory cannot be accessed, while user-issued CREATE DATABASE and ATTACH DATABASE return BAD_ARGUMENTS. No unresolved findings remain; the previous symlink startup issue is addressed. The regression test 05136_database_filesystem_missing_path_on_load passes in the current head's Fast test report.

Final Verdict

✅ Approve. No new inline findings.

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default comp-database-engines Database engine implementations (e.g., Atomic/Replicated) and database-level behavior. labels Oct 19, 2026
@PedroTadim PedroTadim self-assigned this Oct 20, 2026
@PedroTadim PedroTadim added the can be tested Allows running workflows for external contributors label Oct 20, 2026
Comment thread src/Databases/DatabaseFilesystem.cpp Resolved
…at'ed

When `stat` fails on the database path, containment in `user_files` was
checked on the canonical prefix with the failing component kept as
written. A path that is itself a symlink outside `user_files` pointing
into it lost its target that way: for `/srv/imports -> <user_files>/locked/data`
with `locked` no longer traversable, `fs::canonical` fails on the whole
path, the symlink is stripped as the failing component, and the result
`/srv/imports` is outside `user_files`. The startup replay then throws
`Path must be inside user-files path` and the server does not start
(measured: exit 36 on the previous head, 1001 on master).

`canonicalizeResolvablePrefix` now follows a component that is a readable
symlink before keeping it as written, bounded by the kernel's 40-symlink
limit so a loop terminates. It uses the `error_code` overload of
`fs::read_symlink`: `FS::readSymlink` throws, so a `readlink` failing after
a successful `lstat` (EIO, ESTALE, a concurrent replace) would escape as
`STD_EXCEPTION` again. `readlink` of `dir/link/` fails (the trailing
separator makes the kernel follow the link), so that spelling is not
followed with `parent_path` = the link itself; the loop strips the
separator and follows `dir/link` against its real parent.

A stored path of `PATH_MAX` bytes or more is returned as written without
the walk: no such path can be accessed, so it can never be created or
replayed, while stripping it one component at a time copied the path once
per component (measured, debug build: a rejected `CREATE` with 130000
components took 2.29 s on the previous head, 0.08 s on master, 0.48 s now).
Only the stored spelling is checked: a followed `parent/target` may exceed
`PATH_MAX` and still resolve, because the kernel never builds that string
(measured: a 4127-byte `parent/target` into `user_files` starts and drops).

The test gains three server arms: a symlink from outside `user_files` to
a path inside it that stat cannot follow (inside), the same to a path
outside it (refused), and a symlink loop outside it (refused; an
unbounded follow makes this arm time out).

Reported by the AI review on the PR.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Commit b503d56449e4452de4fd10b1a45c6c9d301addf8 was not compared: the CI logs cluster did not answer (exception 241, code 500: { "meta": [ { "name": "file", "type": "LowCardinality(String)" …).

See the job log for details.

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 4d29de3

Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Upgrade check (amd_release) / Segmentation fault (STID: 5726-4458) crash, not from this PR (it changes only the Filesystem database path probe): segfault in the TemporaryBlockStreamHolder constructor under GraceHashJoin::addBuckets while a JoinStep is built. It is in the previous-release server the upgrade check starts from, not in this PR's build: the frame addresses are identical in the Upgrade check of 13 other unrelated PRs in the last day #126204 (mine, merged; its 26.9 and 26.10 backports are merged too)
Upgrade check (amd_release) / the 5 Cannot list/count/run/submit ... mutation(s) and Cannot list the row policies rows fallout of the crash above: the server was down #126204 (the owner of the row above)
Integration tests (arm_binary, 2/4) / test_storage_kafka/test_multi_consumer_quota.py::test_temporary_lock_quota_is_retained_across_rounds flaky, not from this PR (it does not touch Kafka): the test was added to master today and has failed in the same job on 4 other PRs since a fix task is created (investigating at full effort, fixing-PR link to follow here)
Mergeable Check, PR praktika rollups of the rows above #126204 and the fix task above (the owners of the rows above)
Sync - CH Inc sync (private, not actionable by me)

The other FAIL rows in CIDB for this PR belong to the previous head 8faa3eced092 (label-wall Pre Hooks attempts and a FunctionsStress.stress failure).

Session id: cron:our-pr-ci-monitor:20261020-133009

@groeneai

Copy link
Copy Markdown
Collaborator Author

Fixing PR for test_temporary_lock_quota_is_retained_across_rounds created: #126965

Comment thread src/Databases/DatabaseFilesystem.cpp Resolved
Drop `canonicalizeResolvablePrefix`. Whatever it resolved, the outcome
did not depend on it: a user statement over such a path is refused with
`Cannot access path` either way, and the startup replay attaches the
database with no tables. The containment that protects files is the one
each table resolution already does (`checkTableFilePath`), before it
touches a file: a root behind a symlink from outside `user_files` serves
no table even while it is accessible (`File is not inside ...`).

`fs::weakly_canonical` cannot replace the helper: libc++ stops at the
first component whose stat fails for a reason other than ENOENT or
ENOTDIR, which is exactly the case here.

So a path stat fails on skips the containment check. A path outside
`user_files` that stat refuses now reports `Cannot access path` instead
of `Path must be inside user-files path`; master reports the same errno
as `Code: 1001`. 05136 keeps one server arm, since the others only
exercised the helper.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@PedroTadim
PedroTadim added this pull request to the merge queue Oct 21, 2026
Merged via the queue into ClickHouse:master with commit 6a9bea5 Oct 21, 2026
179 checks passed
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-database-engines Database engine implementations (e.g., Atomic/Replicated) and database-level behavior. pr-bugfix Pull request with bugfix, not backported by default 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.

Filesystem DB path guard only handles ENOENT: any other stat error aborts load

3 participants