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

fix: Fix sigsegv in cranectld #368

Merged
merged 3 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 26 additions & 21 deletions src/CraneCtld/CranedKeeper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -663,33 +663,38 @@ void CranedKeeper::PutNodeIntoUnavailList(const std::string &crane_id) {
}

void CranedKeeper::ConnectCranedNode_(CranedId const &craned_id) {
static Mutex s_craned_id_to_ip_cache_map_mtx;
static std::unordered_map<CranedId, std::variant<ipv4_t, ipv6_t>>
s_craned_id_to_ip_cache_map;

std::string ip_addr;

auto it = s_craned_id_to_ip_cache_map.find(craned_id);
if (it != s_craned_id_to_ip_cache_map.end()) {
if (std::holds_alternative<ipv4_t>(it->second)) { // Ipv4
ip_addr = crane::Ipv4ToStr(std::get<ipv4_t>(it->second));
} else {
CRANE_ASSERT(std::holds_alternative<ipv6_t>(it->second));
ip_addr = crane::Ipv6ToStr(std::get<ipv6_t>(it->second));
}
} else {
ipv4_t ipv4_addr;
ipv6_t ipv6_addr;
if (crane::ResolveIpv4FromHostname(craned_id, &ipv4_addr)) {
ip_addr = crane::Ipv4ToStr(ipv4_addr);
s_craned_id_to_ip_cache_map.emplace(craned_id, ipv4_addr);
} else if (crane::ResolveIpv6FromHostname(craned_id, &ipv6_addr)) {
ip_addr = crane::Ipv6ToStr(ipv6_addr);
s_craned_id_to_ip_cache_map.emplace(craned_id, ipv6_addr);
{
util::lock_guard guard(s_craned_id_to_ip_cache_map_mtx);

auto it = s_craned_id_to_ip_cache_map.find(craned_id);
if (it != s_craned_id_to_ip_cache_map.end()) {
if (std::holds_alternative<ipv4_t>(it->second)) { // Ipv4
ip_addr = crane::Ipv4ToStr(std::get<ipv4_t>(it->second));
} else {
CRANE_ASSERT(std::holds_alternative<ipv6_t>(it->second));
ip_addr = crane::Ipv6ToStr(std::get<ipv6_t>(it->second));
}
} else {
// Just hostname. It should never happen,
// but we add error handling here for robustness.
CRANE_ERROR("Unresolved hostname: {}", craned_id);
ip_addr = craned_id;
ipv4_t ipv4_addr;
ipv6_t ipv6_addr;
if (crane::ResolveIpv4FromHostname(craned_id, &ipv4_addr)) {
ip_addr = crane::Ipv4ToStr(ipv4_addr);
s_craned_id_to_ip_cache_map.emplace(craned_id, ipv4_addr);
} else if (crane::ResolveIpv6FromHostname(craned_id, &ipv6_addr)) {
ip_addr = crane::Ipv6ToStr(ipv6_addr);
s_craned_id_to_ip_cache_map.emplace(craned_id, ipv6_addr);
} else {
// Just hostname. It should never happen,
// but we add error handling here for robustness.
CRANE_ERROR("Unresolved hostname: {}", craned_id);
ip_addr = craned_id;
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/CraneCtld/TaskScheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ class TaskScheduler {
m_cancel_task_queue_.enqueue(
CancelPendingTaskQueueElem{.task = std::move(pd_it->second)});
m_cancel_task_async_handle_->send();
m_pending_task_map_.erase(pd_it);
return CraneErr::kOk;
}

Expand Down