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/opentracker.c | 9 ++++++++- src/ot_tcp.c | 12 +++++++++++- src/ot_workqueue.c | 35 +++++++++++++++++++++-------------- src/ot_workqueue.h | 2 +- 4 files changed, 41 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/opentracker.c b/src/opentracker.c index 759a6d0..36dff71 100644 --- a/src/opentracker.c +++ b/src/opentracker.c @@ -25,6 +25,7 @@ #include "ot_config.h" #include "ot_hex.h" #include "ot_http.h" +#include "ot_iovec.h" #include "ot_ip.h" #include "ot_livesync.h" #include "ot_mutex.h" @@ -107,6 +108,7 @@ static void *server_mainloop(void *args) { struct iovec *iovector; size_t iovec_entries; int is_partial; + uint32_t generation; (void)args; @@ -149,8 +151,13 @@ static void *server_mainloop(void *args) { } } - while ((sock = workqueue_popresult(&iovec_entries, &iovector, &is_partial)) != -1) + while ((sock = workqueue_popresult(&iovec_entries, &iovector, &is_partial, &generation)) != -1) { + if (fdstate_generation((int)sock) != generation) { + iovec_free(&iovec_entries, &iovector); + continue; + } http_sendiovecdata(sock, &ws, iovec_entries, iovector, is_partial); + } if (time_is_past(next_timeout_check)) { int cursor = 0; diff --git a/src/ot_tcp.c b/src/ot_tcp.c index 5d7b78f..67a0ebc 100644 --- a/src/ot_tcp.c +++ b/src/ot_tcp.c @@ -44,9 +44,19 @@ static ssize_t header_complete(char *request, ssize_t byte_count) { void tcp_read(const int64_t sock, struct ot_workstruct *ws) { struct http_data *cookie = fdstate_cookie(sock); - ssize_t byte_count = read((int)sock, ws->inbuf, G_INBUF_SIZE); + ssize_t byte_count; uint32_t generation; + /* We're not supposed to read from a socket that waits for + our answer. Prevent its reply from being sent to the next + generation on that fd. */ + if (cookie->flag & (STRUCT_HTTP_FLAG_WAITINGFORTASK | STRUCT_HTTP_FLAG_CHUNKED_IN_TRANSFER)) { + http_close(sock); + return; + } + + byte_count = read((int)sock, ws->inbuf, G_INBUF_SIZE); + if (byte_count == 0 || (byte_count == -1 && errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)) { http_close(sock); return; 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; diff --git a/src/ot_workqueue.h b/src/ot_workqueue.h index d8f30d6..982292e 100644 --- a/src/ot_workqueue.h +++ b/src/ot_workqueue.h @@ -64,7 +64,7 @@ void workqueue_pushsuccess(ot_taskid taskid); ot_taskid workqueue_poptask(ot_tasktype *tasktype); int workqueue_pushresult(ot_taskid taskid, size_t iovec_entries, struct iovec *iovector); int workqueue_pushchunked(ot_taskid taskid, struct iovec *iovec); -int64_t workqueue_popresult(size_t *iovec_entries, struct iovec **iovector, int *is_partial); +int64_t workqueue_popresult(size_t *iovec_entries, struct iovec **iovector, int *is_partial, uint32_t *generation); void workqueue_init(void); void workqueue_deinit(void); -- cgit v1.3