Skip to content

fix(event): report failed read watcher registration - #928

Open
radkesvat wants to merge 2 commits into
ithewei:masterfrom
radkesvat:fix/read-watcher-registration
Open

radkesvat wants to merge 2 commits into
ithewei:masterfrom
radkesvat:fix/read-watcher-registration

Conversation

@radkesvat

Copy link
Copy Markdown
Contributor

On Linux with epoll, passing a regular file to hio_read() returns 0, even though epoll refused to watch it with EPERM. The caller can wait for a read callback that will never arrive. If libhv already has bytes buffered, the same failed call can even deliver those bytes immediately, making the read appear to have started.

What goes wrong

  1. hio_read() asks hio_add() to watch the descriptor for reads.
  2. The backend refuses the request. hio_add() returns an error and leaves the read bit clear.
  3. hio_read() ignores that error, processes any buffered bytes, and returns success. The higher-level hread() also returns an I/O handle.

There is a second case when a socket already has a working read callback. If the caller asks to add write interest with a different callback, hio_add() replaces the callback before the kernel accepts the new interest. If the kernel rejects the change, the next ordinary read can run the replacement callback even though the upgrade failed.

What changes

Read startup now checks registration before installing the requested read mode, callback or buffer. A failed hio_read() returns -1. The hread(), hrecv() and hrecvfrom() helpers return NULL. The handle stays open and buffered data stays available for another read. A successful read can still deliver buffered bytes immediately. A failed interest upgrade keeps the original callback and interest mask.

The loop also checks registration of its own wake reader. If registration fails, it cleans up the wake descriptors and reports startup failure. A caller that owns the loop can retry.

Regression tests

The Linux test uses a regular file for the real epoll refusal. It also starts a read watch on a socket pair, injects one failed interest change, then sends a byte and verifies that the original callback receives it. Other checks cover buffered reads, failed one-shot and framed reads, and loop startup after one failed wake registration.

The test fails on unmodified master with regular file read reported success. It passes in Linux Debug with the shared library and Release with the static library. The existing hdns_test and tcpclient_dns_test also pass in both builds. The regression passed with each Linux wake transport (eventfd, pipe and socket pair) and with the Makefile build.

Run the focused test with CMake.

cmake -S . -B build -DBUILD_UNITTEST=ON -DBUILD_EXAMPLES=OFF
cmake --build build --target read_registration_test
./build/bin/read_registration_test

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