Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[std.process] Make environment use a ReadWriteMutex #10611

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

ntrel
Copy link
Contributor

@ntrel ntrel commented Jan 6, 2025

Fixes #10580.

Make getEnvironPtr @system.
Use a ReadWriteMutex to protect reading and writing to environment.
Add scope to getImpl callback parameter.

Warning 1: This (currently) removes nothrow @nogc from remove.
Warning 2: I am not that experienced with locks (particularly on Windows), so bear that in mind, I may have done something wrong. Or there may be a better solution, please let me know.

@dlang-bot
Copy link
Contributor

Thanks for your pull request and interest in making D better, @ntrel! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

⚠️⚠️⚠️ Warnings ⚠️⚠️⚠️

  • In preparation for migrating from Bugzilla to GitHub Issues, the issue reference syntax has changed. Please add the word "Bugzilla" to issue references. For example, Fix Bugzilla Issue 12345 or Fix Bugzilla 12345.(Reminder: the edit needs to be done in the Git commit message, not the GitHub pull request.)

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + phobos#10611"

@ntrel ntrel changed the title [std.process] Make environment use a ReadWriteMutex [std.process] Make environment use a ReadWriteMutex Jan 6, 2025
@0xEAB
Copy link
Member

0xEAB commented Jan 9, 2025

Improper rebase, my bad, will redo.

Fixes dlang#10580.

Make getEnvironPtr `@system`.
Use a ReadWriteMutex to protect reading and writing to environment.
Add `scope` to `getImpl` callback parameter.

Warning 1: This (currently) removes `nothrow @nogc` from `remove`.
Warning 2: I am not that experienced with locks, so bear that in mind,
I may have done something wrong. Or there may be a better solution,
please let me know.
@0xEAB
Copy link
Member

0xEAB commented Jan 9, 2025

Properly rebased now.
See https://github.com/0xEAB/phobos/tree/ntrel/env-mutex-backup for a backup of the original branch.

std/process.d Outdated Show resolved Hide resolved
@ntrel ntrel marked this pull request as ready for review January 10, 2025 11:20
@0xEAB
Copy link
Member

0xEAB commented Jan 17, 2025

I think it would be better if the final version had braces and indentation around the synchronized blocks.
But for now, having them shoe-horned in keeps the diff straightforward.

@0xEAB
Copy link
Member

0xEAB commented Jan 17, 2025

For changelog reasons, the commit message would ideally be prefixed with Fix #10580 – if it fixes said issue completely.

@@ -296,8 +308,9 @@ static:
multi-threaded programs. See e.g.
$(LINK2 https://www.gnu.org/software/libc/manual/html_node/Environment-Access.html#Environment-Access, glibc).
*/
void remove(scope const(char)[] name) @trusted nothrow @nogc
void remove(scope const(char)[] name) @trusted
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure we can remove those attributes, this might break code.

Why does this now require GC and throwing? Can we catch any exceptions?

Copy link
Member

Choose a reason for hiding this comment

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

Also, the comment for this function should be altered, as now we are supporting multithreaded changes via our own lock.

@schveiguy
Copy link
Member

This at least needs a test.

@schveiguy
Copy link
Member

Thinking about this, I'm not sure I like this approach. It depends on implementation details of C, and avoiding calling C functions that might alter the environment.

Consider this, if you alter the environment in another thread not through this interface, you get the same problem. Yet the interface is explicitly designed to be able to see such changes.

My approach that I recommend is to store a shadow copy of the environment, which updates the environment on set, and is properly synchronized (a rwlock is a decent choice for this). You sync the environment from C with the first call, and then on subsequent calls, you use the internal copy for everything, pushing changes back to C.

Unfortunately, I think this is the only way to keep the functions @safe, only the initial read from libc will need to be trusted.

The nogc thing is a harder problem, but still possible to deal with by using C malloc instead of an AA for storage.

What do you think?

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.

std.process.environment is NOT SAFE
5 participants