From 4e8924c3041c6de6050010e33da11432c5535f87 Mon Sep 17 00:00:00 2001 From: altrdev Date: Mon, 19 Oct 2026 20:41:45 +0200 Subject: [PATCH] feat: the descriptors bar compares with the limit of the process when it is lower Co-Authored-By: Claude Sonnet 5.5 --- CHANGELOG.md | 5 +++++ assets/ui.html | 6 +++++- book/src/endpoints.md | 4 ++-- src/cli/info.rs | 13 +++++++++++++ src/cli/view.rs | 20 +++++++++++++++++++- src/routes/info.rs | 2 +- src/snapshot.rs | 4 +++- src/system.rs | 15 +++++++++++++++ tests/observability.rs | 4 +++- 9 files changed, 66 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6656ac7..2fb87f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/assets/ui.html b/assets/ui.html index a05d769..3a91ae8 100644 --- a/assets/ui.html +++ b/assets/ui.html @@ -638,7 +638,11 @@

Run a test

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'); diff --git a/book/src/endpoints.md b/book/src/endpoints.md index f2a7352..238573a 100644 --- a/book/src/endpoints.md +++ b/book/src/endpoints.md @@ -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 } @@ -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 | diff --git a/src/cli/info.rs b/src/cli/info.rs index 80bceac..ba3b564 100644 --- a/src/cli/info.rs +++ b/src/cli/info.rs @@ -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, +} + +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)] diff --git a/src/cli/view.rs b/src/cli/view.rs index a0cf2ac..bfc19ae 100644 --- a/src/cli/view.rs +++ b/src/cli/view.rs @@ -257,7 +257,9 @@ fn rows(style: &Style, info: &Info, history: &History) -> Vec { ), ]; 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)); @@ -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); diff --git a/src/routes/info.rs b/src/routes/info.rs index 929969b..d285b0d 100644 --- a/src/routes/info.rs +++ b/src/routes/info.rs @@ -36,7 +36,7 @@ pub async fn handler(State(state): State>) -> Json { "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, diff --git a/src/snapshot.rs b/src/snapshot.rs index 48a4b37..877c63e 100644 --- a/src/snapshot.rs +++ b/src/snapshot.rs @@ -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, } pub struct Connections { @@ -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(), diff --git a/src/system.rs b/src/system.rs index 24e81a9..7547692 100644 --- a/src/system.rs +++ b/src/system.rs @@ -89,6 +89,15 @@ pub fn container_limit_bytes() -> Option { None } +/// The soft limit of open descriptors of the process (`ulimit -n`), `None` when it has none +pub fn fd_limit() -> Option { + // 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 { let path = CString::new(dir).ok()?; @@ -103,6 +112,12 @@ pub fn free_bytes(dir: &str) -> Option { 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() { diff --git a/tests/observability.rs b/tests/observability.rs index 2333335..8001764 100644 --- a/tests/observability.rs +++ b/tests/observability.rs @@ -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"],