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 --- DESIGN | 7 +++++++ src/opentracker.c | 9 ++++++++- src/ot_tcp.c | 12 +++++++++++- src/ot_workqueue.c | 35 +++++++++++++++++++++-------------- src/ot_workqueue.h | 2 +- tests/t_workqueue.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 100 insertions(+), 17 deletions(-) diff --git a/DESIGN b/DESIGN index 480aa9b..2e1883e 100644 --- a/DESIGN +++ b/DESIGN @@ -57,6 +57,13 @@ A socket is read or written, never both. http_interest_set replaces an add-and-drop pair so that the invariant cannot be broken by accident, and the event layer hears about a change once instead of twice. +A task belongs to a connection, not to a descriptor number. The queue files +it under the number, and the kernel hands that number to the next connection +the moment this one closes. So a connection that is owed an answer is not +read from, closing it cancels every task filed under its number, and a result +carries the generation the descriptor had when the task was pushed, which the +main loop holds against the current one before it sends anything. + Accept waits until the client's request has arrived: TCP_DEFER_ACCEPT on Linux, the accf_data filter on FreeBSD. Measured against a flood of silent connections, the filter serves more requests than no filter, not fewer, so it 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); diff --git a/tests/t_workqueue.c b/tests/t_workqueue.c index c454e5b..660d882 100644 --- a/tests/t_workqueue.c +++ b/tests/t_workqueue.c @@ -6,6 +6,7 @@ #include /* Opentracker */ +#include "ot_fdstate.h" #include "ot_workqueue.h" #include "test.h" @@ -96,10 +97,61 @@ static void case_cancelled_task_is_not_handed_out(void) { workqueue_canceltask(48); } +/* Tasks are filed under a descriptor number, and the number is handed to the + next connection the moment this one closes. */ + +static void case_cancel_takes_every_task_of_the_socket(void) { + ot_tasktype asked; + + test_case("cancelling a socket leaves none of its tasks behind"); + CHECK_INT(workqueue_pushtask(49, TASK_FULLSCRAPE_TPB_ASCII), 0); + CHECK_INT(workqueue_pushtask(49, TASK_FULLSCRAPE_TPB_BINARY), 0); + CHECK_INT(workqueue_pushtask(50, TASK_FULLSCRAPE), 0); + workqueue_canceltask(49); + + asked = TASK_FULLSCRAPE; + CHECK(workqueue_poptask(&asked) != 0); + CHECK_INT(asked, TASK_FULLSCRAPE); + workqueue_canceltask(50); +} + +static void case_result_names_the_connection_that_asked(void) { + ot_tasktype asked = TASK_STATS; + ot_taskid taskid; + struct iovec *iovector; + size_t iovec_entries; + int is_partial; + uint32_t asked_under, answered_under = ~0u; + + CHECK_INT(fdstate_init(), 0); + asked_under = fdstate_open(51, NULL, 0); + + test_case("a result comes back with the generation its task was pushed under"); + CHECK_INT(workqueue_pushtask(51, TASK_STATS_PEERS), 0); + taskid = workqueue_poptask(&asked); + CHECK(taskid != 0); + + /* The connection goes away without cancelling and the number is reused */ + fdstate_close(51); + fdstate_open(51, NULL, 0); + + CHECK_INT(workqueue_pushresult(taskid, 0, NULL), 0); + CHECK_INT(workqueue_popresult(&iovec_entries, &iovector, &is_partial, &answered_under), 51); + CHECK_INT(answered_under, asked_under); + + test_case("which is no longer the one the descriptor carries"); + CHECK(fdstate_generation(51) != answered_under); + + fdstate_close(51); + fdstate_deinit(); +} + void test_workqueue(void) { case_task_goes_to_its_own_class(); case_flags_survive_the_queue(); case_classes_do_not_cross(); case_every_task_gets_its_own_id(); case_cancelled_task_is_not_handed_out(); + case_cancel_takes_every_task_of_the_socket(); + case_result_names_the_connection_that_asked(); } -- cgit v1.3