Repository navigation
Fix FileLog on macOS re-reading a file renamed during a directory rescan - #122448
Conversation
The macOS `DirectoryWatcherBase` rebuilds inotify events by diffing directory snapshots, and it recognizes a rename only when one inode leaves one name and appears under another. A scan lists the directory and then runs `stat` on each entry, so a rename that lands in between leaves the renamed file under neither of its names (or under both). The diff then reports `REMOVED` + `ADDED`, and `StorageFileLog` reads the file again from offset 0, so its rows are inserted twice. After each scan, drain the kqueue again. If the directory itself changed (`NOTE_WRITE`, `NOTE_LINK`, `NOTE_RENAME` or `NOTE_DELETE` on the directory), a watched file was renamed, or a listed file was gone when it was `stat`-ed, the scan is discarded and repeated at once, and nothing is emitted from it. Appends and attribute changes never discard a scan. A drain that consumed any event makes the next `poll` return at once, so no change is noticed later than before. The Linux (inotify) watcher is unchanged. The new pausable fail point `file_log_directory_watcher_pause_after_listing` sits between the listing and the `stat` calls, which lets the new test rename a file inside a scan deterministically. The test runs only on macOS (`Fast test (arm_darwin)`) and is skipped on other platforms. Failing job: https://github.com/ClickHouse/ClickHouse/actions/runs/36145476798/job/108191212002 CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=119625&sha=d8052ae24c4059bf592f7b999d09e5c3d6e978ba&name_0=PR&name_1=Fast%20test%20%28arm_darwin%29 Related: ClickHouse#119625 Co-Authored-By: Claude Opus 5.5 <[email protected]>
The test renamed the only listed file away inside the paused scan, so the scan's `stat` failed with ENOENT and discarded the pass by itself: the test still passed without the post-scan kqueue drain. Add a second paused scan that renames `b.txt` to `b.txt.1` and recreates `b.txt` at once, as log rotation does. The listed name then still exists and `stat` succeeds on the new file, so only the drain (the directory's `NOTE_WRITE` and the renamed file's `NOTE_RENAME`) discards that scan; without it the rotated file is read again from offset 0. `wait_rows N` now collects rows until N of them arrived, 5 seconds per wait, so the worst case stays at 40 seconds, below the 60-second test timeout of `Fast test (arm_darwin)`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Condense the post-scan drain comment to one line and drop its 'or under both' clause, which no longer holds with the list-then-stat scan. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 4 findings over 2 rounds) plus my own cold review.
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-11:20260926-163100 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20260926-092600 |
|
Workflow [PR], commit [7b84400] Summary: ❌
AI ReviewSummaryThis PR tightens the macOS PR MetadataThe selected ### Changelog category (leave one):
- Bug Fix (user-visible misbehavior in an official stable release)
### Changelog entry:
Fix `FileLog` on macOS re-reading a file from offset 0 when it is renamed during a directory rescan.Missing context / blind spots
Findings❌ Blockers
Final VerdictChanges requested. LLVM Coverage Report |
Build profile diff (arm_release)Commit See the job log for details. |
CI finish ledger - 026bba6Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20260926-223058 |
…lation for renames too
|
🕵 @groeneai I pushed 8318095 on top of your branch to simplify the fix. Please check it. Changes:
Tests on macOS arm64 with a standalone C++ harness that copies the watcher's scan, watches and drain. One file renamed every 20 µs next to 20 static files, 10 s per run (master: 5 s). Counted: startup scans missing a live file, and loop passes diffing a live file as removed.
Passes still complete under that rename storm, so it does not starve. |
|
I checked 8318095. Moving the drain after One regression from the new order. If a watched file is unlinked while Closing the watches of departed names only once a pass is accepted (moving that loop after the |
|
🕵 @groeneai Agreed, that's a real regression. I confirmed the kernel side on macOS arm64: with a I fixed it in 7b84400 with an extra drain instead of deferring the close. A name missing from Harness (same as before, plus a thread that unlinks and recreates a watched file), counting departed watches closed while their file is unlinked and the name is not in
The rotation leftovers are inodes that were renamed away from the watched name before being unlinked, so their |
CI finish ledger - 7b84400Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
146 of 146 check runs completed (120 passed, 25 not selected, 1 red: the shard above), Session id: cron:our-pr-ci-monitor:20260928-190407 |
1e5291b
On macOS,
FileLogsometimes read a file renamed inside the watched directory again from the beginning, inserting its rows twice. It showed up as a02023_storage_filelogfailure inFast test (arm_darwin): https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=119625&sha=d8052ae24c4059bf592f7b999d09e5c3d6e978ba&name_0=PR&name_1=Fast%20test%20%28arm_darwin%29macOS has no inotify, so
DirectoryWatcherBasediffs directory snapshots and pairs renames by inode. A pass lists the directory, callsstaton each entry and opens a kqueue watch for new names, and none of that is atomic with a rename. A rename landing in between leaves the inode under neither name (or a recreated name under a new inode), so the diff emitsREMOVED+ADDEDinstead of a move andStorageFileLogrestarts the file at offset 0.After installing the watches, the watcher now drains its kqueue. If the directory changed structurally (an entry added, removed or renamed, or a watched file renamed), the pass is discarded and repeated at once, and nothing is emitted from it. Appends never discard a pass. The startup scan uses the same check, so a rename racing table startup is not read again either. The Linux watcher is unchanged.
Related: #119625
Changelog category (leave one):
Changelog entry:
Fix
FileLogon macOS re-reading a file from offset 0 when it is renamed during a directory rescan.Workflow [PR]
Sync PR [sync-upstream/pr/122448]
Version info
26.10.1.944-master(included in26.10and later)