main: Increase soft file limit

This commit is contained in:
Victoria Brekenfeld 2024-04-03 18:08:43 +02:00 committed by Victoria Brekenfeld
parent fa7926f7c1
commit 6ba7242cfc
6 changed files with 60 additions and 21 deletions

41
Cargo.lock generated
View file

@ -280,7 +280,7 @@ dependencies = [
"futures-lite 2.2.0", "futures-lite 2.2.0",
"parking", "parking",
"polling 3.3.2", "polling 3.3.2",
"rustix 0.38.30", "rustix 0.38.32",
"slab", "slab",
"tracing", "tracing",
"windows-sys 0.52.0", "windows-sys 0.52.0",
@ -319,7 +319,7 @@ dependencies = [
"cfg-if", "cfg-if",
"event-listener 3.1.0", "event-listener 3.1.0",
"futures-lite 1.13.0", "futures-lite 1.13.0",
"rustix 0.38.30", "rustix 0.38.32",
"windows-sys 0.48.0", "windows-sys 0.48.0",
] ]
@ -346,7 +346,7 @@ dependencies = [
"cfg-if", "cfg-if",
"futures-core", "futures-core",
"futures-io", "futures-io",
"rustix 0.38.30", "rustix 0.38.32",
"signal-hook-registry", "signal-hook-registry",
"slab", "slab",
"windows-sys 0.48.0", "windows-sys 0.48.0",
@ -380,7 +380,7 @@ name = "atomicwrites"
version = "0.4.2" version = "0.4.2"
source = "git+https://github.com/jackpot51/rust-atomicwrites#043ab4859d53ffd3d55334685303d8df39c9f768" source = "git+https://github.com/jackpot51/rust-atomicwrites#043ab4859d53ffd3d55334685303d8df39c9f768"
dependencies = [ dependencies = [
"rustix 0.38.30", "rustix 0.38.32",
"tempfile", "tempfile",
"windows-sys 0.48.0", "windows-sys 0.48.0",
] ]
@ -551,7 +551,7 @@ dependencies = [
"bitflags 2.4.2", "bitflags 2.4.2",
"log", "log",
"polling 3.3.2", "polling 3.3.2",
"rustix 0.38.30", "rustix 0.38.32",
"slab", "slab",
"thiserror", "thiserror",
] ]
@ -566,7 +566,7 @@ dependencies = [
"bitflags 2.4.2", "bitflags 2.4.2",
"log", "log",
"polling 3.3.2", "polling 3.3.2",
"rustix 0.38.30", "rustix 0.38.32",
"slab", "slab",
"thiserror", "thiserror",
] ]
@ -578,7 +578,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f0ea9b9476c7fad82841a8dbb380e2eae480c21910feba80725b46931ed8f02" checksum = "0f0ea9b9476c7fad82841a8dbb380e2eae480c21910feba80725b46931ed8f02"
dependencies = [ dependencies = [
"calloop 0.12.4", "calloop 0.12.4",
"rustix 0.38.30", "rustix 0.38.32",
"wayland-backend", "wayland-backend",
"wayland-client", "wayland-client",
] ]
@ -868,6 +868,7 @@ dependencies = [
"renderdoc", "renderdoc",
"ron", "ron",
"rust-embed", "rust-embed",
"rustix 0.38.32",
"sanitize-filename", "sanitize-filename",
"sendfd", "sendfd",
"serde", "serde",
@ -1260,7 +1261,7 @@ dependencies = [
"bytemuck", "bytemuck",
"drm-ffi", "drm-ffi",
"drm-fourcc", "drm-fourcc",
"rustix 0.38.30", "rustix 0.38.32",
] ]
[[package]] [[package]]
@ -1270,7 +1271,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41334f8405792483e32ad05fbb9c5680ff4e84491883d2947a4757dc54cb2ac6" checksum = "41334f8405792483e32ad05fbb9c5680ff4e84491883d2947a4757dc54cb2ac6"
dependencies = [ dependencies = [
"drm-sys", "drm-sys",
"rustix 0.38.30", "rustix 0.38.32",
] ]
[[package]] [[package]]
@ -2770,9 +2771,9 @@ checksum = "03087c2bad5e1034e8cace5926dec053fb3790248370865f5117a7d0213354c8"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.152" version = "0.2.153"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13e3bf6590cbc649f4d1a3eefc9d5d6eb746f5200ffb04e5e142700b8faa56e7" checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd"
[[package]] [[package]]
name = "libcosmic" name = "libcosmic"
@ -3809,7 +3810,7 @@ dependencies = [
"cfg-if", "cfg-if",
"concurrent-queue", "concurrent-queue",
"pin-project-lite", "pin-project-lite",
"rustix 0.38.30", "rustix 0.38.32",
"tracing", "tracing",
"windows-sys 0.52.0", "windows-sys 0.52.0",
] ]
@ -4265,9 +4266,9 @@ dependencies = [
[[package]] [[package]]
name = "rustix" name = "rustix"
version = "0.38.30" version = "0.38.32"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "322394588aaf33c24007e8bb3238ee3e4c5c09c084ab32bc73890b99ff326bca" checksum = "65e04861e65f21776e67888bfbea442b3642beaa0138fdb1dd7a84a52dffdb89"
dependencies = [ dependencies = [
"bitflags 2.4.2", "bitflags 2.4.2",
"errno", "errno",
@ -4533,7 +4534,7 @@ dependencies = [
"pkg-config", "pkg-config",
"profiling", "profiling",
"rand", "rand",
"rustix 0.38.30", "rustix 0.38.32",
"scan_fmt", "scan_fmt",
"scopeguard", "scopeguard",
"smallvec", "smallvec",
@ -4566,7 +4567,7 @@ dependencies = [
"libc", "libc",
"log", "log",
"memmap2 0.9.3", "memmap2 0.9.3",
"rustix 0.38.30", "rustix 0.38.32",
"thiserror", "thiserror",
"wayland-backend", "wayland-backend",
"wayland-client", "wayland-client",
@ -4648,7 +4649,7 @@ dependencies = [
"objc", "objc",
"raw-window-handle", "raw-window-handle",
"redox_syscall 0.4.1", "redox_syscall 0.4.1",
"rustix 0.38.30", "rustix 0.38.32",
"tiny-xlib", "tiny-xlib",
"wasm-bindgen", "wasm-bindgen",
"wayland-backend", "wayland-backend",
@ -4791,7 +4792,7 @@ dependencies = [
"cfg-if", "cfg-if",
"fastrand 2.0.1", "fastrand 2.0.1",
"redox_syscall 0.4.1", "redox_syscall 0.4.1",
"rustix 0.38.30", "rustix 0.38.32",
"windows-sys 0.52.0", "windows-sys 0.52.0",
] ]
@ -6065,7 +6066,7 @@ dependencies = [
"percent-encoding", "percent-encoding",
"raw-window-handle", "raw-window-handle",
"redox_syscall 0.3.5", "redox_syscall 0.3.5",
"rustix 0.38.30", "rustix 0.38.32",
"smithay-client-toolkit", "smithay-client-toolkit",
"smol_str", "smol_str",
"unicode-segmentation", "unicode-segmentation",
@ -6123,7 +6124,7 @@ dependencies = [
"libc", "libc",
"libloading 0.8.1", "libloading 0.8.1",
"once_cell", "once_cell",
"rustix 0.38.30", "rustix 0.38.32",
"x11rb-protocol", "x11rb-protocol",
] ]

View file

@ -57,6 +57,7 @@ xdg-user = "0.2.1"
xkbcommon = "0.7" xkbcommon = "0.7"
zbus = "3.15.0" zbus = "3.15.0"
profiling = { version = "1.0" } profiling = { version = "1.0" }
rustix = { version = "0.38.32", features = ["process"] }
[dependencies.id_tree] [dependencies.id_tree]
branch = "feature/copy_clone" branch = "feature/copy_clone"

View file

@ -74,6 +74,7 @@ use std::{
any::Any, any::Any,
cell::RefCell, cell::RefCell,
collections::HashMap, collections::HashMap,
os::unix::process::CommandExt,
thread, thread,
time::{Duration, Instant}, time::{Duration, Instant},
}; };
@ -2303,6 +2304,7 @@ impl State {
.env("XDG_ACTIVATION_TOKEN", &*token) .env("XDG_ACTIVATION_TOKEN", &*token)
.env("DESKTOP_STARTUP_ID", &*token) .env("DESKTOP_STARTUP_ID", &*token)
.env_remove("COSMIC_SESSION_SOCK"); .env_remove("COSMIC_SESSION_SOCK");
unsafe { cmd.pre_exec(|| Ok(crate::utils::rlimit::restore_nofile_limit())) };
std::thread::spawn(move || match cmd.spawn() { std::thread::spawn(move || match cmd.spawn() {
Ok(mut child) => { Ok(mut child) => {

View file

@ -9,7 +9,7 @@ use smithay::{
}; };
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use std::{env, ffi::OsString, process, sync::Arc}; use std::{env, ffi::OsString, os::unix::process::CommandExt, process, sync::Arc};
use tracing::{error, info, warn}; use tracing::{error, info, warn};
use crate::{ use crate::{
@ -44,6 +44,8 @@ fn main() -> Result<()> {
logger::init_logger()?; logger::init_logger()?;
info!("Cosmic starting up!"); info!("Cosmic starting up!");
utils::rlimit::increase_nofile_limit();
// init event loop // init event loop
let mut event_loop = EventLoop::try_new().with_context(|| "Failed to initialize event loop")?; let mut event_loop = EventLoop::try_new().with_context(|| "Failed to initialize event loop")?;
// init wayland // init wayland
@ -71,6 +73,8 @@ fn main() -> Result<()> {
let mut command = process::Command::new(&exec); let mut command = process::Command::new(&exec);
command.args(args); command.args(args);
command.envs(session::get_env(&state)?); command.envs(session::get_env(&state)?);
unsafe { command.pre_exec(|| Ok(utils::rlimit::restore_nofile_limit())) };
info!("Running {:?}", exec); info!("Running {:?}", exec);
Some(command.spawn()?) Some(command.spawn()?)
} else { } else {

View file

@ -5,5 +5,6 @@ pub(crate) use self::ids::id_gen;
pub mod geometry; pub mod geometry;
pub mod iced; pub mod iced;
pub mod prelude; pub mod prelude;
pub mod rlimit;
pub mod screenshot; pub mod screenshot;
pub mod tween; pub mod tween;

30
src/utils/rlimit.rs Normal file
View file

@ -0,0 +1,30 @@
use rustix::process::{getrlimit, setrlimit, Resource, Rlimit};
use std::sync::atomic::{AtomicU64, Ordering};
static OLD_LIMIT: AtomicU64 = AtomicU64::new(0);
static MAX_LIMIT: AtomicU64 = AtomicU64::new(0);
pub fn increase_nofile_limit() {
let mut limits = getrlimit(Resource::Nofile);
OLD_LIMIT.store(limits.current.unwrap_or(0), Ordering::SeqCst);
MAX_LIMIT.store(limits.maximum.unwrap_or(0), Ordering::SeqCst);
limits.current = limits.maximum;
if let Err(err) = setrlimit(Resource::Nofile, limits) {
tracing::warn!("Failed to raise nofile soft limit: {:?}", err);
}
}
pub fn restore_nofile_limit() {
let current = OLD_LIMIT.load(Ordering::SeqCst);
let maximum = MAX_LIMIT.load(Ordering::SeqCst);
let limits = Rlimit {
current: (current > 0).then_some(current),
maximum: (maximum > 0).then_some(maximum),
};
if let Err(err) = setrlimit(Resource::Nofile, limits) {
eprintln!("Failed to restore nofile soft limit: {:?}", err);
}
}