Skip to content

FileLog on macOS: read a file moved back into the directory from the start when a link outside kept its inode - #133904

Merged
Algunenano merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/filelog-darwin-unlinked-inode-moved-back
Nov 24, 2026
Merged

Algunenano merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/filelog-darwin-unlinked-inode-moved-back

Conversation

@groeneai

@groeneai groeneai commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #117738 (comment)
Related: #125794
Related: #123593

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):

Description

05319_file_log_rename_in_reused_inode timed out in Fast test (arm_darwin) of #117738 (report). One macOS scan saw a.csv gone and b.csv, moved in from a hard link outside the directory, with the same inode, and paired them as a rename (onItemMovedFrom a.csv, onItemMovedTo b.csv in one batch), so b.csv was read from a.csv's offset and rows were skipped.

Since #125794, a NOTE_DELETE of a file that still has another link no longer records anything, so the veto from #123593 does not fire. The watcher now records the inode of such a file, and is_rename does not pair it when its new name matches the path: the last scan listed every matching name, so the name is new to the file, which is read from the start. An excluded name (a hidden alias) is unchanged.

A file renamed inside the directory in the same scan as another of its links is removed is read again from the start, as before #125794. A file moved back under its own old name in one scan is still kept as the same file on macOS, as on master.

The test gains the same steps run in one process, so a macOS scan sees them together more often. Validation: macOS cross-compile of all FileLog objects with CI's flags, Linux 50/50 of the test; the macOS run is this PR's Fast test (arm_darwin).

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.12.1.409-master (included in 26.12 and later)

05319_file_log_rename_in_reused_inode timed out in Fast test (arm_darwin) of ClickHouse#117738:
one macOS scan saw a.csv gone and b.csv, moved in from a hard link outside the
directory, with the same inode, diffed them as MOVED_FROM a.csv + MOVED_TO b.csv,
and StorageFileLog kept a.csv's offset, skipping rows of b.csv.

ClickHouse#123593 made is_rename refuse a name in deleted_files as a rename source. Before
ClickHouse#125794 every NOTE_DELETE of a watched descriptor put its name there; ClickHouse#125794
(4d75ef8) returns early while the file still has links, because kqueue reports
an unlink of any name to every descriptor of the file and the scan has to find
which name went away (hidden excluded aliases of 05316_file_log_globs). When the
remaining link is outside the directory, nothing recorded the unlink any more.

Record the inode in that branch (unlinked_inodes), and in is_rename refuse the pair
when the inode's current name matches the path: the scan lists every matching name
and is_rename requires one listed name per side, so such a name was not a link of the
file at the last scan and the file came back, i.e. it is new here, as on Linux (an
IN_MOVED_TO without its IN_MOVED_FROM is ADDED). An excluded current name can be a
hidden alias and keeps ClickHouse#125794's behaviour. The set lives next to deleted_files: kept
across discarded and failed passes, cleared after a committed one, the first snapshot
included (its listing already reflects every unlink recorded before it, and it pairs
nothing). The unlink is drained before the diff (scanFilesAndUpdateWatches drains
before syncFileWatches closes descriptors), so the decision does not depend on scan
timing.

The test gains the same steps in one process so a macOS scan sees the unlink and
the move-in together; the new content is longer than c.csv's offset, so a carried-
over offset gives wrong rows (checked on Linux with a same-inode rename, which keeps
the offset: only 110 is read).

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; 2 findings, then 0 on the re-review of the fix) plus my own review (3 nits).

# Sev Finding Verdict Evidence / action
1 ⚠️ unlinked_inodes survived the first snapshot, so a rename right after it could be re-read (DirectoryWatcherBase::onTimer) AGREE, fixed cleared when the first snapshot commits
2 ⚠️ The one-process test step can pass without the fix if a scan lands between the unlink and the move-in DISAGREE the step only makes a one-scan view more likely, as the description says; with the fix every interleaving reads 60..110 once; a watcher failpoint for this was removed in #122448
3 💡 A rename in the same scan as the removal of another link of the file is re-read DISAGREE disclosed in the description; same as before #125794
4 💡 The is_rename comment states the common case as a fact AGREE, fixed reworded

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

Session id: cron:clickhouse-review-slot-8:20261123-220900

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? macOS only, and there is no macOS machine available to me: the CI logs of a07b289 show the exact interleaving (one batch with onItemMovedFrom a.csv and onItemMovedTo b.csv, then 30, 40 instead of 10 to 40). A Linux negative control (same-inode rename that keeps the offset) proves the new test step fails on a carried-over offset (60 to 100 missing).
b Root cause explained? Since #125794 a NOTE_DELETE of a file that still has a link outside the directory records nothing, so the is_rename veto from #123593 does not fire and the file moved back in under a new name is paired with the removed one, keeping its offset.
c Fix matches root cause? Yes: the watcher records the inode of such a file and is_rename, the one place that decides pairing, refuses it when its new name matches the path. No timing or test-only change.
d Test intent preserved / new tests added? 05319 keeps all its steps and gains the same scenario run in one process, so a macOS scan sees the unlink and the move-in together.
e Both directions demonstrated? Fails: CI a07b289 (Fast test (arm_darwin)). Passes: Linux 50/50 and a macOS cross-compile with CI's flags (-Werror); the macOS run is this PR's Fast test (arm_darwin).
f Fix is general across code paths? All five users of is_rename share the change. The Linux inotify path pairs by cookie and is already correct. Moving a file back under its own old name in one scan is unchanged (as on master).
g Fix generalizes across inputs (params/datatypes/wrappers)? Matching vs excluded new names (hidden aliases keep their behaviour, 05316_file_log_globs passes), links inside vs outside the directory, discarded and failed scans (the set is kept like deleted_files).
h Backward compatible? (maintainer-approved exception only) Yes: no settings, formats or stored state.
i Invariants and contracts preserved? The new set is reactor-thread state like deleted_files, cleared where it is and also when the first snapshot commits (that listing already reflects every earlier unlink and pairs nothing); the event order of a scan is unchanged.

Session id: cron:clickhouse-impl-slot-4:20261123-210205

@clickhouse-gh

clickhouse-gh Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f9a2299]

Summary: ✅


AI Review

Summary

The PR prevents macOS FileLog scans from treating a file moved back into the watched directory under a new matching name as a rename when an outside hard link preserved its inode. It also extends the regression scenario to shorten the interval between unlinking and moving the file back. No actionable correctness or safety findings remain on f9a22996743bbdc6d15bd29e76af2e42599c74b0.

Missing context / blind spots
  • ⚠️ Native macOS execution was unavailable locally, and Praktika had no completed Fast test (arm_darwin) result at review time. That job must provide platform validation; the passing Linux fast test does not execute the changed macOS code.
Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-not-for-changelog This PR should not be mentioned in the changelog can be tested Allows running workflows for external contributors comp-message-queues Message queue integrations (Kafka, RabbitMQ, NATS table engines for stream ingestion/egress). labels Nov 23, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing f9a229967 with master c9994aa17 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 6ee2dbefc; 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.

Compile time of recompiled translation units

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

Job report

@Algunenano Algunenano self-assigned this Nov 24, 2026

@Algunenano Algunenano left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I feel this code keeps getting more and more complex (partially my fault) and I doubt it has many users

@Algunenano
Algunenano added this pull request to the merge queue Nov 24, 2026
Merged via the queue into ClickHouse:master with commit 75a9537 Nov 24, 2026
388 of 389 checks passed
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 24, 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-message-queues Message queue integrations (Kafka, RabbitMQ, NATS table engines for stream ingestion/egress). 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