Kqueue + Libevent

about bus aboutbus at gmail.com
Fri Aug 19 11:03:01 UTC 2011


I've got some interesting problem with my own server which use Libevent and
Kqueue holds some sockets for a long time (while reading data) and gives it
back to application when another side closes connection.

I have server with FreeBSD 7.2 which serves several thousands requests per
Nginx -> statical requests, images
         -> backend for dynamical requests: Libevent based http server
written on C

Sometimes, about 0.1% of request, Nginx displays error: "Operation timed out
while reading response from upstream."
Switching Libevent from Kqueue to Poll in my http server fixes the problem.
Poll works fine without errors in Nginx log file.

Debug output for timeouted requests from Libevent:
1) Libevent accepts incoming connection from Nginx.
2) Libevent adds socket to Kqueue for waiting incoming data with HTTP
3) No events on socket for 60 seconds, no received data. (60 - timeout value
in Nginx config)
4) On the another side Nginx closes connection and displays timeout error.
5) At the same time Kqueue reports about incoming data and returns socket to
6) Libevent reads correct HTTP request from socket and passes it to my

Why Kqueue does not reports about read event for so long time?
Why Kqueue do it only when another side closes connection?
It is strange, because Poll does not have such problems.

More information about the freebsd-hackers mailing list