Skip to content

Add a raw fd accessor to SocketDigest - #1127

Open
starjoe wants to merge 1 commit into
cloudflare:mainfrom
starjoe:socket-digest-raw-fd
Open

starjoe wants to merge 1 commit into
cloudflare:mainfrom
starjoe:socket-digest-raw-fd

Conversation

@starjoe

@starjoe starjoe commented Nov 17, 2026

Copy link
Copy Markdown

What

Adds SocketDigest::raw_fd() on unix, next to the existing socket_cookie():

#[cfg(unix)]
pub fn raw_fd(&self) -> std::os::unix::io::RawFd

It returns the fd that the digest already stores and uses internally (socket_cookie(), tcp_info(), get_recv_buf(), ...). The fd stays owned by the connection; the doc comment says that callers may read or set socket options on it but must not close it or do I/O on it.

Why

On an HTTP/1.x session the downstream fd is reachable through Session::stream(). For HTTP/2 sessions stream() returns None, and the session's Digest is the only handle to the downstream connection. An application that needs a socket option Pingora does not wrap has no direct way to get the fd there.

We are building a proxy on top of Pingora and needed this to abort a downstream connection with a TCP RST (SO_LINGER with a zero timeout). On 0.9.1 the only way we found was to take socket_cookie() and scan /proc/self/fd, comparing each socket's SO_COOKIE, which is Linux-only and scans every fd of the process. With this accessor the lookup is not needed.

This is a small, additive change, so I did not open a separate issue first; happy to open one if you prefer. #571 describes a related need (closing a TCP connection from the application, currently done by collecting the fd).

Compatibility

Additive only; no existing signature or behavior changes. Like socket_cookie(), the method exists on unix only. On Windows the digest stores a raw socket instead; I did not add an accessor for it, but can if wanted.

Tests

raw_fd_returns_the_socket_fd in protocols/digest.rs, in the existing Linux-only test module next to the socket_cookie() tests: for a connected client and an accepted server TcpStream, raw_fd() returns the same fd as as_raw_fd().

Checks run locally (Debian 12, x86_64), following .github/workflows/build.yml:

  • cargo +1.97.1 fmt --all -- --check: clean
  • cargo +1.97.1 clippy --all-targets --all -- --allow=unknown-lints --deny=warnings: clean
  • cargo +1.97.1 test -p pingora-core: all pass except test_h2_late_frames_on_cancelled_streams_keep_connection, which also fails on main here and is skipped in CI
  • cargo +1.85.0 check -p pingora-core: clean

I did not run the pingora-proxy test suite.

🤖 Generated with Claude Code

`SocketDigest` keeps the socket's fd, but only uses it internally, for
example in `socket_cookie()` and `tcp_info()`. On an HTTP/1.x session the
fd is reachable through `Session::stream()`, but for HTTP/2 sessions
`stream()` returns `None` and the digest is the only handle to the
downstream connection. Applications that need a socket option that
Pingora does not wrap have no way to get the fd there.

Add `SocketDigest::raw_fd()` on unix, next to `socket_cookie()`. The fd
stays owned by the connection; the doc comment says callers must not close
it or do I/O on it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant