nix-rust / nix

Rust friendly bindings to *nix APIs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`kernel_version` can cause a data race when called from multiple threads

LegionMammal978 opened this issue · comments

In src/features.rs, kernel_version() is defined as:

    fn kernel_version() -> Result<usize> {
        static mut KERNEL_VERS: usize = 0;

        unsafe {
            if KERNEL_VERS == 0 {
                KERNEL_VERS = parse_kernel_version()?;
            }

            Ok(KERNEL_VERS)
        }
    }

If two threads call this function simultaneously when KERNEL_VERS is still 0, then it will result in a data race. A trivial fix is to replace it with an AtomicUsize accessed with Relaxed, since the worst case is that a thread performs a redundant uname() call.