From 0b5917abc17e02e884e359d1e2146a19daecd944 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C4=81ris=20Narti=C5=A1s?= Date: Wed, 27 Sep 2023 14:44:10 +0300 Subject: [PATCH] mapcache_seed: try to kill still running threads after grace period (#313) --- util/mapcache_seed.c | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/util/mapcache_seed.c b/util/mapcache_seed.c index 08b46ddd..7778e15d 100644 --- a/util/mapcache_seed.c +++ b/util/mapcache_seed.c @@ -485,7 +485,23 @@ void cmd_recurse(mapcache_context *cmd_ctx, mapcache_tile *tile) if(sig_int_received || error_detected) { //stop if we were asked to stop by hitting ctrl-c //remove all items from the queue struct seed_cmd entry; - while (trypop_queue(&entry)!=APR_EAGAIN) /*do nothing*/; + int retry_count = 0; + int ret = trypop_queue(&entry); + while (ret != APR_EAGAIN) { + // try to empty queue with a graceful retreat up to 55 seconds + // for retries before forcefully terminating threads + if (ret == APR_EOF) + break; + if (ret != APR_SUCCESS) + retry_count++; + if (retry_count > 10) { + printf("Feed worker threads failed to terminate. Stopping forcefully.\n"); + apr_queue_interrupt_all(work_queue); + break; + } + apr_sleep(retry_count * 1000000); + ret = trypop_queue(&entry); + } return; } @@ -614,7 +630,23 @@ void feed_worker() if(sig_int_received || error_detected) { //stop if we were asked to stop by hitting ctrl-c //remove all items from the queue struct seed_cmd entry; - while (trypop_queue(&entry)!=APR_EAGAIN) /* do nothing */; + int retry_count = 0; + int ret = trypop_queue(&entry); + while (ret != APR_EAGAIN) { + // try to empty queue with a graceful retreat up to 55 seconds + // for retries before forcefully terminating threads + if (ret == APR_EOF) + break; + if (ret != APR_SUCCESS) + retry_count++; + if (retry_count > 10) { + printf("Feed worker threads failed to terminate. Stopping forcefully.\n"); + apr_queue_interrupt_all(work_queue); + break; + } + apr_sleep(retry_count * 1000000); + ret = trypop_queue(&entry); + } break; } if(iteration_mode == MAPCACHE_ITERATION_LOG) {