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

Does getaddrinfo need to take the ENV lock? #27970

Open
bluss opened this Issue Aug 23, 2015 · 5 comments

Comments

Projects
None yet
5 participants
@bluss
Copy link
Contributor

bluss commented Aug 23, 2015

Like the documentation for our set_var (setenv) says, care must be taken with mutating environment variables in a multithreaded program. See this glibc bug #13271 that says getaddrinfo may call getenv.

It looks like we have an unsynchronized call to getaddrinfo and this may cause trouble with glibc/Linux.

Seeing glibc's attitude to setenv in multithreaded programs, set_var seems like a big hazard in general(?).

Discovered as an issue tangential to #27966

cc @alexcrichton

@bluss

This comment has been minimized.

Copy link
Contributor Author

bluss commented Aug 23, 2015

@alexcrichton

This comment has been minimized.

Copy link
Member

alexcrichton commented Aug 24, 2015

If we did take this route I'd want to convert the environment lock to a rwlock to ensure that we could at least have parallel dns queries. Also cc #27705.

@bluss

This comment has been minimized.

Copy link
Contributor Author

bluss commented Aug 24, 2015

Yeah, either way the situation is tricky. We get tangled up in glibc internals.

@alexcrichton

This comment has been minimized.

Copy link
Member

alexcrichton commented Aug 24, 2015

I found the wording on this page also particularly interesting:

       env    Functions marked with env as an MT-Safety issue access the
              environment with getenv(3) or similar, without any guards to
              ensure safety in the presence of concurrent modifications.

              We do not mark these functions as MT-Unsafe, however, because
              functions that modify the environment are all marked with
              const:env and regarded as unsafe.  Being unsafe, the latter
              are not to be called when multiple threads are running or
              asynchronous signals are enabled, and so the environment can
              be considered effectively constant in these contexts, which
              makes the former safe.

The getaddrinfo function is indeed marked with this tag

@fweimer

This comment has been minimized.

Copy link

fweimer commented Dec 3, 2016

std::env::set_var is just not safe to use in a multi-threaded program, as I commented on issue #24741:

Even without low-level races, std::env::set_var is not quite safe because you can change a variable (such as TZ), do something, and change it back to the original value, without impacting something else in the process which runs at the same time.

This problem would not go away if we provided thread-safe environment access at the libc layer. Therefore, I'm surprised the function isn't marked unsafe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.