Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

All notable changes to this project are documented here. The GitHub release of each version uses the matching section.

## Unreleased

### Changed
- `/info` reports the limit of descriptors of the process (`fd.processLimit`, the soft `ulimit -n`), and the descriptors bar of `watch` and of the control panel compares with the lower of that and `MAX_FD`, so it is full when `EMFILE` comes and not at a quarter of a limit the process can't reach. `watch` marks it with `(ulimit)`.

## 4.6.0 - 2026-10-19

### Added
Expand Down
6 changes: 5 additions & 1 deletion assets/ui.html
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,11 @@ <h2>Run a test</h2>

gauges.forEach((g) => draw(g.key, g.get(info)));
if (info.connections) draw('connections', {v: info.connections.held, max: info.connections.limit, text: `${info.connections.held} / ${info.connections.limit}`, cap: 'held by /connections'});
if (info.fd) draw('fd', {v: info.fd.held, max: info.fd.limit, text: `${info.fd.held} / ${info.fd.limit}`, cap: 'held by /fd'});
if (info.fd) {
const byProcess = info.fd.processLimit != null && info.fd.processLimit < info.fd.limit;
const max = byProcess ? info.fd.processLimit : info.fd.limit;
draw('fd', {v: info.fd.held, max, text: `${info.fd.held} / ${max}`, cap: byProcess ? 'held by /fd, process limit (ulimit -n)' : 'held by /fd'});
}
requests(metrics);
} catch (e) {
setTab('bad', 'torchio · unreachable');
Expand Down
4 changes: 2 additions & 2 deletions book/src/endpoints.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ Returns a JSON document with what the server is doing right now. It takes no par
"delay": { "inFlight": 3 },
"memory": { "heldMb": 200, "limitMb": 512, "processRssMb": 310, "containerLimitMb": 256 },
"disk": { "heldMb": 100, "limitMb": 1024, "dir": "/tmp", "freeMb": 8120 },
"fd": { "held": 0, "limit": 10000 },
"fd": { "held": 0, "limit": 10000, "processLimit": 1048576 },
"ready": { "ready": true, "secondsLeft": null },
"log": null,
"limits": { "maxNumber": 50, "maxDelayMs": 60000, "maxHoldSeconds": 300, "maxLogLines": 100000, "maxLogRate": 10000 }
Expand All @@ -318,7 +318,7 @@ Returns a JSON document with what the server is doing right now. It takes no par
| `memory.containerLimitMb` | Memory limit of the container, `null` when there is none |
| `disk.heldMb`, `limitMb` | Same as for memory, for `/disk` (`MAX_DISK_MB`) |
| `disk.freeMb` | Free space in `DISK_DIR`, `null` if the directory is not usable |
| `fd.held`, `limit` | File descriptors held by `/fd` and the total it may hold (`MAX_FD`) |
| `fd.held`, `limit`, `processLimit` | File descriptors held by `/fd`, the total it may hold (`MAX_FD`) and the limit of the process itself (`ulimit -n`, `null` when it has none) |
| `connections.held`, `limit` | Connections held by `/connections` and the total it may hold (`MAX_CONNECTIONS`) |
| `log` | What `/log` is writing, `{ "sent", "lines", "rate" }`, or `null` when it is not running |
| `shutdown` | `{ "draining", "graceSeconds", "drainSeconds", "openRequests", "openConnections" }`: whether SIGTERM started a [drain](shutdown.md#shutdown), for how long (`null` if not), and what is open now, the request for `/info` and its connection included |
Expand Down
13 changes: 13 additions & 0 deletions src/cli/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,22 @@ pub struct Connections {
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Fd {
pub held: i64,
pub limit: i64,
pub process_limit: Option<i64>,
}

impl Fd {
/// What a bar should compare with: the budget of `/fd`, or the limit of the process when it is lower,
/// which is where `EMFILE` comes first. The flag says it is the process
pub fn ceiling(&self) -> (i64, bool) {
match self.process_limit {
Some(process) if process < self.limit => (process, true),
_ => (self.limit, false),
}
}
}

#[derive(Debug, Deserialize)]
Expand Down
20 changes: 19 additions & 1 deletion src/cli/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,9 @@ fn rows(style: &Style, info: &Info, history: &History) -> Vec<String> {
),
];
if let Some(fd) = &info.fd {
rows.push(style.row("FDs held", fd.held, fd.limit, &format!("{} / {}", fd.held, fd.limit), &history.fd, false));
let (ceiling, by_process) = fd.ceiling();
let text = format!("{} / {}{}", fd.held, ceiling, if by_process { " (ulimit)" } else { "" });
rows.push(style.row("FDs held", fd.held, ceiling, &text, &history.fd, false));
}
if let Some(c) = &info.connections {
rows.push(style.row("Connections", c.held, c.limit, &format!("{} / {}", c.held, c.limit), &history.connections, false));
Expand Down Expand Up @@ -369,6 +371,22 @@ mod tests {
render(&Style::new(false), "http://x:3000", &info, &history)
}

#[test]
fn the_fd_bar_compares_with_the_lower_of_the_budget_and_the_process_limit() {
let low =
INFO.replace("\"fd\": {\"held\": 40, \"limit\": 10000}", "\"fd\": {\"held\": 2549, \"limit\": 10000, \"processLimit\": 2560}");
assert!(plain(&low).contains("FDs held ████████████████████ 2549 / 2560 (ulimit)"), "{}", plain(&low));
let high =
INFO.replace("\"fd\": {\"held\": 40, \"limit\": 10000}", "\"fd\": {\"held\": 40, \"limit\": 10000, \"processLimit\": 1048576}");
assert!(
plain(&high).contains("FDs held ░░░░░░░░░░░░░░░░░░░░ 40 / 10000") && !plain(&high).contains("ulimit"),
"a higher limit of the process changes nothing"
);
let none =
INFO.replace("\"fd\": {\"held\": 40, \"limit\": 10000}", "\"fd\": {\"held\": 40, \"limit\": 10000, \"processLimit\": null}");
assert!(plain(&none).contains("40 / 10000") && !plain(&none).contains("ulimit"), "no limit of the process changes nothing");
}

#[test]
fn the_view_has_every_figure() {
let view = plain(INFO);
Expand Down
2 changes: 1 addition & 1 deletion src/routes/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub async fn handler(State(state): State<Arc<AppState>>) -> Json<Value> {
"dir": snapshot.disk.dir,
"freeMb": snapshot.disk.free_bytes.map(to_mb),
},
"fd": {"held": snapshot.fd.held, "limit": snapshot.fd.limit},
"fd": {"held": snapshot.fd.held, "limit": snapshot.fd.limit, "processLimit": snapshot.fd.process_limit},
"connections": {"held": snapshot.connections.held, "limit": snapshot.connections.limit},
"ready": snapshot.ready,
"block": snapshot.block,
Expand Down
4 changes: 3 additions & 1 deletion src/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub struct Disk {
pub struct Fd {
pub held: i64,
pub limit: i64,
/// What the process can open at all (`ulimit -n`), which may be lower than `limit`
pub process_limit: Option<u64>,
}

pub struct Connections {
Expand Down Expand Up @@ -61,7 +63,7 @@ impl AppState {
dir: self.config.disk_dir.clone(),
free_bytes: system::free_bytes(&self.config.disk_dir),
},
fd: Fd { held: self.fd.budget().held(), limit: self.fd.budget().limit() },
fd: Fd { held: self.fd.budget().held(), limit: self.fd.budget().limit(), process_limit: system::fd_limit() },
connections: Connections { held: self.connections.budget().held(), limit: self.connections.budget().limit() },
ready: self.ready.state(),
block: self.block.snapshot(),
Expand Down
15 changes: 15 additions & 0 deletions src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ pub fn container_limit_bytes() -> Option<u64> {
None
}

/// The soft limit of open descriptors of the process (`ulimit -n`), `None` when it has none
pub fn fd_limit() -> Option<u64> {
// SAFETY: `limits` is plain data that getrlimit fills in
unsafe {
let mut limits: libc::rlimit = std::mem::zeroed();
(libc::getrlimit(libc::RLIMIT_NOFILE, &mut limits) == 0 && limits.rlim_cur != libc::RLIM_INFINITY).then_some(limits.rlim_cur as u64)
}
}

/// Free space for an unprivileged process in the file system of `dir`, `None` when it can't be read
pub fn free_bytes(dir: &str) -> Option<u64> {
let path = CString::new(dir).ok()?;
Expand All @@ -103,6 +112,12 @@ pub fn free_bytes(dir: &str) -> Option<u64> {
mod tests {
use super::*;

#[test]
fn the_process_has_a_limit_of_descriptors() {
// there is always a soft limit, a few at least, unless it is unlimited
assert!(fd_limit().is_none_or(|limit| limit > 3));
}

#[test]
#[cfg(target_os = "linux")]
fn limits() {
Expand Down
4 changes: 3 additions & 1 deletion tests/observability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ fn info_describes_the_server() {
assert_eq!(info["disk"]["limitMb"], 300);
assert_eq!(info["disk"]["dir"], server.disk_dir.path().to_str().unwrap());
assert!(info["disk"]["freeMb"].as_i64().unwrap() > 0);
assert_eq!(info["fd"], json!({"held": 0, "limit": 10000}));
assert_eq!((&info["fd"]["held"], &info["fd"]["limit"]), (&json!(0), &json!(10000)));
// the limit of the process (`ulimit -n`), a number or null when there is none
assert!(info["fd"]["processLimit"].is_u64() || info["fd"]["processLimit"].is_null(), "{}", info["fd"]);
assert!(info["log"].is_null());
assert_eq!(
info["limits"],
Expand Down
Loading