From 03d044b88dd1377281caa44c0133d9f22579ba90 Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Sat, 28 Nov 2026 22:16:45 +0100 Subject: Add a generation to a task on the workqueue to prevent its result being sent to the next client reusing the fd --- src/ot_workqueue.c | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) (limited to 'src/ot_workqueue.c') diff --git a/src/ot_workqueue.c b/src/ot_workqueue.c index 56a7cd2..cdefddc 100644 --- a/src/ot_workqueue.c +++ b/src/ot_workqueue.c @@ -9,6 +9,7 @@ #include /* Opentracker */ +#include "ot_fdstate.h" #include "ot_iovec.h" #include "ot_workqueue.h" #include "trackerlogic.h" @@ -20,6 +21,7 @@ struct ot_task { ot_taskid taskid; ot_tasktype tasktype; int64_t sock; + uint32_t generation; size_t iovec_entries; struct iovec *iovec; struct ot_task *next; @@ -40,6 +42,7 @@ int workqueue_pushtask(int64_t sock, ot_tasktype tasktype) { task->taskid = 0; task->tasktype = tasktype; task->sock = sock; + task->generation = fdstate_generation((int)sock); task->iovec_entries = 0; task->iovec = NULL; task->next = 0; @@ -65,22 +68,25 @@ void workqueue_canceltask(int64_t sock) { /* Want exclusive access to tasklist */ pthread_mutex_lock(&tasklist_mutex); - for (task = &tasklist; *task; task = &((*task)->next)) - if ((*task)->sock == sock) { - struct iovec *iovec = (*task)->iovec; - struct ot_task *ptask = *task; - size_t i; - - /* Free task's iovec, the buffers it points at and the vector itself */ - for (i = 0; i < (*task)->iovec_entries; ++i) - free(iovec[i].iov_base); - free(iovec); + task = &tasklist; + while (*task) { + struct ot_task *ptask = *task; + size_t i; - *task = (*task)->next; - free(ptask); - break; + if (ptask->sock != sock) { + task = &ptask->next; + continue; } + /* Free task's iovec, the buffers it points at and the vector itself */ + for (i = 0; i < ptask->iovec_entries; ++i) + free(ptask->iovec[i].iov_base); + free(ptask->iovec); + + *task = ptask->next; + free(ptask); + } + /* Release lock */ pthread_mutex_unlock(&tasklist_mutex); } @@ -190,7 +196,7 @@ int workqueue_pushchunked(ot_taskid taskid, struct iovec *iovec) { return task ? 0 : -1; } -int64_t workqueue_popresult(size_t *iovec_entries, struct iovec **iovec, int *is_partial) { +int64_t workqueue_popresult(size_t *iovec_entries, struct iovec **iovec, int *is_partial, uint32_t *generation) { struct ot_task **task; int64_t sock = -1; @@ -205,6 +211,7 @@ int64_t workqueue_popresult(size_t *iovec_entries, struct iovec **iovec, int *is *iovec_entries = ptask->iovec_entries; *iovec = ptask->iovec; sock = ptask->sock; + *generation = ptask->generation; if ((*task)->tasktype == TASK_DONE) { *task = ptask->next; -- cgit v1.3