Skip to content

perf(mux): two EPOLL_CTL_DEL per request immediately precede close(2) #1567

Closed

Description

@FlorentinDUBOIS

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) and HttpsSession::close (lib/src/https.rs) deregister the front socket, then the session drops and closes it;
  • Mux::close and the dead-backend sweep in Mux::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 dup or try_clone of a front or back socket. The try_clone calls in Server::new clone the mio Registry, 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_tokens removes the session from the slab, calls close(), and drops the last Rc on the spot. The dead-backend sweep drops its Connection at 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). deregister is a bare epoll_ctl(EPOLL_CTL_DEL) (src/sys/unix/selector/epoll.rs, Selector::deregister), and mio's IoSource has no Drop that needs it.
  • Prior art: HAProxy's epoll poller issues EPOLL_CTL_DEL on close only for FD_CLONED descriptors, i.e. ones shared across processes (src/ev_epoll.c, __fd_clo). Every other fd is left to close(). tokio deregisters in PollEvented::drop because it is a general-purpose library that cannot rule out duplicates, and because its driver must release its own ScheduledIo slot 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions