In the Rust std, `set_var` and `remove_var` will correctly require using an `unsafe {}` block in the next edition (2024). The documentation does now mention the safety issue but obviously it was a mistake to make these functions safe originally (albeit a mistake even higher level languages have made). https://doc.rust-lang.org/stable/std/env/fn.set_var.html There is a patch for glibc which makes `getenv` safe in more…
Why requiring unsafe when the std implementation could take care of the synchronisation?
C stdlib isn't threadsafe and even safe Rust didn't save us
21–30 of 370 posts
Re: C stdlib isn't threadsafe and even safe Rust didn't save us
#22Yet another person is burned by calling setenv() in a multi-threaded context. There really needs to be a big warning banner on the manpage for setenv() that warns about this because it seems like a far more common problem than you would expect.
The man page says: > POSIX.1 does not require setenv() or unsetenv() to be reentrant. A non-reentrant function cannot be thread safe. In general (for POSIX, libc and many other libraries: if the docs do not explicitly say "this function is thread safe" they are not).
Re: C stdlib isn't threadsafe and even safe Rust didn't save us
#23Mutable global state is evil. Friends don’t let friends use mutable global state. I hate envvars. It’s “the Linux way”. I avoid them like the plague. A++ strong recommend. libc is terrible. The world needs to move on.
the problem is not linux, not mutable global state or resources and not libc.
the problem is not getting time at work to do things properly. like spotting this in GDB before the issue hit, because your boss gave you time to tirelessly debug and reverse your code and anything it touches....
there is too much money in halfbaked code. sad but true.
Re: C stdlib isn't threadsafe and even safe Rust didn't save us
#24Re: C stdlib isn't threadsafe and even safe Rust didn't save us
#25Earlier quoted context omitted.
The man page says: > POSIX.1 does not require setenv() or unsetenv() to be reentrant. A non-reentrant function cannot be thread safe. In general (for POSIX, libc and many other libraries: if the docs do not explicitly say "this function is thread safe" they are not).
It's time to move beyond this attitude and make things safe by default. For example, Solaris has a safer version of setenv(). "It is ridiculous that this has been a known problem for so long. It has wasted thousands of hours of people's time, either debugging the problems, or debating what to do about it. We know how to fix the problem." https://www.evanjones.ca/setenv-is-not-thread-safe.html
Its a different story for languages/environments that are supposed to be safe by default and where you have language features that ensure safety (actors, optionals etc) but not for something like libc which has a standard it has to conform to and like 100 years of history.
Re: C stdlib isn't threadsafe and even safe Rust didn't save us
#26I wonder why it is so hard for Rust to implement its own safe stdlib independent of C.
Re: C stdlib isn't threadsafe and even safe Rust didn't save us
#27Re: C stdlib isn't threadsafe and even safe Rust didn't save us
#28I wonder why it is so hard for Rust to implement its own safe stdlib independent of C.
Re: C stdlib isn't threadsafe and even safe Rust didn't save us
#29I'm also not convinced by Musl's maintainer that it can't be fixed within Musl considering glibc is making changes to make this a non-issue.
Re: C stdlib isn't threadsafe and even safe Rust didn't save us
#30 if (__environ == NULL || name[0] == '\0')
return NULL;