mitchellh / libxev

libxev is a cross-platform, high-performance event loop that provides abstractions for non-blocking IO, timers, events, and more and works on Linux (io_uring or epoll), macOS (kqueue), and Wasm + WASI. Available as both a Zig and C API.
MIT License
2.07k stars 74 forks source link

Potential issue in UDP ping-pong benchmark #38

Open Corendos opened 1 year ago

Corendos commented 1 year ago

What's the issue

In the ping-udp1.zig benchmark, there is a potential issue that can arise depending on the order of execution of read/write callbacks.

If everything is fine, the write callback will run first, thus setting the associated Completion to a .dead state and ready to be used again.

However, if the read callback runs first, here is what happens:

Context

I noticed that while working on the IOCP backend. Somehow, the read completion was handled before the write operation and it lead to the described behavior.

Potential solutions

Solution 1 - Simple but kind of misses the point of the bench

Instead of queuing the read operation at the same time as the write operation, we can wait for the write to be completed. However, this introduce artificial latency and despite making the behavior correct, it doesn't really bench anything as operation are serialized.

Solution 2

Another approach would be to wait for both callbacks to have been executed before enqueuing new operations. That would solve the issue of the c_write completion to be reused before its callback has been called.