Restart longpoll under lock when we'll be reusing it on pool switch to prevent derefs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
diff --git a/main.c b/main.c
index 11d8b97..9edc01d 100644
--- a/main.c
+++ b/main.c
@@ -2455,11 +2455,12 @@ static struct pool *priority_pool(int choice)
return ret;
}
-static void restart_longpoll(void);
+static void __restart_longpoll(void);
static void switch_pools(struct pool *selected)
{
struct pool *pool, *last_pool;
+ bool new_pool = false;
int i, pool_no;
mutex_lock(&control_lock);
@@ -2507,13 +2508,23 @@ static void switch_pools(struct pool *selected)
currentpool = pools[pool_no];
pool = currentpool;
- mutex_unlock(&control_lock);
if (pool != last_pool) {
- applog(LOG_WARNING, "Switching to %s", pool->rpc_url);
+ new_pool = true;
/* Only switch longpoll if the new pool also supports LP */
if (pool->hdr_path)
- restart_longpoll();
+ __restart_longpoll();
+ }
+
+ mutex_unlock(&control_lock);
+
+ if (new_pool) {
+ applog(LOG_WARNING, "Switching to %s", pool->rpc_url);
+ if (want_longpoll) {
+ if (opt_debug)
+ applog(LOG_DEBUG, "Pushing ping to longpoll thread");
+ tq_push(thr_info[longpoll_thr_id].q, &ping);
+ }
}
/* Reset the queued amount to allow more to be queued for the new pool */
@@ -3003,6 +3014,8 @@ retry:
opt_loginput = false;
}
+static void restart_longpoll(void);
+
static void set_options(void)
{
int selected;
@@ -4472,6 +4485,23 @@ static void restart_longpoll(void)
start_longpoll();
}
+/* Version for when we are holding a lock, grabs no other locks to prevent
+ * deadlock */
+static void __restart_longpoll(void)
+{
+ struct thr_info *thr = &thr_info[longpoll_thr_id];
+
+ thr_info_cancel(thr);
+ have_longpoll = false;
+
+ if (!want_longpoll)
+ return;
+
+ tq_thaw(thr->q);
+ if (unlikely(thr_info_create(thr, NULL, longpoll_thread, thr)))
+ quit(1, "longpoll thread create failed");
+}
+
static void *reinit_cpu(void *userdata)
{
pthread_detach(pthread_self());