From 73ba6004b906b1ea022c169b25b9bdab589583e4 Mon Sep 17 00:00:00 2001
From: Quentin Armitage <quentin@armitage.org.uk>
Date: Fri, 22 Oct 2021 15:16:01 +0100
Subject: [PATCH] scheduler: Handle cancelling timer thread on ready queue

The timer thread on the ready queue, if cancelled, was corrupting
the read list_head, since it assumed it was on a red black tree.

Signed-off-by: Quentin Armitage <quentin@armitage.org.uk>
Conflict: NA
Reference: https://github.com/acassen/keepalived/commit/73ba6004b906b1ea022c169b25b9bdab589583e4
---
 lib/scheduler.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/lib/scheduler.c b/lib/scheduler.c
index a588f52..ebf5ea3 100644
--- a/lib/scheduler.c
+++ b/lib/scheduler.c
@@ -1527,6 +1527,7 @@ thread_cancel(thread_ref_t thread_cp)
 		break;
 	case THREAD_EVENT:
 	case THREAD_READY:
+	case THREAD_READY_TIMER:
 #ifdef USE_SIGNAL_THREADS
 	case THREAD_SIGNAL:
 #endif
@@ -1534,7 +1535,13 @@ thread_cancel(thread_ref_t thread_cp)
 	case THREAD_CHILD_TERMINATED:
 		list_del_init(&thread->e_list);
 		break;
+	case THREAD_TIMER_SHUTDOWN:
+	case THREAD_TERMINATE_START:
+	case THREAD_TERMINATE:
+		log_message(LOG_WARNING, "ERROR - thread_cancel called for THREAD_%s", thread->type == THREAD_TIMER_SHUTDOWN ? "TIMER_SHUTDOWN" : thread->type == THREAD_TERMINATE ? "TERMINATE" : "TERMINATE_START");
+		return;
 	default:
+		log_message(LOG_WARNING, "ERROR - thread_cancel called for unknown thread type %u", thread->type);
 		break;
 	}
 
-- 
2.23.0

