diff --git a/main/threadpool.c b/main/threadpool.c
index 1bf6003ab8898f1e5e58fbbe9fd2bd300b44a264..f6755e3fb1d5dcd42b0644af19235e47c6295ddc 100644
--- a/main/threadpool.c
+++ b/main/threadpool.c
@@ -792,7 +792,7 @@ static struct set_size_data *set_size_data_alloc(struct ast_threadpool *pool,
  */
 static int queued_set_size(void *data)
 {
-	struct set_size_data *ssd = data;
+	RAII_VAR(struct set_size_data *, ssd, data, ao2_cleanup);
 	struct ast_threadpool *pool = ssd->pool;
 	unsigned int num_threads = ssd->size;
 
@@ -813,7 +813,6 @@ static int queued_set_size(void *data)
 	}
 
 	threadpool_send_state_changed(pool);
-	ao2_ref(ssd, -1);
 	return 0;
 }