Thread Safety Nightmare: A Mysterious Crash on ARM64, Even with Safe Rust
2025-01-22

While porting network I/O code from Python to Rust in EdgeDB, a mysterious crash on ARM64 platforms emerged. Initially suspected to be a deadlock, the root cause turned out to be thread-unsafe behavior in the `setenv` and `getenv` functions. On ARM64, the `openssl-probe` library uses `setenv` to set environment variables, while another thread concurrently calls `getenv`. This reallocates the `environ` array, leading to a crash. The solution involved switching reqwest's TLS backend from rust-native-tls to rustls. This highlights how even in memory-safe Rust, interactions with the C standard library can still introduce thread safety issues.