From 7b9b0781146dde69378d53bafd3c8817fb8909e9 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Tue, 12 Jul 2011 19:19:00 +1000 Subject: [PATCH] Change get_work to use a timeout when trying to tq_pop as a sanity failsafe in case of unusual circumstances. --- main.c | 13 ++++++++++--- util.c | 6 +++++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/main.c b/main.c index 8d59f6c2..8d049fd7 100644 --- a/main.c +++ b/main.c @@ -1149,6 +1149,8 @@ static void flush_requests(bool longpoll) static bool get_work(struct work *work, bool queued) { + struct timeval now; + struct timespec abstime = {}; struct thr_info *thr = &thr_info[0]; struct work *work_heap; bool ret = false; @@ -1157,6 +1159,9 @@ static bool get_work(struct work *work, bool queued) getwork_requested++; retry: + gettimeofday(&now, NULL); + abstime.tv_sec = now.tv_sec + 60; + if (unlikely(!queued && !queue_request())) { applog(LOG_WARNING, "Failed to queue_request in get_work"); goto out; @@ -1181,10 +1186,12 @@ retry: applog(LOG_WARNING, "Resumed retrieving work from server"); } - /* wait for 1st response, or get cached response */ - work_heap = tq_pop(thr->q, NULL); + /* Wait for 1st response, or get cached response. We really should + * never time out on the pop request but something might go amiss :/ + */ + work_heap = tq_pop(thr->q, &abstime); if (unlikely(!work_heap)) { - applog(LOG_WARNING, "Failed to tq_pop in get_work"); + applog(LOG_INFO, "Failed to tq_pop in get_work"); goto out; } dec_queued(); diff --git a/util.c b/util.c index 2be8a3b4..5205c348 100644 --- a/util.c +++ b/util.c @@ -21,6 +21,7 @@ #include #include #include +#include #include "miner.h" #include "elist.h" @@ -559,8 +560,11 @@ void *tq_pop(struct thread_q *tq, const struct timespec *abstime) rc = pthread_cond_timedwait(&tq->cond, &tq->mutex, abstime); else rc = pthread_cond_wait(&tq->cond, &tq->mutex); - if (rc) + if (unlikely(rc)) { + if (rc == ETIMEDOUT) + applog(LOG_WARNING, "Timed out waiting in tq_pop"); goto out; + } if (list_empty(&tq->q)) goto out;