Skip to content

Commit 455dcc6

Browse files
committed
Add CommandExt::before_exec
1 parent 8c27bfb commit 455dcc6

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

src/rust-2024/newly-unsafe-functions.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,43 @@ More information may be found in the tracking issue at <https://github.com/rust-
88
- The following functions are now marked [`unsafe`]:
99
- [`std::env::set_var`]
1010
- [`std::env::remove_var`]
11+
- [`std::os::unix::process::CommandExt::before_exec`]
1112

1213
[`unsafe`]: ../../reference/unsafe-keyword.html#unsafe-functions-unsafe-fn
1314
[`std::env::set_var`]: ../../std/env/fn.set_var.html
1415
[`std::env::remove_var`]: ../../std/env/fn.remove_var.html
16+
[`std::os::unix::process::CommandExt::before_exec`]: ../../std/os/unix/process/trait.CommandExt.html#method.before_exec
1517

1618
## Details
1719

20+
Over time it has become evident that certain functions in the standard library should have been marked as `unsafe`. However, adding `unsafe` to a function can be a breaking change since it requires existing code to be placed in an `unsafe` block. To avoid the breaking change, these functions are marked as `unsafe` starting in the 2024 Edition, while not requiring `unsafe` in previous editions.
21+
22+
### `std::env::{set_var, remove_var}`
23+
1824
It can be unsound to call [`std::env::set_var`] or [`std::env::remove_var`] in a multi-threaded program due to safety limitations of the way the process environment is handled on some platforms. The standard library originally defined these as safe functions, but it was later determined that was not correct.
1925

2026
It is important to ensure that these functions are not called when any other thread might be running. See the [Safety] section of the function documentation for more details.
2127

22-
Ordinarily it would be a backwards-incompatible change to add `unsafe` to these functions. To address that problem, they are marked as `unsafe` only in the 2024 Edition.
23-
2428
[Safety]: ../../std/env/fn.set_var.html#safety
2529

30+
### `std::os::unix::process::CommandExt::before_exec`
31+
32+
The [`std::os::unix::process::CommandExt::before_exec`] function is a unix-specific function which provides a way to run a closure before calling `exec`. This function was deprecated in the 1.37 release, and replaced with [`pre_exec`] which does the same thing, but is marked as `unsafe`.
33+
34+
Even though `before_exec` is deprecated, it is now correctly marked as `unsafe` starting in the 2024 Edition. This should help ensure that any legacy code which has not already migrated to `pre_exec` to require an `unsafe` block.
35+
36+
There are very strict safety requirements for the `before_exec` closure to satisfy. See the [Safety section][pre-exec-safety] for more details.
37+
38+
[`pre_exec`]: ../../std/os/unix/process/trait.CommandExt.html#tymethod.pre_exec
39+
[pre-exec-safety]: ../../std/os/unix/process/trait.CommandExt.html#notes-and-safety
40+
2641
## Migration
2742

28-
To make your code compile in both the 2021 and 2024 editions, you will need to make sure that `set_var` and `remove_var` are called only from within `unsafe` blocks.
43+
To make your code compile in both the 2021 and 2024 editions, you will need to make sure that these functions are called only from within `unsafe` blocks.
2944

30-
**⚠ Caution**: It is important that you manually inspect the calls to `set_var` and `remove_var` and possibly rewrite your code to satisfy the preconditions of those functions. In particular, they should not be called if there might be multiple threads running. You may need to elect to use a different mechanism other than environment variables to manage your use case.
45+
**⚠ Caution**: It is important that you manually inspect the calls to these functions and possibly rewrite your code to satisfy the preconditions of those functions. In particular, `set_var` and `remove_var` should not be called if there might be multiple threads running. You may need to elect to use a different mechanism other than environment variables to manage your use case.
3146

32-
The [`deprecated_safe_2024`] lint will automatically modify any use of `set_var` or `remove_var` to be wrapped in an `unsafe` block so that it can compile on both editions. This lint is part of the `rust-2024-compatibility` lint group, which will automatically be applied when running `cargo fix --edition`. To migrate your code to be Rust 2024 Edition compatible, run:
47+
The [`deprecated_safe_2024`] lint will automatically modify any use of these functions to be wrapped in an `unsafe` block so that it can compile on both editions. This lint is part of the `rust-2024-compatibility` lint group, which will automatically be applied when running `cargo fix --edition`. To migrate your code to be Rust 2024 Edition compatible, run:
3348

3449
```sh
3550
cargo fix --edition

0 commit comments

Comments
 (0)