I/O Multiplexing (select vs. poll vs. epoll/kqueue)

nima101.github.io

122 points by pykello 4 days ago


eqvinox - 9 hours ago

> epoll/kqueue are replacements for their deprecated counterparts poll and select.

Neither poll nor select are deprecated. They're just not good fits for particular use patterns. But even select() is fine if you just need to watch 2 FDs in a CLI tool.

In fact, due to its footguns, I'd highly advise against epoll (particularly edge triggering) unless you really need it.

thasso - 9 hours ago

This part is bewildering to me:

> Now, if you try to watch file descriptor 2000, select will loop over fds from 0 to 1999 and will read garbage. The bigger issue is when it tries to set results for a file descriptor past 1024 and tries to set that bit field in say readfds, writefds or errorfds field. At this point it will write something random on the stack eventually crashing the process and making it very hard to debug what happened since your stack is randomized.

I'm not too literate on the Linux kernel code, but I checked, and it looks like the author is right [1].

It would have been so easy to introduce a size check on the array to make sure this can't happen. The man page reads like FD_SETSIZE differs between platforms. It states that FD_SETSIZE is 1024 in glibc, but no upper limit is imposed by the Linux kernel. My guess is that the Linux kernel doesn't want to assume a value of FD_SETSIZE so they leave it unbounded.

It's hard to imagine how anyone came up with this thinking it's a good design. Maybe 1024 FDs was so much at the time when this was designed that nobody considered what would happen if this limit is reached? Or they were working on system where 1024 was the maximum number of FDs that a process can open?

[1]: The core_sys_select function checks the nfds argument passed to select(2) and modifies the fd_set structures that were passed to the system call. The function ensures that n <= max_fds (as the author of the post stated), but it doesn't compare n to the size of the fd_set structures. The set_fd_set function, which modifies the user-side fd_set structures, calls right into __copy_to_user without additional bounds checks. This means page faults will be caught and return -EFAULT, but out-of-bounds accesses that corrupt the user stack are possible.

mort96 - 13 hours ago

I wish the UNIXes had gone together and standardized a modern alternative to poll, maybe as part of POSIX. It sucks that any time I want to listen to IO events, I have to choose between old, low performance, cross-platform APIs and the new, higher-performance but Linux-only epoll.

lukaslalinsky - 7 hours ago

The trouble with I/O multiplexing in a language like C is that the callbacks and state machines get quite complex as you need more functionality. In C++ you can at least do closures, so it's easier to manage. I recently wanted to add networking to my Zig project and decided to do some yak shaving and implemented a fiber runtime with async I/O to avoid the callback complexity. https://github.com/lalinsky/zio

sureglymop - 15 hours ago

Good read but I wish it included io_uring as well.

drewg123 - 6 hours ago

> kqueue (on macOS)

Wish they'd give some credit to FreeBSD, where it originated..

tarruda - 11 hours ago

I have implemented a simple asyncio compatible micro event loop library in python.

The goal was to understand the underlying mechanisms behind python's async/await and to help coworkers understand how event loops work under the hoods.

The end result is somewhat interesting, as unlike traditional event loop libraries, it doesn't use callbacks as the scheduling primitive: https://gist.github.com/tarruda/5b8c19779c8ff4e8100f0b37eb59...

Luker88 - 9 hours ago

I have vague memories of OSX kqueue not supporting all the usecases that FreeBSD kqueue does from many years ago.

Have they reached feature parity?

quibono - 10 hours ago

I'm assuming epoll is covered implicitly by the section on kqueue. Are there any differences between the two besides the name?

lynx97 - 13 hours ago

There is no mention of epoll in thsi other then the heading.

khaledh - 9 hours ago

Needs "(2020)" in the title.

commandersaki - 11 hours ago

Nice article, though a few spelling mistakes that I thought was to distinguish it from AI slop, only to realise this was written a few years before the AI/GPT craze.