`cargo test` produces `Too many open files` on NixOS when using multiple integration tests
Problem
Hi!
Recently I switched to a new machine running NixOS. I've used the same configuration as on the previous machine but with slight modifications. However. When I started working on a project that I've been working on on a previous machine, it turned out, that I cannot run cargo test successfully.
The compilation of integration tests (multiple files) always fails with a Too many open files error on random .rlib or .rmeta file either during compilation or during linking. On my previous machine it compiled just fine.
cargo test: cargo-test.logstrace cargo test: strace-full.logstrace -e open,close,openat cargo test: strace.log
What did help was to change the extension of all integration test files but one, compile, change the extension back to .rs for another file and repeat until all tests are renamed and compiled. But making bigger changes to the codebase means that I have to recompile all of the integration tests again.
I also tried to compile only one crate at the time, but it was taking too much time with all of the dependencies of the project.
Currently the only way to mitigate it for me is to put all the tests into unit tests, as they seem to work fine.
Steps
- Clone https://github.com/wvffle/nixos-rust-too-many-open-files/tree/master
- enter dev shell with
nix develop - run
cargo test
Note that the repro, has unused dependencies imported in the tests/common/mod.rs. When I remove them, I can compile just fine. But if they are imported (even if unused) the compilation fails.
Possible Solution(s)
No response
Notes
I've tried rust 1.75, 1.80 and 1.82.0-nightly but all versions fail the same way.
The ulimit -n was initially set to 1024. I've tried to set it higher but that didn't work either. I've even raised the hard and soft limits in PAM using this configuration, but that didn't help also:
systemd.extraConfig = "DefaultLimitNOFILE=1048576";
security.pam.loginLimits = [{
domain = "*";
item = "nofile";
type = "-";
value = "1048576";
}];
Tried linking using mold, no change.
Version
cargo 1.82.0-nightly (0d8d22f83 2024-08-08)
release: 1.82.0-nightly
commit-hash: 0d8d22f83b066503f6b2b755925197e959e58b4f
commit-date: 2024-08-08
host: x86_64-unknown-linux-gnu
libgit2: 1.8.1 (sys:0.19.0 vendored)
libcurl: 8.9.0-DEV (sys:0.4.74+curl-8.9.0 vendored ssl:OpenSSL/3.3.1)
ssl: OpenSSL 3.3.1 4 Jun 2024
os: NixOS 24.11.0 [64-bit]