Change DnsTlsTransport to store a copy of a query
This is preparation for configurable per-query timeout in order to
allow a lookup thread to exit while a DnsTlsSocket loop thread is
still waiting the response. Since a DnsTlsTransport instance is
accessible to both lookup threads and a DnsTlsSocket loop thread, change
it to store a copy of a query in case of any operation needed for
the query in the loop thread; for example, the response arrives after
timeout.
Bug: 120182528
Bug: 141218721
Test: cd packages/modules/DnsResolver && atest
Change-Id: I57bf40cb1eb0d688d5328242649c841f20d81430
diff --git a/DnsTlsQueryMap.cpp b/DnsTlsQueryMap.cpp
index 79060a4..c4ae450 100644
--- a/DnsTlsQueryMap.cpp
+++ b/DnsTlsQueryMap.cpp
@@ -37,10 +37,12 @@
LOG(WARNING) << "All query IDs are in use";
return nullptr;
}
- Query q = { .newId = static_cast<uint16_t>(newId), .query = query };
- std::map<uint16_t, QueryPromise>::iterator it;
- bool inserted;
- std::tie(it, inserted) = mQueries.emplace(newId, q);
+
+ // Make a copy of the query.
+ std::vector<uint8_t> tmp(query.base(), query.base() + query.size());
+ Query q = {.newId = static_cast<uint16_t>(newId), .query = std::move(tmp)};
+
+ const auto [it, inserted] = mQueries.try_emplace(newId, q);
if (!inserted) {
LOG(ERROR) << "Failed to store pending query";
return nullptr;
@@ -137,7 +139,7 @@
}
Result r = { .code = Response::success, .response = std::move(response) };
// Rewrite ID to match the query
- const uint8_t* data = it->second.query.query.base();
+ const uint8_t* data = it->second.query.query.data();
r.response[0] = data[0];
r.response[1] = data[1];
LOG(DEBUG) << "Sending result to dispatcher";