From 5381697dbc3507705d3f9f6e0946e0982242eaff Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Sun, 2 Jan 2022 20:49:45 +0000 Subject: [PATCH] [Fix] Fix removing from khash --- contrib/librdns/resolver.c | 2 +- contrib/librdns/util.c | 33 +++++++++++++++++++-------------- contrib/librdns/util.h | 6 ++++++ 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/contrib/librdns/resolver.c b/contrib/librdns/resolver.c index 05d838317..c27d9afcc 100644 --- a/contrib/librdns/resolver.c +++ b/contrib/librdns/resolver.c @@ -507,7 +507,7 @@ rdns_process_timer (void *arg) req->async->del_timer (req->async->data, req->async_event); req->async_event = NULL; - kh_del(rdns_requests_hash, req->io->requests, req->id); + rdns_request_remove_from_hash(req); } /* We have not scheduled timeout actually due to send error */ diff --git a/contrib/librdns/util.c b/contrib/librdns/util.c index 9b74b7466..2fc9ec6c3 100644 --- a/contrib/librdns/util.c +++ b/contrib/librdns/util.c @@ -40,6 +40,21 @@ #include "logger.h" #include "rdns.h" +inline void +rdns_request_remove_from_hash (struct rdns_request *req) +{ + /* Remove from id hashes */ + if (req->io) { + khiter_t k; + + k = kh_get(rdns_requests_hash, req->io->requests, req->id); + + if (k != kh_end(req->io->requests)) { + kh_del(rdns_requests_hash, req->io->requests, req->id); + } + } +} + static int rdns_make_socket_nonblocking (int fd) { @@ -457,19 +472,14 @@ rdns_request_free (struct rdns_request *req) /* Remove timer */ req->async->del_timer (req->async->data, req->async_event); - /* Remove from id hashes */ - if (req->io) { - kh_del(rdns_requests_hash, req->io->requests, req->id); - } + rdns_request_remove_from_hash(req); req->async_event = NULL; } else if (req->state == RDNS_REQUEST_WAIT_SEND) { /* Remove retransmit event */ req->async->del_write (req->async->data, req->async_event); - if (req->io) { - kh_del(rdns_requests_hash, req->io->requests, req->id); - } + rdns_request_remove_from_hash(req); req->async_event = NULL; } else if (req->state == RDNS_REQUEST_FAKE) { @@ -605,19 +615,14 @@ rdns_request_unschedule (struct rdns_request *req) if (req->state == RDNS_REQUEST_WAIT_REPLY) { req->async->del_timer (req->async->data, req->async_event); - /* Remove from id hashes */ - if (req->io) { - kh_del(rdns_requests_hash, req->io->requests, req->id); - } + req->async_event = NULL; } else if (req->state == RDNS_REQUEST_WAIT_SEND) { req->async->del_write (req->async->data, req->async_event); /* Remove from id hashes */ - if (req->io) { - kh_del(rdns_requests_hash, req->io->requests, req->id); - } + rdns_request_remove_from_hash(req); req->async_event = NULL; } } diff --git a/contrib/librdns/util.h b/contrib/librdns/util.h index 5fc94eb80..eea818dee 100644 --- a/contrib/librdns/util.h +++ b/contrib/librdns/util.h @@ -63,6 +63,12 @@ struct rdns_io_channel * rdns_ioc_new (struct rdns_server *srv, */ void rdns_request_free (struct rdns_request *req); +/** + * Removes request from a channel's hash (e.g. if needed to migrate to another channel) + * @param req + */ +void rdns_request_remove_from_hash (struct rdns_request *req); + /** * Free reply * @param rep -- 2.39.5