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

Cleanup ares_library_init from Envoy ProcessWide::ProcessWide() #18713

Closed
yanjunxiang-google opened this issue Oct 21, 2021 · 10 comments
Closed
Assignees
Labels
area/dns enhancement Feature requests. Not bugs or questions. no stalebot Disables stalebot from closing an issue

Comments

@yanjunxiang-google
Copy link
Contributor

yanjunxiang-google commented Oct 21, 2021

Title: Cleanup ares_library_init from Envoy ProcessWide::ProcessWide()

Description: With Envoy DNS as an extension now(#17479), c-ares DNS library may not be needed in some cases. Thus, we should not call ares_library_init(ARES_LIB_INIT_ALL) in ProcessWide::ProcessWide().

Same for call ares_library_cleanup() in ProcessWide::~ProcessWide()

https://source.corp.google.com/piper///depot/google3/third_party/envoy/src/source/exe/process_wide.cc;rcl=404246718;l=20
https://source.corp.google.com/piper///depot/google3/third_party/envoy/src/source/exe/process_wide.cc;rcl=404246718;l=46

@yanjunxiang-google yanjunxiang-google added the triage Issue requires triage label Oct 21, 2021
@yanjunxiang-google
Copy link
Contributor Author

Just an initial thought. This ares_library_init() can be moved to the c-ares DNS resolver object creation time, like here:

DnsResolverImpl::DnsResolverImpl(

And ares_library_cleanup() can be called in the e c-ares DNS resolver object destruction time, like here:

DnsResolverImpl::~DnsResolverImpl() {

@yanjunxiang-google
Copy link
Contributor Author

/assign @jmarantz

@yanjunxiang-google
Copy link
Contributor Author

/assign @yanavlasov

@htuch htuch added area/dns enhancement Feature requests. Not bugs or questions. and removed triage Issue requires triage labels Oct 22, 2021
@jmarantz
Copy link
Contributor

RE putting it in the DnsResolverImpl's ctor/dtor makes sense to me. I think if you dive into the impl inside c_ares you will see that it tracks nested instantiations so if you init twice, it won't really cleanup until you cleanup twice. However, it does not use any kind of thread-safe structure to do that (atomic or mutex) so this will race if the DnsResolverImpl objects are created/destroyed across threads.

So we should either do ASSERT(Thread::isMainOrTestThread()) in the ctor/dtor, or we should add a wrapper object that makes a lazy-init mutable singleton with a mutex to de-race the init/cleanup of the c_area code.

Alternatively we could try an upstream change to c_ares to make its init/destruct thread-safe.

@yanjunxiang-google
Copy link
Contributor Author

HI, Josh,

That's a good point. Yeah, simply adding it in DnsResolverImpl's ctor/dtor is not thread safe since we do support DnsResolver creation/destruction in both main threads (e.g. dns_cache_impl, cluster, ...) and worker threads(e.g. dns_filter). Thus if we want to go this approach, as you mentioned, we need either using mutex to de-race, or refactor code to make it thread safe. The other option I am thinking is to add it the CaresDnsResolverFactory ctor/dtor, which is done during the bootstrapping, and main thread only:

class CaresDnsResolverFactory : public DnsResolverFactory {

Does this make sense to you?
Thanks,
Yanjun

@yanjunxiang-google
Copy link
Contributor Author

yanjunxiang-google commented Oct 25, 2021

/assign @htuch

@jmarantz
Copy link
Contributor

CaresDnsResolverFactory seems like a great place to do it, if it's only created when someone configures c_ares. We'd be back in the same boat if that gets created eagerly.

alternatively, you could init c_ares from CaresDnsResolverFactory::createDnsResolver(), but only once per factory, and guarded by a mutex or atomic member var in CaresDnsResolverFactory to avoid races.

@htuch
Copy link
Member

htuch commented Oct 26, 2021

+1 factory makes sense here.

@github-actions
Copy link

This issue has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed in the next 7 days unless it is tagged "help wanted" or "no stalebot" or other activity occurs. Thank you for your contributions.

@github-actions github-actions bot added the stale stalebot believes this issue/PR has not been touched recently label Nov 25, 2021
@yanavlasov yanavlasov added no stalebot Disables stalebot from closing an issue and removed stale stalebot believes this issue/PR has not been touched recently labels Nov 30, 2021
@yanjunxiang-google
Copy link
Contributor Author

Closed by: #21884

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/dns enhancement Feature requests. Not bugs or questions. no stalebot Disables stalebot from closing an issue
Projects
None yet
Development

No branches or pull requests

4 participants