diff options
| author | Dirk Engling <erdgeist@erdgeist.org> | 2026-11-28 22:16:45 +0100 |
|---|---|---|
| committer | Dirk Engling <erdgeist@erdgeist.org> | 2026-11-28 22:16:45 +0100 |
| commit | 03d044b88dd1377281caa44c0133d9f22579ba90 (patch) | |
| tree | 216c88a5344c2299c5120b035433e96d1fd2f91a | |
| parent | fdbd31bbb3e61cc42c2d12371975b892934dd51c (diff) | |
Add a generation to a task on the workqueue to prevent its result being sent to the next client reusing the fd
| -rw-r--r-- | DESIGN | 7 | ||||
| -rw-r--r-- | src/opentracker.c | 9 | ||||
| -rw-r--r-- | src/ot_tcp.c | 12 | ||||
| -rw-r--r-- | src/ot_workqueue.c | 35 | ||||
| -rw-r--r-- | src/ot_workqueue.h | 2 | ||||
| -rw-r--r-- | tests/t_workqueue.c | 52 |
6 files changed, 100 insertions, 17 deletions
| @@ -57,6 +57,13 @@ A socket is read or written, never both. http_interest_set replaces an | |||
| 57 | add-and-drop pair so that the invariant cannot be broken by accident, and the | 57 | add-and-drop pair so that the invariant cannot be broken by accident, and the |
| 58 | event layer hears about a change once instead of twice. | 58 | event layer hears about a change once instead of twice. |
| 59 | 59 | ||
| 60 | A task belongs to a connection, not to a descriptor number. The queue files | ||
| 61 | it under the number, and the kernel hands that number to the next connection | ||
| 62 | the moment this one closes. So a connection that is owed an answer is not | ||
| 63 | read from, closing it cancels every task filed under its number, and a result | ||
| 64 | carries the generation the descriptor had when the task was pushed, which the | ||
| 65 | main loop holds against the current one before it sends anything. | ||
| 66 | |||
| 60 | Accept waits until the client's request has arrived: TCP_DEFER_ACCEPT on | 67 | Accept waits until the client's request has arrived: TCP_DEFER_ACCEPT on |
| 61 | Linux, the accf_data filter on FreeBSD. Measured against a flood of silent | 68 | Linux, the accf_data filter on FreeBSD. Measured against a flood of silent |
| 62 | connections, the filter serves more requests than no filter, not fewer, so it | 69 | 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 @@ | |||
| 25 | #include "ot_config.h" | 25 | #include "ot_config.h" |
| 26 | #include "ot_hex.h" | 26 | #include "ot_hex.h" |
| 27 | #include "ot_http.h" | 27 | #include "ot_http.h" |
| 28 | #include "ot_iovec.h" | ||
| 28 | #include "ot_ip.h" | 29 | #include "ot_ip.h" |
| 29 | #include "ot_livesync.h" | 30 | #include "ot_livesync.h" |
| 30 | #include "ot_mutex.h" | 31 | #include "ot_mutex.h" |
| @@ -107,6 +108,7 @@ static void *server_mainloop(void *args) { | |||
| 107 | struct iovec *iovector; | 108 | struct iovec *iovector; |
| 108 | size_t iovec_entries; | 109 | size_t iovec_entries; |
| 109 | int is_partial; | 110 | int is_partial; |
| 111 | uint32_t generation; | ||
| 110 | 112 | ||
| 111 | (void)args; | 113 | (void)args; |
| 112 | 114 | ||
| @@ -149,8 +151,13 @@ static void *server_mainloop(void *args) { | |||
| 149 | } | 151 | } |
| 150 | } | 152 | } |
| 151 | 153 | ||
| 152 | while ((sock = workqueue_popresult(&iovec_entries, &iovector, &is_partial)) != -1) | 154 | while ((sock = workqueue_popresult(&iovec_entries, &iovector, &is_partial, &generation)) != -1) { |
| 155 | if (fdstate_generation((int)sock) != generation) { | ||
| 156 | iovec_free(&iovec_entries, &iovector); | ||
| 157 | continue; | ||
| 158 | } | ||
| 153 | http_sendiovecdata(sock, &ws, iovec_entries, iovector, is_partial); | 159 | http_sendiovecdata(sock, &ws, iovec_entries, iovector, is_partial); |
| 160 | } | ||
| 154 | 161 | ||
| 155 | if (time_is_past(next_timeout_check)) { | 162 | if (time_is_past(next_timeout_check)) { |
| 156 | int cursor = 0; | 163 | 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) { | |||
| 44 | 44 | ||
| 45 | void tcp_read(const int64_t sock, struct ot_workstruct *ws) { | 45 | void tcp_read(const int64_t sock, struct ot_workstruct *ws) { |
| 46 | struct http_data *cookie = fdstate_cookie(sock); | 46 | struct http_data *cookie = fdstate_cookie(sock); |
| 47 | ssize_t byte_count = read((int)sock, ws->inbuf, G_INBUF_SIZE); | 47 | ssize_t byte_count; |
| 48 | uint32_t generation; | 48 | uint32_t generation; |
| 49 | 49 | ||
| 50 | /* We're not supposed to read from a socket that waits for | ||
| 51 | our answer. Prevent its reply from being sent to the next | ||
| 52 | generation on that fd. */ | ||
| 53 | if (cookie->flag & (STRUCT_HTTP_FLAG_WAITINGFORTASK | STRUCT_HTTP_FLAG_CHUNKED_IN_TRANSFER)) { | ||
| 54 | http_close(sock); | ||
| 55 | return; | ||
| 56 | } | ||
| 57 | |||
| 58 | byte_count = read((int)sock, ws->inbuf, G_INBUF_SIZE); | ||
| 59 | |||
| 50 | if (byte_count == 0 || (byte_count == -1 && errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)) { | 60 | if (byte_count == 0 || (byte_count == -1 && errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)) { |
| 51 | http_close(sock); | 61 | http_close(sock); |
| 52 | return; | 62 | 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 @@ | |||
| 9 | #include <unistd.h> | 9 | #include <unistd.h> |
| 10 | 10 | ||
| 11 | /* Opentracker */ | 11 | /* Opentracker */ |
| 12 | #include "ot_fdstate.h" | ||
| 12 | #include "ot_iovec.h" | 13 | #include "ot_iovec.h" |
| 13 | #include "ot_workqueue.h" | 14 | #include "ot_workqueue.h" |
| 14 | #include "trackerlogic.h" | 15 | #include "trackerlogic.h" |
| @@ -20,6 +21,7 @@ struct ot_task { | |||
| 20 | ot_taskid taskid; | 21 | ot_taskid taskid; |
| 21 | ot_tasktype tasktype; | 22 | ot_tasktype tasktype; |
| 22 | int64_t sock; | 23 | int64_t sock; |
| 24 | uint32_t generation; | ||
| 23 | size_t iovec_entries; | 25 | size_t iovec_entries; |
| 24 | struct iovec *iovec; | 26 | struct iovec *iovec; |
| 25 | struct ot_task *next; | 27 | struct ot_task *next; |
| @@ -40,6 +42,7 @@ int workqueue_pushtask(int64_t sock, ot_tasktype tasktype) { | |||
| 40 | task->taskid = 0; | 42 | task->taskid = 0; |
| 41 | task->tasktype = tasktype; | 43 | task->tasktype = tasktype; |
| 42 | task->sock = sock; | 44 | task->sock = sock; |
| 45 | task->generation = fdstate_generation((int)sock); | ||
| 43 | task->iovec_entries = 0; | 46 | task->iovec_entries = 0; |
| 44 | task->iovec = NULL; | 47 | task->iovec = NULL; |
| 45 | task->next = 0; | 48 | task->next = 0; |
| @@ -65,22 +68,25 @@ void workqueue_canceltask(int64_t sock) { | |||
| 65 | /* Want exclusive access to tasklist */ | 68 | /* Want exclusive access to tasklist */ |
| 66 | pthread_mutex_lock(&tasklist_mutex); | 69 | pthread_mutex_lock(&tasklist_mutex); |
| 67 | 70 | ||
| 68 | for (task = &tasklist; *task; task = &((*task)->next)) | 71 | task = &tasklist; |
| 69 | if ((*task)->sock == sock) { | 72 | while (*task) { |
| 70 | struct iovec *iovec = (*task)->iovec; | 73 | struct ot_task *ptask = *task; |
| 71 | struct ot_task *ptask = *task; | 74 | size_t i; |
| 72 | size_t i; | ||
| 73 | |||
| 74 | /* Free task's iovec, the buffers it points at and the vector itself */ | ||
| 75 | for (i = 0; i < (*task)->iovec_entries; ++i) | ||
| 76 | free(iovec[i].iov_base); | ||
| 77 | free(iovec); | ||
| 78 | 75 | ||
| 79 | *task = (*task)->next; | 76 | if (ptask->sock != sock) { |
| 80 | free(ptask); | 77 | task = &ptask->next; |
| 81 | break; | 78 | continue; |
| 82 | } | 79 | } |
| 83 | 80 | ||
| 81 | /* Free task's iovec, the buffers it points at and the vector itself */ | ||
| 82 | for (i = 0; i < ptask->iovec_entries; ++i) | ||
| 83 | free(ptask->iovec[i].iov_base); | ||
| 84 | free(ptask->iovec); | ||
| 85 | |||
| 86 | *task = ptask->next; | ||
| 87 | free(ptask); | ||
| 88 | } | ||
| 89 | |||
| 84 | /* Release lock */ | 90 | /* Release lock */ |
| 85 | pthread_mutex_unlock(&tasklist_mutex); | 91 | pthread_mutex_unlock(&tasklist_mutex); |
| 86 | } | 92 | } |
| @@ -190,7 +196,7 @@ int workqueue_pushchunked(ot_taskid taskid, struct iovec *iovec) { | |||
| 190 | return task ? 0 : -1; | 196 | return task ? 0 : -1; |
| 191 | } | 197 | } |
| 192 | 198 | ||
| 193 | int64_t workqueue_popresult(size_t *iovec_entries, struct iovec **iovec, int *is_partial) { | 199 | int64_t workqueue_popresult(size_t *iovec_entries, struct iovec **iovec, int *is_partial, uint32_t *generation) { |
| 194 | struct ot_task **task; | 200 | struct ot_task **task; |
| 195 | int64_t sock = -1; | 201 | int64_t sock = -1; |
| 196 | 202 | ||
| @@ -205,6 +211,7 @@ int64_t workqueue_popresult(size_t *iovec_entries, struct iovec **iovec, int *is | |||
| 205 | *iovec_entries = ptask->iovec_entries; | 211 | *iovec_entries = ptask->iovec_entries; |
| 206 | *iovec = ptask->iovec; | 212 | *iovec = ptask->iovec; |
| 207 | sock = ptask->sock; | 213 | sock = ptask->sock; |
| 214 | *generation = ptask->generation; | ||
| 208 | 215 | ||
| 209 | if ((*task)->tasktype == TASK_DONE) { | 216 | if ((*task)->tasktype == TASK_DONE) { |
| 210 | *task = ptask->next; | 217 | *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); | |||
| 64 | ot_taskid workqueue_poptask(ot_tasktype *tasktype); | 64 | ot_taskid workqueue_poptask(ot_tasktype *tasktype); |
| 65 | int workqueue_pushresult(ot_taskid taskid, size_t iovec_entries, struct iovec *iovector); | 65 | int workqueue_pushresult(ot_taskid taskid, size_t iovec_entries, struct iovec *iovector); |
| 66 | int workqueue_pushchunked(ot_taskid taskid, struct iovec *iovec); | 66 | int workqueue_pushchunked(ot_taskid taskid, struct iovec *iovec); |
| 67 | int64_t workqueue_popresult(size_t *iovec_entries, struct iovec **iovector, int *is_partial); | 67 | int64_t workqueue_popresult(size_t *iovec_entries, struct iovec **iovector, int *is_partial, uint32_t *generation); |
| 68 | 68 | ||
| 69 | void workqueue_init(void); | 69 | void workqueue_init(void); |
| 70 | void workqueue_deinit(void); | 70 | 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 @@ | |||
| 6 | #include <string.h> | 6 | #include <string.h> |
| 7 | 7 | ||
| 8 | /* Opentracker */ | 8 | /* Opentracker */ |
| 9 | #include "ot_fdstate.h" | ||
| 9 | #include "ot_workqueue.h" | 10 | #include "ot_workqueue.h" |
| 10 | #include "test.h" | 11 | #include "test.h" |
| 11 | 12 | ||
| @@ -96,10 +97,61 @@ static void case_cancelled_task_is_not_handed_out(void) { | |||
| 96 | workqueue_canceltask(48); | 97 | workqueue_canceltask(48); |
| 97 | } | 98 | } |
| 98 | 99 | ||
| 100 | /* Tasks are filed under a descriptor number, and the number is handed to the | ||
| 101 | next connection the moment this one closes. */ | ||
| 102 | |||
| 103 | static void case_cancel_takes_every_task_of_the_socket(void) { | ||
| 104 | ot_tasktype asked; | ||
| 105 | |||
| 106 | test_case("cancelling a socket leaves none of its tasks behind"); | ||
| 107 | CHECK_INT(workqueue_pushtask(49, TASK_FULLSCRAPE_TPB_ASCII), 0); | ||
| 108 | CHECK_INT(workqueue_pushtask(49, TASK_FULLSCRAPE_TPB_BINARY), 0); | ||
| 109 | CHECK_INT(workqueue_pushtask(50, TASK_FULLSCRAPE), 0); | ||
| 110 | workqueue_canceltask(49); | ||
| 111 | |||
| 112 | asked = TASK_FULLSCRAPE; | ||
| 113 | CHECK(workqueue_poptask(&asked) != 0); | ||
| 114 | CHECK_INT(asked, TASK_FULLSCRAPE); | ||
| 115 | workqueue_canceltask(50); | ||
| 116 | } | ||
| 117 | |||
| 118 | static void case_result_names_the_connection_that_asked(void) { | ||
| 119 | ot_tasktype asked = TASK_STATS; | ||
| 120 | ot_taskid taskid; | ||
| 121 | struct iovec *iovector; | ||
| 122 | size_t iovec_entries; | ||
| 123 | int is_partial; | ||
| 124 | uint32_t asked_under, answered_under = ~0u; | ||
| 125 | |||
| 126 | CHECK_INT(fdstate_init(), 0); | ||
| 127 | asked_under = fdstate_open(51, NULL, 0); | ||
| 128 | |||
| 129 | test_case("a result comes back with the generation its task was pushed under"); | ||
| 130 | CHECK_INT(workqueue_pushtask(51, TASK_STATS_PEERS), 0); | ||
| 131 | taskid = workqueue_poptask(&asked); | ||
| 132 | CHECK(taskid != 0); | ||
| 133 | |||
| 134 | /* The connection goes away without cancelling and the number is reused */ | ||
| 135 | fdstate_close(51); | ||
| 136 | fdstate_open(51, NULL, 0); | ||
| 137 | |||
| 138 | CHECK_INT(workqueue_pushresult(taskid, 0, NULL), 0); | ||
| 139 | CHECK_INT(workqueue_popresult(&iovec_entries, &iovector, &is_partial, &answered_under), 51); | ||
| 140 | CHECK_INT(answered_under, asked_under); | ||
| 141 | |||
| 142 | test_case("which is no longer the one the descriptor carries"); | ||
| 143 | CHECK(fdstate_generation(51) != answered_under); | ||
| 144 | |||
| 145 | fdstate_close(51); | ||
| 146 | fdstate_deinit(); | ||
| 147 | } | ||
| 148 | |||
| 99 | void test_workqueue(void) { | 149 | void test_workqueue(void) { |
| 100 | case_task_goes_to_its_own_class(); | 150 | case_task_goes_to_its_own_class(); |
| 101 | case_flags_survive_the_queue(); | 151 | case_flags_survive_the_queue(); |
| 102 | case_classes_do_not_cross(); | 152 | case_classes_do_not_cross(); |
| 103 | case_every_task_gets_its_own_id(); | 153 | case_every_task_gets_its_own_id(); |
| 104 | case_cancelled_task_is_not_handed_out(); | 154 | case_cancelled_task_is_not_handed_out(); |
| 155 | case_cancel_takes_every_task_of_the_socket(); | ||
| 156 | case_result_names_the_connection_that_asked(); | ||
| 105 | } | 157 | } |
