Setenv() isn't threadsafe and even safe Rust didn't save us
a year ago
- #Rust
- #Debugging
- #ARM64
- EdgeDB encountered a crash on ARM64 CI runners while porting network I/O code from Python to Rust.
- The issue appeared as a deadlock but was actually a crash in `getenv` due to a race condition with `setenv`.
- Debugging revealed the crash occurred when `getenv` tried to access an invalid memory location during environment variable scanning.
- The root cause was identified as `setenv` being called by `openssl-probe` to set SSL certificate paths, leading to a race condition.
- The solution involved switching from `reqwest`'s `rust-native-tls`/`openssl` backend to `rustls` on Linux to avoid the issue.
- The Rust project plans to mark environment-setter functions as `unsafe` in the 2024 edition to prevent similar issues.
- `glibc` has recently improved thread-safety in `getenv` by avoiding `realloc` and leaking older environments.