From f74792b77881318087312afb27a12dd26d2edb6e Mon Sep 17 00:00:00 2001 From: warptangent Date: Wed, 14 Jan 2015 13:14:01 -0800 Subject: [PATCH] Fix seed node threaded DNS lookup Use copied value of seed node index during thread creation, not reference. - fixes segfault Use boost::thread::try_join_until() instead of an atomic flag result variable for each thread. Add and handle interrupt for thread timeout. - fixes segfault where a thread exceeds requested timeout and tries to assign results to a referenced, but now out-of-scope, variable in the main thread. --- src/p2p/net_node.inl | 69 ++++++++++++++++++++++++++------------------ 1 file changed, 41 insertions(+), 28 deletions(-) diff --git a/src/p2p/net_node.inl b/src/p2p/net_node.inl index 3454e112c..ee4a10789 100644 --- a/src/p2p/net_node.inl +++ b/src/p2p/net_node.inl @@ -259,53 +259,66 @@ namespace nodetool std::vector> dns_results; dns_results.resize(m_seed_nodes_list.size()); - std::unique_ptr dns_finished(new std::atomic_flag[m_seed_nodes_list.size()]); - - // set each flag, thread will release when finished - for (uint64_t i = 0; i < m_seed_nodes_list.size(); ++i) - dns_finished[i].test_and_set(); - + std::list dns_threads; uint64_t result_index = 0; for (const std::string& addr_str : m_seed_nodes_list) { - - uint64_t result_index_capture = result_index++; - boost::thread t([&] + boost::thread* th = new boost::thread([=, &dns_results, &addr_str] { + LOG_PRINT_L4("dns_threads[" << result_index << "] created for: " << addr_str) // TODO: care about dnssec avail/valid bool avail, valid; - std::vector addr_list = tools::DNSResolver().get_ipv4(addr_str, avail, valid); - - dns_results[result_index_capture] = addr_list; - dns_finished[result_index_capture].clear(); + std::vector addr_list; + + try + { + addr_list = tools::DNSResolver().get_ipv4(addr_str, avail, valid); + LOG_PRINT_L4("dns_threads[" << result_index << "] DNS resolve done"); + boost::this_thread::interruption_point(); + } + catch(const boost::thread_interrupted&) + { + // thread interruption request + // even if we now have results, finish thread without setting + // result variables, which are now out of scope in main thread + LOG_PRINT_L4("dns_threads[" << result_index << "] interrupted"); + return; + } + + LOG_PRINT_L4("dns_threads[" << result_index << "] addr_str: " << addr_str << " number of results: " << addr_list.size()); + dns_results[result_index] = addr_list; }); + dns_threads.push_back(th); + ++result_index; } - uint64_t sleep_count = 0; - uint64_t sleep_interval_ms = 100; - while (sleep_count++ * sleep_interval_ms < CRYPTONOTE_DNS_TIMEOUT_MS) + LOG_PRINT_L4("dns_threads created, now waiting for completion or timeout of " << CRYPTONOTE_DNS_TIMEOUT_MS << "ms"); + boost::chrono::system_clock::time_point deadline = boost::chrono::system_clock::now() + boost::chrono::milliseconds(CRYPTONOTE_DNS_TIMEOUT_MS); + uint64_t i = 0; + for (boost::thread* th : dns_threads) { - boost::this_thread::sleep(boost::posix_time::milliseconds(sleep_interval_ms)); - bool all_done = false; - for (uint64_t i = 0; i < m_seed_nodes_list.size(); ++i) + if (! th->try_join_until(deadline)) { - if (dns_finished[i].test_and_set()) - break; - else - dns_finished[i].clear(); - all_done = true; + LOG_PRINT_L4("dns_threads[" << i << "] timed out, sending interrupt"); + th->interrupt(); } - if (all_done) - break; + ++i; } + i = 0; for (const auto& result : dns_results) { - for (const auto& addr_string : result) + LOG_PRINT_L4("DNS lookup for " << m_seed_nodes_list[i] << ": " << result.size() << " results"); + // if no results for node, thread's lookup likely timed out + if (result.size()) { - append_net_address(m_seed_nodes, addr_string + ":18080"); + for (const auto& addr_string : result) + { + append_net_address(m_seed_nodes, addr_string + ":18080"); + } } + ++i; } if (!m_seed_nodes.size())