Skip to content

Index watchers by the kernel file descriptor - #21

Open
alexey-milovidov wants to merge 3 commits into
ClickHouse/v1.53.0from
fd-randomization
Open

alexey-milovidov wants to merge 3 commits into
ClickHouse/v1.53.0from
fd-randomization

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

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

  • The table of watchers is indexed by the kernel descriptor (uv__kernel_fd, identity without USE_MUSL).
  • A watcher whose descriptor is already closed is found by a search, so no stale pointer stays in the table.
  • An epoll event whose descriptor does not match the watcher (closed and reused) is disarmed like an event of a stopped watcher.
  • io_uring submission entries get kernel descriptors, and the result of openat through 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->watchers by kernel FD via new uv__kernel_fd() (identity by default; ClickHouse musl maps encoded program FDs to small kernel slots). uv__io_start, uv__io_stop, uv__fd_exists, and uv_poll now 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 Linux uv__io_poll resolves 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, kernel CLOSE via io_uring is skipped when libc must own close; UV_FS_OPEN completion 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.

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>
alexey-milovidov and others added 2 commits November 29, 2026 20:35
`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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant