7 ms·
My first thought when reading this is "what about rust" ? Rust uses the system libc to support most of it's standard library, so could it have the same problem
by roblabla 10y ago
My first thought when reading this is "what about rust" ? Rust uses the system libc to support most of it's standard library, so could it have the same problem ?
A quick look at the std::env::set_env docs [0] tells us that Rust is aware the underlying implementation are inherently thread-unsafe, and looking at its implementation [1] tells us that rust uses a global lock for all access to the environment.
So at least, it will avoid segfaulting your programs.
Good job rust :D
EDIT: However, I guess if you fork() (which is not something you can do with the standard rust library AFAIK, so requires unsafe), you may get the first problem of having a deadlock.
[0]: https://doc.rust-lang.org/std/env/fn.set_var.html https://doc.rust-lang.org/std/env/fn.set_var.html
[1]: https://github.com/rust-lang/rust/blob/51d29343c04a27570a8ff8282611007d6e6408de/src/libstd/sys/unix/os.rs#L415 https://github.com/rust-lang/rust/blob/51d29343c04a27570a8ff...
- quotemstr 10y agoIf your fork in a multithreaded program, you have bigger problems. You can deadlock taking any lock in the child that could possibly have been taken by some thread at the time of fork. In practice, you can't predict all these locks, so the only sane thing you can do in a forked child of a multithreaded program is call async-signal-safe functions (as POSIX commandeth) and probably execve something.
- deleted 10y ago[deleted]
- loeg 10y agoYou could also invalidate all locks from the parent with some kind of generational scheme based on pid or whatever. I believe the Python interpreters do something crazy like this to try and avoid deadlock after forking multithreaded programs.
- quotemstr 10y agoSo what? Under that scheme, you can take the locks, but the lock-protected data structures are probably garbage. (That is, the locks become implicitly "abandoned", not "unlocked".)
- loeg 10y agoI'm not defending it as a model. I think python's approach to fork & thread interaction, which is to punt on it completely while exposing both to end users, is pretty broken.
- cesarb 10y ago> However, I guess if you fork() (which is not something you can do with the standard rust library AFAIK, so requires unsafe) https://github.com/rust-lang/rust/blob/0c85f2a1bdb01666e74cd391df630da581f12ad0/src/libstd/sys/unix/process/process_unix.rs#L39 https://github.com/rust-lang/rust/blob/0c85f2a1bdb01666e74cd... Command::spawn() calls fork(). And you can schedule things to run on the child with CommandExt::before_exec().
- roblabla 10y agobefore_exec is unstable AFAIK ? EDIT: To be stabilized in 1.15 coming in two days. Oh well, at least, it shouldn't be possible to invoke UB. Hopefully :D
- amluto 10y agoThat's an unfortunate implementation. posix_spawn() is much faster in large applications because it doesn't need to make all MAP_PRIVATE mappings read-only for CoW purposes. Rust could hand-roll it by calling clone() directly, but the code is a bit tricky.
- majewsky 10y ago> So at least, it will avoid segfaulting your programs. That's implying there are no setenv() calls in the C code that the Rust code calls.
- gens 10y agoWhy does everything related to C have to have comments on rust ?
- Sharlin 10y agoBecause the point of Rust is to be a better C.
- Keluri 10y agoSo? That doesn't mean every submitted article mentioning C or C++ has to immediately be filled with comments on how Rust is so much better, safer, etc - So is Java and C#, and you don't see those communities espousing their language the same way!
- theamk 10y agoBecause Java and C# are not system programming languages and are not intended to replace C? Would I want my init system written in Java or C#? Hell no. Would I want it written in Rust? Yes please maybe it will crash less and have fewer CVEs. I have a belief that every time a C developer switches to Rust, users benefit (unless developer goes crazy from Rust's cognitive load, but this is a separate issue :) )
- gens 10y agoIf you ask me rust is also not for programming systems. (the error handling specifically) Side note: Who even started the whole "x is a system programming language" ? Who said you can't write "system software"[0] in just about any language that can (directly or indirectly) do "lower level" things. For example the MS Singularity OS has a virtual machine and all the drivers and everything is high level code. >Would I want my init system written in Java or C#? Hell no. Would I want it written in Rust? Yes please maybe it will crash less and have fewer CVEs. Contrary to popular belief init systems are simple and can be written in any language. Not to mention that when people talk about inits they don't think about the actual initialization part (mounting, setting network, etc) but about the starting of programs part. Those two things are similar from a far but are really not so much. >I have a belief that every time a C developer switches to Rust, users benefit (unless developer goes crazy from Rust's cognitive load, but this is a separate issue :) ) I have a belief that people should make up their own mind and do whatever they want. This topic is about C and only about C. All other talk is "noise". Note that i actually like programming in C as i like optimized programs, so i may be biased. (although not nearly as biased as the rust crowd) [0] https://en.wikipedia.org/wiki/System_programming_language https://en.wikipedia.org/wiki/System_programming_language
- wyldfire 10y agoBTW Jorge Aparicio is working on a std that doesn't need a libc [1], at least for those supported platforms. [1] https://github.com/japaric/steed https://github.com/japaric/steed