Repository navigation
Index watchers by the kernel file descriptor - #21
Open
alexey-milovidov wants to merge 3 commits into
Open
alexey-milovidov wants to merge 3 commits into
alexey-milovidov wants to merge 3 commits into
Conversation
With the musl of ClickHouse, the file descriptors visible to the program are a pseudorandom 31-bit encoding of the kernel descriptor and its generation (see `contrib/musl/src/internal/fdmap.h` in ClickHouse), so `loop->watchers[fd]` would need an array of gigabytes. The table is now indexed by the kernel descriptor, which is small. A watcher whose descriptor is already closed is found by a search, and an epoll event of a descriptor that does not match the watcher (closed and reused) is disarmed like an event of a stopped watcher. The kernel reads the descriptors of io_uring submission entries directly, so they are translated too, and the result of an `openat` through io_uring is converted back. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`UV__IORING_OP_CLOSE` closes the kernel descriptor directly, so the generation of the descriptor in the bundled musl of ClickHouse did not change: the stale descriptor was accepted again as soon as another file reused the kernel descriptor. `uv_fs_close` now goes to the thread pool, where `close` of libc invalidates the descriptor. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
With the musl of ClickHouse, the kernel descriptor returned by an io_uring `openat` is converted to a descriptor of the program. A kernel descriptor that is too large for that (at or above `1 << 20`) was converted with the function for existing descriptors, which fails and leaves it alone: the file stayed open in the kernel, where nothing could close it, and the request got the result -1 instead of an error code. Use `__musl_new_fd_from_kernel`, the conversion for a descriptor that the kernel has just created: it closes the descriptor when it cannot be represented and sets `errno` to `EMFILE`. Found by the AI review of ClickHouse/ClickHouse#133404 Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
With the musl of ClickHouse, the file descriptors visible to the program are a pseudorandom 31-bit encoding of the kernel descriptor and its generation, so
loop->watchers[fd]would need an array of gigabytes (RabbitMQ and NATS use libuv).uv__kernel_fd, identity withoutUSE_MUSL).openatthrough io_uring is converted back.Related: ClickHouse/musl#22
🤖 Generated with Claude Code
Note
Medium Risk
Changes the core Linux epoll/io_uring watcher table and FD lookup paths used on every event-loop tick; musl-specific open/close bridging adds further correctness surface.
Overview
Indexes
loop->watchersby kernel FD via newuv__kernel_fd()(identity by default; ClickHouse musl maps encoded program FDs to small kernel slots).uv__io_start,uv__io_stop,uv__fd_exists, anduv_pollnow resize, register, and dedupe watchers using that slot instead of the raw FD.Adds
uv__io_slot()so stopping a watcher still clears the table when the FD is already closed (linear search fallback). On Linuxuv__io_pollresolves epoll events through the kernel slot and ignores events when the watcher’s program FD no longer matches (closed/reused).io_uring submission entries (close, read/write, statx, epoll ctl, etc.) pass kernel FDs. Under
USE_MUSL, kernelCLOSEvia io_uring is skipped when libc must own close;UV_FS_OPENcompletion converts the kernel FD back with__musl_new_fd_from_kernel.Reviewed by Cursor Bugbot for commit 20f0dae. Bugbot is set up for automated code reviews on this repo. Configure here.