rust/library/std/src/sys
Matthias Krüger 2dd2fb728e
Rollup merge of #104493 - adamncasey:cgroupzeroperiod, r=m-ou-se
available_parallelism: Gracefully handle zero value cfs_period_us

There seem to be some scenarios where the cgroup cpu quota field `cpu.cfs_period_us` can contain `0`. This field is used to determine the "amount" of parallelism suggested by the function `std:🧵:available_parallelism`

A zero value of this field cause a panic when `available_parallelism()` is invoked. This issue was detected by the call from binaries built by `cargo test`. I really don't feel like `0` is a good value for `cpu.cfs_period_us`, but I also don't think applications should panic if this value is seen.

This panic started happening with rust 1.64.0.

This case is gracefully handled by other projects which read this information: [num_cpus](e437b9d908/src/linux.rs (L207-L210)), [ninja](https://github.com/ninja-build/ninja/pull/2174/files), [dotnet](c4341d45ac/src/coreclr/pal/src/misc/cgroup.cpp (L481-L483))

Before this change, running `cargo test` in environments configured as described above would trigger this panic:
```
$ RUST_BACKTRACE=1 cargo test
    Finished test [unoptimized + debuginfo] target(s) in 3.55s
     Running unittests src/main.rs (target/debug/deps/x-9a42e145aca2934d)
thread 'main' panicked at 'attempt to divide by zero', library/std/src/sys/unix/thread.rs:546:70
stack backtrace:
   0: rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::panicking::panic
   3: std::sys::unix:🧵:cgroups::quota
   4: std::sys::unix:🧵:available_parallelism
   5: std:🧵:available_parallelism
   6: test::helpers::concurrency::get_concurrency
   7: test::console::run_tests_console
   8: test::test_main
   9: test::test_main_static
  10: x::main
             at ./src/main.rs:1:1
  11: core::ops::function::FnOnce::call_once
             at /tmp/rust-1.64-1.64.0-1/library/core/src/ops/function.rs:248:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
error: test failed, to rerun pass '--bin x'
```

I've tested this change in an environment which has the bad (questionable?) setup and rebuilding the test executable against a fixed std library fixes the panic.
2022-12-28 22:22:18 +01:00
..
common Remove redundant all in cfg 2022-11-26 09:31:40 +00:00
hermit hermit: Remove unused exports 2022-11-29 12:25:35 +01:00
itron kmc-solid: Don't do Box::from_raw(&*(x: Box<T>) as *const T as *mut T) 2022-12-02 16:58:41 +09:00
sgx Rollup merge of #98391 - joboet:sgx_parker, r=m-ou-se 2022-12-10 09:24:40 +01:00
solid kmc-solid: Address compiler warnings 2022-12-01 13:18:05 +09:00
unix Rollup merge of #104493 - adamncasey:cgroupzeroperiod, r=m-ou-se 2022-12-28 22:22:18 +01:00
unsupported Auto merge of #105698 - joboet:unsupported_threads_once, r=thomcc 2022-12-19 16:46:57 +00:00
wasi std: use a more efficient Once on platforms without threads 2022-12-14 13:55:30 +01:00
wasm std: use a more efficient Once on platforms without threads 2022-12-14 13:55:30 +01:00
windows Implement blocking output 2022-12-11 10:21:40 +05:30
mod.rs Reduce CString allocations in std as much as possible 2022-10-03 11:13:17 -07:00