use std::future::Future;
use std::io;
use std::sync::OnceLock;
use std::time::{Duration, Instant};
use tokio::sync::{Semaphore, SemaphorePermit};
pub(crate) type Descriptor = SemaphorePermit<'static>;
pub(crate) fn gate() -> &'static Semaphore {
static GATE: OnceLock<Semaphore> = OnceLock::new();
GATE.get_or_init(|| Semaphore::new(budget()))
}
pub(crate) fn descriptor_blocking(runtime: &tokio::runtime::Handle) -> Descriptor {
runtime
.block_on(gate().acquire())
.expect("the descriptor gate is never closed")
}
pub(crate) const PATIENCE: Duration = Duration::from_secs(10);
pub(crate) fn patience() -> Duration {
#[cfg(all(test, unix))]
if let Some(patience) = testing::patience() {
return patience;
}
PATIENCE
}
pub(crate) const FIRST_PAUSE: Duration = Duration::from_millis(10);
pub(crate) const LONGEST_PAUSE: Duration = Duration::from_millis(250);
pub(crate) async fn patiently<T, F, Fut>(patience: Duration, mut attempt: F) -> io::Result<T>
where
F: FnMut() -> Fut,
Fut: Future<Output = io::Result<T>>,
{
let mut refused_since: Option<Instant> = None;
let mut pause = FIRST_PAUSE;
loop {
match attempt().await {
Err(e) if exhausted(&e) => {
if refused_since.get_or_insert_with(Instant::now).elapsed() >= patience {
return Err(e);
}
tokio::time::sleep(pause).await;
pause = (pause * 2).min(LONGEST_PAUSE);
}
result => return result,
}
}
}
pub(crate) fn patiently_blocking<T>(
patience: Duration,
mut attempt: impl FnMut() -> io::Result<T>,
) -> io::Result<T> {
let mut refused_since: Option<Instant> = None;
let mut pause = FIRST_PAUSE;
loop {
match attempt() {
Err(e) if exhausted(&e) => {
if refused_since.get_or_insert_with(Instant::now).elapsed() >= patience {
return Err(e);
}
std::thread::sleep(pause);
pause = (pause * 2).min(LONGEST_PAUSE);
}
result => return result,
}
}
}
pub(crate) fn starved(patience: Duration) -> String {
let limit = soft_limit()
.map(|limit| format!(" of {limit}"))
.unwrap_or_default();
format!("file descriptor limit{limit} reached, no socket free within {patience:?}")
}
pub(crate) fn starved_briefly() -> String {
match soft_limit() {
Some(limit) => format!("file limit {limit}"),
None => String::from("file limit"),
}
}
pub(crate) const RESERVE: usize = 16;
pub(crate) const OPENED_WHILE_RUNNING: usize = 8;
pub(crate) fn budget() -> usize {
budget_within(soft_limit())
}
fn budget_within(soft: Option<usize>) -> usize {
soft.map_or(Semaphore::MAX_PERMITS, |soft| {
soft.saturating_sub(reserve_within(soft))
.clamp(1, Semaphore::MAX_PERMITS)
})
}
fn reserve_within(soft: usize) -> usize {
(soft / 2).max(RESERVE)
}
pub(crate) fn too_few(captures: usize) -> Option<(usize, usize)> {
let soft = soft_limit();
too_few_within(soft, soft.and_then(open_descriptors), captures)
}
pub(crate) fn hold_back() -> Option<Descriptor> {
let soft = soft_limit()?;
let open = open_descriptors(soft)?;
let gate = gate();
let excess = held_back_within(gate.available_permits(), soft, open);
if excess == 0 {
return None;
}
gate.try_acquire_many(u32::try_from(excess).ok()?).ok()
}
fn held_back_within(available: usize, soft: usize, open: usize) -> usize {
let room = soft.saturating_sub(open + OPENED_WHILE_RUNNING).max(1);
available.saturating_sub(room)
}
fn too_few_within(
soft: Option<usize>,
open: Option<usize>,
captures: usize,
) -> Option<(usize, usize)> {
let beside = open.map_or(RESERVE, |open| open + OPENED_WHILE_RUNNING);
let needed = beside + captures + 1;
soft.filter(|&soft| soft < needed)
.map(|soft| (soft, needed))
}
fn open_descriptors(soft: usize) -> Option<usize> {
#[cfg(any(target_os = "linux", target_os = "macos"))]
{
let below_the_limit = |entry: std::io::Result<std::fs::DirEntry>| {
entry.ok().and_then(|entry| {
let number = entry.file_name().to_str()?.parse::<usize>().ok()?;
(number < soft).then_some(())
})
};
match std::fs::read_dir("/dev/fd") {
Ok(listing) => Some(
listing
.filter_map(below_the_limit)
.count()
.saturating_sub(1),
),
Err(error) if exhausted(&error) => Some(soft),
Err(_) => None,
}
}
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
{
let _ = soft;
None
}
}
pub(crate) fn soft_limit() -> Option<usize> {
#[cfg(unix)]
{
let mut bounds = libc::rlimit {
rlim_cur: 0,
rlim_max: 0,
};
if unsafe { libc::getrlimit(libc::RLIMIT_NOFILE, &mut bounds) } != 0 {
return None;
}
if bounds.rlim_cur == libc::RLIM_INFINITY {
return None;
}
usize::try_from(bounds.rlim_cur).ok()
}
#[cfg(not(unix))]
{
None
}
}
pub(crate) fn exhausted(error: &io::Error) -> bool {
let Some(code) = error.raw_os_error() else {
return false;
};
#[cfg(unix)]
{
code == libc::EMFILE || code == libc::ENFILE
}
#[cfg(windows)]
{
use windows_sys::Win32::Networking::WinSock::{WSAEMFILE, WSAENOBUFS};
code == WSAEMFILE || code == WSAENOBUFS
}
#[cfg(not(any(unix, windows)))]
{
let _ = code;
false
}
}
#[cfg(all(test, unix))]
pub(crate) mod testing {
pub(crate) use crate::testing::own_process::in_a_process_of_its_own;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Duration;
static PATIENCE_MS: AtomicU64 = AtomicU64::new(0);
pub(crate) fn wait_out_a_full_table_for(patience: Duration) {
let millis = u64::try_from(patience.as_millis())
.unwrap_or(u64::MAX)
.max(1);
PATIENCE_MS.store(millis, Ordering::Relaxed);
}
pub(super) fn patience() -> Option<Duration> {
match PATIENCE_MS.load(Ordering::Relaxed) {
0 => None,
millis => Some(Duration::from_millis(millis)),
}
}
pub(crate) struct Refusing(libc::rlimit);
pub(crate) fn refuse_every_descriptor() -> Refusing {
let mut bounds = libc::rlimit {
rlim_cur: 0,
rlim_max: 0,
};
unsafe {
assert_eq!(libc::getrlimit(libc::RLIMIT_NOFILE, &mut bounds), 0);
let was = bounds;
bounds.rlim_cur = 0;
assert_eq!(libc::setrlimit(libc::RLIMIT_NOFILE, &bounds), 0);
Refusing(was)
}
}
impl Drop for Refusing {
fn drop(&mut self) {
unsafe {
libc::setrlimit(libc::RLIMIT_NOFILE, &self.0);
}
}
}
pub(crate) fn exhaust(limit: libc::rlim_t) -> Vec<std::fs::File> {
let mut bounds = libc::rlimit {
rlim_cur: 0,
rlim_max: 0,
};
unsafe {
assert_eq!(libc::getrlimit(libc::RLIMIT_NOFILE, &mut bounds), 0);
bounds.rlim_cur = limit;
assert_eq!(libc::setrlimit(libc::RLIMIT_NOFILE, &bounds), 0);
}
let mut held = Vec::new();
loop {
match std::fs::File::open("/dev/null") {
Ok(file) => held.push(file),
Err(e) if e.raw_os_error() == Some(libc::EMFILE) => return held,
Err(e) => panic!("filling the descriptor table: {e}"),
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_connect_paths_take_half_of_what_the_process_may_hold() {
assert_eq!(budget_within(Some(256)), 128, "a macOS Terminal");
assert_eq!(budget_within(Some(1024)), 512, "a Linux shell");
assert_eq!(budget_within(None), Semaphore::MAX_PERMITS);
assert_eq!(budget_within(Some(usize::MAX)), Semaphore::MAX_PERMITS);
}
#[test]
fn a_small_limit_keeps_the_reserve_and_gives_the_scan_the_rest() {
assert_eq!(
budget_within(Some(48)),
24,
"half, which is above the reserve"
);
assert_eq!(budget_within(Some(24)), 24 - RESERVE);
assert_eq!(budget_within(Some(RESERVE + 1)), 1);
assert_eq!(
budget_within(Some(RESERVE)),
1,
"a gate is never empty, so a connection past the refusal still runs"
);
}
#[test]
fn a_limit_that_leaves_no_socket_beside_the_reserve_refuses_the_scan() {
assert_eq!(too_few_within(Some(16), None, 0), Some((16, RESERVE + 1)));
assert_eq!(too_few_within(Some(0), None, 0), Some((0, RESERVE + 1)));
assert_eq!(too_few_within(Some(RESERVE + 1), None, 0), None);
assert_eq!(too_few_within(Some(256), None, 0), None);
assert_eq!(
too_few_within(None, None, 0),
None,
"no limit to fall short of"
);
}
#[test]
fn what_is_open_already_is_needed_beside_what_the_scan_opens() {
assert_eq!(
too_few_within(Some(64), Some(57), 0),
Some((64, 57 + OPENED_WHILE_RUNNING + 1))
);
assert_eq!(too_few_within(Some(64), Some(10), 0), None);
assert_eq!(too_few_within(Some(256), Some(64), 0), None);
assert_eq!(too_few_within(None, Some(57), 0), None);
}
#[test]
fn a_raw_scan_needs_a_descriptor_for_every_link_it_captures_on() {
assert_eq!(too_few_within(Some(64), Some(30), 0), None, "by connect");
assert_eq!(
too_few_within(Some(64), Some(30), 28),
Some((64, 30 + OPENED_WHILE_RUNNING + 28 + 1))
);
assert_eq!(too_few_within(Some(256), Some(30), 28), None);
}
#[cfg(unix)]
#[test]
fn a_table_already_nearly_full_refuses_the_scan_whatever_the_limit() {
if !testing::in_a_process_of_its_own(
module_path!(),
"a_table_already_nearly_full_refuses_the_scan_whatever_the_limit",
) {
return;
}
let mut held = testing::exhaust(64);
held.truncate(held.len() - 7);
let refused = too_few(0);
drop(held);
let (limit, needed) = refused.expect("a scan with seven descriptors free");
assert_eq!(limit, 64);
assert!(
needed > 64 - 7 + OPENED_WHILE_RUNNING,
"{needed} names no limit that would hold what is open and the scan"
);
}
#[test]
fn a_gate_is_held_to_the_sockets_the_table_has_room_for() {
assert_eq!(too_few_within(Some(64), Some(40), 0), None);
assert_eq!(held_back_within(budget_within(Some(64)), 64, 40), 32 - 16);
assert_eq!(held_back_within(budget_within(Some(256)), 256, 10), 0);
assert_eq!(held_back_within(32 - 5, 64, 40 + 5), 32 - 16);
assert_eq!(held_back_within(32, 64, 64), 31);
}
#[test]
fn what_the_process_holds_of_its_own_is_counted_once() {
let open = 64 - 24 + 7;
let gate = budget_within(Some(64));
let left = gate - held_back_within(gate, 64, open);
assert_eq!(
left,
64 - open - OPENED_WHILE_RUNNING,
"every socket the table has beside what the scan opens as it runs"
);
assert!(left > 1, "{left} connection at a time");
}
#[cfg(unix)]
#[test]
fn a_scan_in_a_crowded_table_holds_back_what_it_has_no_socket_for() {
use std::os::fd::{AsRawFd, FromRawFd, OwnedFd};
if !testing::in_a_process_of_its_own(
module_path!(),
"a_scan_in_a_crowded_table_holds_back_what_it_has_no_socket_for",
) {
return;
}
let null = std::fs::File::open("/dev/null").expect("a descriptor to copy");
let above = unsafe {
let copy = libc::fcntl(null.as_raw_fd(), libc::F_DUPFD, 100);
assert!(copy >= 100, "a descriptor above the limit");
OwnedFd::from_raw_fd(copy)
};
drop(null);
let mut held = testing::exhaust(64);
let free = 24;
held.truncate(held.len() - free);
assert_eq!(
too_few(0),
None,
"a table with {free} free passes the refusal"
);
let whole = gate().available_permits();
let held_back = hold_back();
let left = gate().available_permits();
drop(held_back);
let returned = gate().available_permits();
drop((held, above));
assert_eq!(whole, 32);
assert_eq!(
left,
free - OPENED_WHILE_RUNNING,
"permits left for {free} free descriptors, {OPENED_WHILE_RUNNING} of them \
what the scan opens as it runs"
);
assert_eq!(returned, whole);
}
#[cfg(unix)]
#[tokio::test]
async fn a_full_table_is_waited_out_rather_than_passed_on() {
let mut refusals = 3;
let outcome = patiently(Duration::from_secs(10), || {
let refused = refusals > 0;
refusals -= 1;
async move {
match refused {
true => Err(io::Error::from_raw_os_error(libc::EMFILE)),
false => Ok("connected"),
}
}
})
.await;
assert_eq!(
outcome.expect("the fourth attempt had a socket"),
"connected"
);
let mut refusals = 3;
let outcome = patiently_blocking(Duration::from_secs(10), || {
refusals -= 1;
match refusals {
0.. => Err(io::Error::from_raw_os_error(libc::ENFILE)),
_ => Ok("connected"),
}
});
assert_eq!(outcome.expect("a later attempt had a socket"), "connected");
}
#[cfg(unix)]
#[tokio::test]
async fn the_wait_ends_on_its_patience_and_nothing_else_is_waited_on() {
let outcome: io::Result<()> = patiently(Duration::from_millis(30), || async {
Err(io::Error::from_raw_os_error(libc::EMFILE))
})
.await;
assert!(
outcome.is_err_and(|e| exhausted(&e)),
"the refusal is named"
);
let mut attempts = 0;
let outcome: io::Result<()> = patiently(Duration::from_secs(10), || {
attempts += 1;
async { Err(io::Error::from_raw_os_error(libc::ECONNREFUSED)) }
})
.await;
assert!(outcome.is_err_and(|e| !exhausted(&e)));
assert_eq!(attempts, 1, "a target's answer was asked again");
}
#[cfg(unix)]
#[test]
fn a_full_descriptor_table_is_told_apart_from_a_target_s_answer() {
assert!(exhausted(&io::Error::from_raw_os_error(libc::EMFILE)));
assert!(exhausted(&io::Error::from_raw_os_error(libc::ENFILE)));
assert!(!exhausted(&io::Error::from_raw_os_error(
libc::ECONNREFUSED
)));
assert!(!exhausted(&io::Error::from_raw_os_error(libc::ETIMEDOUT)));
assert!(!exhausted(&io::ErrorKind::TimedOut.into()));
}
}