Repository navigation
Make the name resolution interruptible - #8695
Conversation
This is a preparation for introducing cancellable getaddrinfo/getnameinfo.
6a123e7 to
6736cfe
Compare
|
Is it guaranteed to be safe to interrupt a thread which is stuck in |
My patch does not interrupt a thread which is stuck in |
| #ifndef GETADDRINFO_IMPL | ||
| # ifdef GETADDRINFO_EMU | ||
| # define GETADDRINFO_IMPL 0 | ||
| # elif !defined(HAVE_PTHREAD_CREATE) || !defined(HAVE_PTHREAD_DETACH) || defined(__MINGW32__) || defined(__MINGW64__) |
There was a problem hiding this comment.
if !defined(HAVE_PTHREAD_CREATE) || !defined(HAVE_PTHREAD_DETACH) is given, both functions are available.
You can also use #define HAVE_PTHREAD_H 1 in .ext/include/x86_64-linux/ruby/config.h (arch).
There was a problem hiding this comment.
I was worried about environments that only provide one of the two, or environments where pthread.h are available but not actually usable. So I thought this would be a safe bet. I may try it after Ruby CI passes.
|
I talked to @ko1 and he said it looked good. |
There was a problem hiding this comment.
Because glibc getaddrinfo() calls getenv() without any synchronization while running without GVL, it could race with Ruby ENV writers that setenv() and crash. ENV write methods use a lock, but the reads inside getaddrinfo() do not.
There is a recent issue stemming from the same underlying issue from Go:
- https://rachelbythebay.com/w/2023/10/16/env/
- golang/go#63567
- https://rachelbythebay.com/w/2017/01/30/env/
The race is also apparent if you combine man 7 attributes to interpret what MT-Safe env locale means in man 3 getaddrinfo, but maybe only with the benefit of hindsight.
Sorry for pointing out an issue without offer a potential solution :/
Even without this new feature the implementation already releases the GVL, so maybe it won't be more problematic than it already is currently.
|
On the blog post I read:
I'm not sure how official this is, but maybe we can prevent Since threads are not cancelled, is it possible that we end up creating an unbounded number of threads? |
When pthread_create is available, rb_getaddrinfo creates a pthread and executes getaddrinfo(3) in it. The caller thread waits for the pthread to complete, but detaches it if interrupted. This allows name resolution to be interuppted by Timeout.timeout, etc. even if it takes a long time (for example, when the DNS server does not respond). [Feature ruby#19965]
Same as previous commit for rb_getnameinfo.
Cosmetic change per ko1's preference
8225226 to
fe74498
Compare
|
@XrXr Thanks. @shyouhei also shared the news with me.
I agree with this. Currently, users must not change
It might be theoretically correct, but practically, incompatibility could be a problem. I think we should carefully consider it separately. |
|
I'd like to merge it and see what happens with CI. |
|
@mame in some "worst case" can this in theory create a large (potentially unbounded, or bounded to the number of requests) number of threads? |
|
Yes. A typical case where a large number of pthreads will be created is to repeatedly interrupt name resolution. The timeout of |
|
That seems reasonable to me. |
|
👋 Possible to backport this into earlier ruby versions? We're running 3.0.x in prod and this looks like it would solve a nasty issue for us with forking and tracing. |
|
@flux-benj There is no chance to backport into Ruby 3.0. Because Ruby 3.0 is only security maintenance status now. |
|
Ah right, we're looking at upgrading soon. Is this intended only for 3.3.x? If yes we will try and jump straight to that version |
|
i tried ruby -rsocket -e 'Addrinfo.getaddrinfo("www.ruby-lang.org", 80)'with ruby 3.3 and a bad dns server, and i still could not interrupt it- is that expected? |
When pthread_create is available, rb_getaddrinfo creates a pthread and
executes getaddrinfo(3) in it. The caller thread waits for the pthread
to complete, but detaches it if interrupted. This allows name resolution
to be interuppted by Timeout.timeout, etc. even if it takes a long time
(for example, when the DNS server does not respond).