Skip to content

Make the name resolution interruptible - #8695

Merged
mame merged 8 commits into
ruby:masterfrom
mame:wip-interruptible-getaddrinfo
Oct 24, 2023
Merged

mame merged 8 commits into
ruby:masterfrom
mame:wip-interruptible-getaddrinfo

Conversation

@mame

@mame mame commented Oct 18, 2023

Copy link
Copy Markdown
Member

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).

@mame
mame force-pushed the wip-interruptible-getaddrinfo branch 2 times, most recently from 6a123e7 to 6736cfe Compare October 18, 2023 11:41
@ioquatix

Copy link
Copy Markdown
Member

Is it guaranteed to be safe to interrupt a thread which is stuck in getaddrinfo?

@mame

mame commented Oct 19, 2023

Copy link
Copy Markdown
Member Author

@mame

mame commented Oct 19, 2023

Copy link
Copy Markdown
Member Author

Is it guaranteed to be safe to interrupt a thread which is stuck in getaddrinfo?

My patch does not interrupt a thread which is stuck in getaddrinfo(3). See my ticket.

@mame
mame marked this pull request as ready for review October 19, 2023 03:36
@mame
mame requested review from ko1 and nobu October 19, 2023 03:36
Comment thread ext/socket/raddrinfo.c
#ifndef GETADDRINFO_IMPL
# ifdef GETADDRINFO_EMU
# define GETADDRINFO_IMPL 0
# elif !defined(HAVE_PTHREAD_CREATE) || !defined(HAVE_PTHREAD_DETACH) || defined(__MINGW32__) || defined(__MINGW64__)

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.

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).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

Comment thread ext/socket/raddrinfo.c Resolved
@mame

mame commented Oct 19, 2023

Copy link
Copy Markdown
Member Author

I talked to @ko1 and he said it looked good.

@XrXr XrXr left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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:

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.

@ioquatix

ioquatix commented Oct 20, 2023 •

Copy link
Copy Markdown
Member

On the blog post I read:

you are not allowed to setenv in a multi-threaded program

I'm not sure how official this is, but maybe we can prevent ENV[...] = ... when multiple threads exist, i.e. still allow it at start-up, but prevent it later to avoid this entire class of issues.

Since threads are not cancelled, is it possible that we end up creating an unbounded number of threads?

mame added 5 commits October 24, 2023 11:07
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
@mame
mame force-pushed the wip-interruptible-getaddrinfo branch from 8225226 to fe74498 Compare October 24, 2023 02:07
@mame

mame commented Oct 24, 2023

Copy link
Copy Markdown
Member Author

@XrXr Thanks. @shyouhei also shared the news with me.

Even without this new feature the implementation already releases the GVL, so maybe it won't be more problematic than it already is currently.

I agree with this. Currently, users must not change setenv and locale during getaddrinfo. We might want to document it, but I guess we need to check functions other than getaddrinfo that are conditionally unsafe.

@ioquatix

maybe we can prevent ENV[...] = ... when multiple threads exist

It might be theoretically correct, but practically, incompatibility could be a problem. I think we should carefully consider it separately.

@mame

mame commented Oct 24, 2023

Copy link
Copy Markdown
Member Author

I'd like to merge it and see what happens with CI.

@mame
mame merged commit 25c1204 into ruby:master Oct 24, 2023
@mame
mame deleted the wip-interruptible-getaddrinfo branch October 24, 2023 03:23
@ioquatix

Copy link
Copy Markdown
Member

@mame in some "worst case" can this in theory create a large (potentially unbounded, or bounded to the number of requests) number of threads?

@mame

mame commented Oct 24, 2023 •

Copy link
Copy Markdown
Member Author

Yes. A typical case where a large number of pthreads will be created is to repeatedly interrupt name resolution.

loop do
  begin
    Timeout.timeout(2) do
      Addrinfo.getaddrinfo("unsolvabledomain")
    end
  rescue Timeout::Error
  end
end

The timeout of getaddrinfo(3) depends on the system configuration. On my Ubuntu, it seems to be 10 seconds. So, if Timeout.timeout(2) repeatedly interrupts Addrinfo.getaddrinfo, 10 / 2 = 5 pthreads will stay.

@ioquatix

Copy link
Copy Markdown
Member

That seems reasonable to me.

@flux-benj

Copy link
Copy Markdown

👋 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.

@hsbt

hsbt commented Dec 8, 2023

Copy link
Copy Markdown
Member

@flux-benj There is no chance to backport into Ruby 3.0. Because Ruby 3.0 is only security maintenance status now.

@flux-benj

Copy link
Copy Markdown

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

@jjb

jjb commented Apr 30, 2024

Copy link
Copy Markdown
Contributor

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?

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.

7 participants