Skip to content

uucore: centralize unsafe env::set_var/remove_var in a wrapper module - #12068

Closed
sylvestre wants to merge 1 commit into
uutils:mainfrom
sylvestre:set_var
Closed

sylvestre wants to merge 1 commit into
uutils:mainfrom
sylvestre:set_var

Conversation

@sylvestre

Copy link
Copy Markdown
Contributor

No description provided.

@github-actions

github-actions Bot commented Apr 28, 2026 •

Copy link
Copy Markdown

GNU testsuite comparison:

Skip an intermittent issue tests/cut/bounded-memory (fails in this run but passes in the 'main' branch)
Skip an intermittent issue tests/tail/retry (fails in this run but passes in the 'main' branch)
Skipping an intermittent issue tests/date/resolution (passes in this run but fails in the 'main' branch)
Skipping an intermittent issue tests/tail/follow-name (passes in this run but fails in the 'main' branch)
Skipping an intermittent issue tests/tail/tail-n0f (passes in this run but fails in the 'main' branch)
Skip an intermittent issue tests/pr/bounded-memory (was skipped on 'main', now failing)

@sylvestre
sylvestre force-pushed the set_var branch 4 times, most recently from 14394df to 21c5c76 Compare April 28, 2026 21:07
@sylvestre
sylvestre marked this pull request as ready for review April 28, 2026 21:20
@sylvestre

Copy link
Copy Markdown
Contributor Author

@oech3 wdyt ? :)

@oech3

oech3 commented Apr 28, 2026

Copy link
Copy Markdown
Contributor

This does not make set_var an actual thread safe function...

///
/// Wrapper around [`std::env::set_var`]. See the module documentation for
/// the safety considerations callers must uphold.
pub fn set_var<K: AsRef<OsStr>, V: AsRef<OsStr>>(key: K, value: V) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This violates Rust's safety model. Safe Rust code must never be able to trigger UB by calling a safe function according to its documented signature. This wrapper makes the function safe to call from anywhere, but does not enforce the required invariants.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure but i don't think it is a big deal. i don't think we have a program dealing with env variable in a parallel context

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dd, sort

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and external crates

This comment was marked as outdated.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think you are overreacting. afaik, it doesn't introduce safety issues

@sylvestre

Copy link
Copy Markdown
Contributor Author

This does not make set_var an actual thread safe function...

of course but it centralized its management

@sylvestre
sylvestre requested a review from cakebaker April 30, 2026 09:29
@oech3

oech3 commented May 11, 2026

Copy link
Copy Markdown
Contributor

How about adding unsafe { uucore::env::declare_single_thread() }?

@xtqqczze

Copy link
Copy Markdown
Collaborator

How about adding unsafe { uucore::env::declare_single_thread() }?

How would that help? std::env::set_var was made unsafe precisely because there’s no way to guarantee that external C libraries access the environment in a thread-safe manner.

@xtqqczze

Copy link
Copy Markdown
Collaborator

I think we should remove use of these functions instead: #12238

@oech3

oech3 commented May 14, 2026

Copy link
Copy Markdown
Contributor

Can we wrap Mutex here?

@xtqqczze

xtqqczze commented May 14, 2026 •

Copy link
Copy Markdown
Collaborator

A mutex doesn't address the soundness issue, see rust-lang/rust#124636 (comment).

cc: @ChrisDenton

@ChrisDenton

Copy link
Copy Markdown
Contributor

In an application if you can be 100% certain there is only one thread then it is safe to mutate the environment because another function literally can't be called while you're using set_var or remove_var.

If there are, or could be, multiple threads then there would need to be some way to guarantee that no thread (directly or indirectly) accesses the environment. This is practically impossible to uphold since many things can read the environment (e.g. almost any libc function reserves the right to do so). A mutex doesn't really help.

@xtqqczze

Copy link
Copy Markdown
Collaborator

@sylvestre The proposed uucore::env::set_var and uucore::env::remove_var functions are public APIs, so we can’t make guarantees about threading. Once exposed, they need to remain sound under arbitrary usage, including multi-threaded contexts.

@tbu-

tbu- commented May 15, 2026

Copy link
Copy Markdown

It's a bit weird to "centralize" unsafe functions into a single function not marked as unsafe. It moves the unsafe block away from the place where you need to argue that the usage is actually safe.

@sylvestre

Copy link
Copy Markdown
Contributor Author

@samueltardieu wdyt ? :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants