Commit 9e7b8c5004 for asterisk.org
commit 9e7b8c500487f78eea9b94d72aa4dbb4c62a3672
Author: Puja Gediya <puja.gediya@ecosmob.com>
Date: Thu Nov 19 11:09:08 2026 +0530
taskpool: Only shrink taskprocessors that are actually idle
taskpool_dynamic_pool_shrink() considered a dynamic taskprocessor idle
when nothing had been pushed to it for the idle timeout, even if it was
still executing a task or had tasks queued. A serializer drain in
execute_tasks() can run for longer than that, as tasks pushed to the
serializer meanwhile do not touch the taskprocessor. Destroying the
taskprocessor joins its thread with the pool lock held, so when the
running task then needed the pool lock, for example execute_tasks()
requeueing the serializer with ast_taskpool_push(), the shrink and the
task waited for each other. Every other push to the pool then blocked on
the pool lock too, which stopped all PJSIP processing.
A taskprocessor is now only idle when, in addition, it has no queued or
executing task. Its queue size and executing state are read with the
taskprocessor lock held so they are consistent.
Add the push_shrink_while_executing unit test: a task outlives the idle
timeout on a dynamic taskprocessor and then pushes to the pool. It
deadlocks without this change, and it verifies that the taskprocessor
was not removed while the task was running.
Resolves: #2314
UserNote: Fixed a deadlock that could stop all PJSIP processing when the
PJSIP taskpool shrank while a dynamic taskprocessor was still running a
task.
diff --git a/main/taskpool.c b/main/taskpool.c
index 832a877da4..6707b1832a 100644
--- a/main/taskpool.c
+++ b/main/taskpool.c
@@ -231,8 +231,28 @@ static void taskpool_taskprocessors_cleanup(struct taskpool_taskprocessors *task
/*!
* \internal
* \brief Determine if a taskpool taskprocessor is idle
+ *
+ * Nothing must have been pushed to it within the timeout, and it must have no
+ * queued or executing task. A task can run for longer than the timeout, and
+ * destroying the taskprocessor joins its thread.
+ *
+ * The taskprocessor lock is held so the queue size and the executing state are
+ * consistent, a task is dequeued and marked as executing under it.
*/
-#define TASKPROCESSOR_IS_IDLE(tps, timeout) (ast_tvdiff_ms(ast_tvnow(), tps->last_pushed) > (timeout))
+static int taskpool_taskprocessor_is_idle(struct taskpool_taskprocessor *tp, long timeout)
+{
+ long load;
+
+ if (ast_tvdiff_ms(ast_tvnow(), tp->last_pushed) <= timeout) {
+ return 0;
+ }
+
+ ao2_lock(tp->taskprocessor);
+ load = taskpool_taskprocessor_load(tp);
+ ao2_unlock(tp->taskprocessor);
+
+ return !load;
+}
/*! \internal
* \brief Taskpool dynamic pool shrink function
@@ -253,7 +273,7 @@ static int taskpool_dynamic_pool_shrink(const void *data)
/* Go through the dynamic taskprocessors and find any which have been idle long enough and remove them */
num_removed = AST_VECTOR_REMOVE_ALL_CMP_UNORDERED(&pool->dynamic_taskprocessors.taskprocessors, pool->options.idle_timeout * 1000,
- TASKPROCESSOR_IS_IDLE, ao2_cleanup);
+ taskpool_taskprocessor_is_idle, ao2_cleanup);
if (num_removed) {
/* If we've removed any taskprocessors the taskprocessor_num may no longer be valid, so update it */
if (pool->dynamic_taskprocessors.taskprocessor_num >= AST_VECTOR_SIZE(&pool->dynamic_taskprocessors.taskprocessors)) {
diff --git a/tests/test_taskpool.c b/tests/test_taskpool.c
index a4f25a6636..f0ce31f252 100644
--- a/tests/test_taskpool.c
+++ b/tests/test_taskpool.c
@@ -551,6 +551,107 @@ end:
return res;
}
+static int noop_task(void *data)
+{
+ return 0;
+}
+
+/*! \brief Outlives idle_timeout on a dynamic taskprocessor, then pushes to the pool like execute_tasks does */
+static int push_after_idle_timeout_task(void *data)
+{
+ struct test_data *td = data;
+ int res;
+
+ sleep(3);
+
+ /* Takes the pool lock, so this deadlocks if the shrink is joining this thread */
+ if (ast_taskpool_taskprocessors_count(td->test_specific_data) != 1) {
+ /* The shrink removed this taskprocessor while it was executing */
+ res = -2;
+ } else {
+ res = ast_taskpool_push(td->test_specific_data, noop_task, NULL) ? -1 : 1;
+ }
+
+ ast_mutex_lock(&td->lock);
+ td->executed = res;
+ ast_cond_signal(&td->cond);
+ ast_mutex_unlock(&td->lock);
+ return 0;
+}
+
+AST_TEST_DEFINE(taskpool_push_shrink_while_executing)
+{
+ struct ast_taskpool *pool = NULL;
+ struct test_data *td = NULL;
+ struct ast_taskpool_options options = {
+ .version = AST_TASKPOOL_OPTIONS_VERSION,
+ .idle_timeout = 1,
+ .auto_increment = 1,
+ .minimum_size = 0,
+ .initial_size = 0,
+ .max_size = 1,
+ };
+ enum ast_test_result_state res = AST_TEST_PASS;
+ struct timeval start;
+ struct timespec end;
+
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = "push_shrink_while_executing";
+ info->category = "/main/taskpool/";
+ info->summary = "Taskpool shrink does not remove a taskprocessor executing a task";
+ info->description =
+ "Runs a task on a dynamic taskprocessor for longer than the idle timeout, then\n"
+ "has the task push to the pool. The shrink must not remove the taskprocessor\n"
+ "while it is executing, and the push must not deadlock against the shrink.";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
+ td = test_alloc();
+ if (!td) {
+ return AST_TEST_FAIL;
+ }
+
+ pool = ast_taskpool_create(info->name, &options);
+ if (!pool) {
+ res = AST_TEST_FAIL;
+ goto end;
+ }
+ td->test_specific_data = pool;
+
+ if (ast_taskpool_push(pool, push_after_idle_timeout_task, td)) {
+ res = AST_TEST_FAIL;
+ goto end;
+ }
+
+ /* The task sleeps 3 seconds, so 10 seconds is plenty unless it is deadlocked */
+ start = ast_tvnow();
+ end.tv_sec = start.tv_sec + 10;
+ end.tv_nsec = start.tv_usec * 1000;
+
+ ast_mutex_lock(&td->lock);
+ while (!td->executed && ast_cond_timedwait(&td->cond, &td->lock, &end) != ETIMEDOUT) {
+ }
+ ast_mutex_unlock(&td->lock);
+
+ if (td->executed != 1) {
+ ast_test_status_update(test, "Task running past the idle timeout %s\n",
+ !td->executed ? "deadlocked against the pool shrink" :
+ td->executed == -2 ? "was removed by the shrink while executing" : "failed to push");
+ if (!td->executed) {
+ /* The pool and its threads are deadlocked, shutting it down would hang the test run */
+ return AST_TEST_FAIL;
+ }
+ res = AST_TEST_FAIL;
+ }
+
+end:
+ ast_taskpool_shutdown(pool);
+ test_destroy(td);
+ return res;
+}
+
AST_TEST_DEFINE(taskpool_serializer_suspension)
{
struct ast_taskpool *pool = NULL;
@@ -1290,6 +1391,7 @@ static int unload_module(void)
AST_TEST_UNREGISTER(taskpool_push_serializer_synchronous_requeue);
AST_TEST_UNREGISTER(taskpool_push_grow);
AST_TEST_UNREGISTER(taskpool_push_shrink);
+ AST_TEST_UNREGISTER(taskpool_push_shrink_while_executing);
AST_TEST_UNREGISTER(taskpool_serializer_suspension);
AST_TEST_UNREGISTER(taskpool_serializer_multiple_suspension);
AST_TEST_UNREGISTER(taskpool_serializer_push_wait_while_suspended_from_other_serializer);
@@ -1307,6 +1409,7 @@ static int load_module(void)
AST_TEST_REGISTER(taskpool_push_serializer_synchronous_requeue);
AST_TEST_REGISTER(taskpool_push_grow);
AST_TEST_REGISTER(taskpool_push_shrink);
+ AST_TEST_REGISTER(taskpool_push_shrink_while_executing);
AST_TEST_REGISTER(taskpool_serializer_suspension);
AST_TEST_REGISTER(taskpool_serializer_multiple_suspension);
AST_TEST_REGISTER(taskpool_serializer_push_wait_while_suspended_from_other_serializer);