Skip to content

Commit

Permalink
Set a reasonable max count on open files and don't panic if set fails
Browse files Browse the repository at this point in the history
  • Loading branch information
connorimes committed Oct 13, 2015
1 parent efdaf5e commit fd710ee
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions components/script/lib.rs
Expand Up @@ -102,15 +102,37 @@ use dom::bindings::codegen::RegisterBindings;
#[allow(unsafe_code)]
fn perform_platform_specific_initialization() {
use std::mem;
const RLIMIT_NOFILE: libc::c_int = 7;
// 4096 is default max on many linux systems
const MAX_FILE_LIMIT: libc::rlim_t = 4096;

// Bump up our number of file descriptors to save us from impending doom caused by an onslaught
// of iframes.
unsafe {
let mut rlim = mem::uninitialized();
assert!(libc::getrlimit(RLIMIT_NOFILE, &mut rlim) == 0);
rlim.rlim_cur = rlim.rlim_max;
assert!(libc::setrlimit(RLIMIT_NOFILE, &mut rlim) == 0);
let mut rlim: libc::rlimit = mem::uninitialized();
match libc::getrlimit(libc::RLIMIT_NOFILE, &mut rlim) {
0 => {
if rlim.rlim_cur >= MAX_FILE_LIMIT {
// we have more than enough
return;
}

rlim.rlim_cur = match rlim.rlim_max {
libc::RLIM_INFINITY => MAX_FILE_LIMIT,
_ => {
if rlim.rlim_max < MAX_FILE_LIMIT {
rlim.rlim_max
} else {
MAX_FILE_LIMIT
}
}
};
match libc::setrlimit(libc::RLIMIT_NOFILE, &mut rlim) {
0 => (),
_ => warn!("Failed to set file count limit"),
};
},
_ => warn!("Failed to get file count limit"),
};
}
}

Expand Down

0 comments on commit fd710ee

Please sign in to comment.