Repository navigation
Description
Every proxied request costs four epoll_ctl calls in the worker. Two are EPOLL_CTL_DEL, and each runs immediately before a close(2) of the same socket:
HttpSession::close(lib/src/http.rs) andHttpsSession::close(lib/src/https.rs) deregister the front socket, then the session drops and closes it;Mux::closeand the dead-backend sweep inMux::ready(lib/src/protocol/mux/mod.rs) deregister each backend socket, then shut it down and drop it.
Linux removes a file from every epoll set on its last close. The EPOLL_CTL_DEL only matters when another descriptor still refers to the same file (a dup, a try_clone, a fork).
Measurement
Release worker (--no-default-features --features crypto-ring,opentelemetry,splice,simd), worker_count = 1, backend python3 -m http.server on loopback, curl N=20 sequential requests, H1 on the cleartext listener, H2 on the TLS listener. Traced with intentrace -p <worker> and an LD_PRELOAD interposer; both give the same counts.
| call per request | H1 | H2 |
|---|---|---|
epoll_ctl total |
4.00 | 4.00 |
of which EPOLL_CTL_ADD |
2.00 | 2.00 |
of which EPOLL_CTL_DEL |
2.00 | 2.00 |
The interposer trace shows the order for each request: back DEL → shutdown(SHUT_WR) → close, then front shutdown(SHUT_WR) → DEL → close. The next request reuses the same slab tokens and fd numbers.
Why the DEL can go
- Nothing duplicates a session socket. The tree has no
duportry_cloneof a front or back socket. Thetry_clonecalls inServer::newclone the mioRegistry, which is the epoll fd, not a watched fd. The splice path uses its own pipes. SCM_RIGHTS (command/src/scm_socket.rs) only carries listeners. Workers never fork: forks happen in the main process (fork_main_into_worker,bin/src/upgrade.rs), which holds no session sockets. - Each socket closes before the next
epoll_wait.shut_down_sessions_by_frontend_tokensremoves the session from the slab, callsclose(), and drops the lastRcon the spot. The dead-backend sweep drops itsConnectionat the end of its block. So a closed socket cannot deliver an event to a recycled slab token. - mio 1.2.3 keeps no per-source state on epoll or kqueue (
src/sys/unix/selector/stateless_io_source.rs).deregisteris a bareepoll_ctl(EPOLL_CTL_DEL)(src/sys/unix/selector/epoll.rs,Selector::deregister), and mio'sIoSourcehas noDropthat needs it. - Prior art: HAProxy's epoll poller issues
EPOLL_CTL_DELon close only forFD_CLONEDdescriptors, i.e. ones shared across processes (src/ev_epoll.c,__fd_clo). Every other fd is left toclose(). tokio deregisters inPollEvented::dropbecause it is a general-purpose library that cannot rule out duplicates, and because its driver must release its ownScheduledIoslot anyway (runtime/io/driver.rs,deregister_source).
Expected result: epoll_ctl 4 → 2 per request on H1 and H2, every other count unchanged, and a stable worker fd count after 1000 requests.
Activity
refactor(lib): remove L7Proxy::deregister_socket (#1618)
perf(tcp): drop the per-session getpeername and EPOLL_CTL_DEL
perf(tcp): drop the per-session getpeername and EPOLL_CTL_DEL
perf(tcp): drop the per-session getpeername and EPOLL_CTL_DEL
perf(tcp): drop the per-session getpeername and EPOLL_CTL_DEL (#1657)