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 --- tests/t_workqueue.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'tests') 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