From 253432c918dacc8bfd5d9a6a46c45a9dc682c7ec Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Tue, 3 Nov 2026 02:45:55 +0100 Subject: Give the live sync worker a real workstruct --- src/opentracker.c | 2 +- src/ot_livesync.c | 12 ++++++++---- src/ot_worker.c | 2 +- src/trackerlogic.c | 6 ++++-- src/trackerlogic.h | 2 +- tests/integration/test_livesync.py | 11 +++++++++++ tests/t_trackerlogic.c | 2 +- 7 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/opentracker.c b/src/opentracker.c index 14b817e..60a6d8c 100644 --- a/src/opentracker.c +++ b/src/opentracker.c @@ -109,7 +109,7 @@ static void *server_mainloop(void *args) { (void)args; - if (workstruct_init(&ws) == -1) + if (workstruct_init(&ws, G_INBUF_SIZE) == -1) panic("Initializing worker failed"); while (g_opentracker_running) { diff --git a/src/ot_livesync.c b/src/ot_livesync.c index 083fe1a..18dae0d 100644 --- a/src/ot_livesync.c +++ b/src/ot_livesync.c @@ -4,6 +4,7 @@ /* System */ #include #include +#include #include #include #include @@ -200,9 +201,12 @@ static void *livesync_worker(void *args) { (void)args; - /* Initialize our "thread local storage" */ - ws.inbuf = ws.request = malloc(LIVESYNC_INCOMING_BUFFSIZE); - ws.outbuf = ws.reply = 0; + if (workstruct_init(&ws, LIVESYNC_INCOMING_BUFFSIZE) == -1) { + fprintf(stderr, "Warning: live sync could not get its buffers and will not run.\n"); + atomic_store(&thread_left, 1); + return NULL; + } + ws.request = ws.inbuf; while (g_opentracker_running) { uint32_t packet_type; @@ -233,7 +237,7 @@ static void *livesync_worker(void *args) { } } - free(ws.inbuf); + workstruct_deinit(&ws); atomic_store(&thread_left, 1); return NULL; } diff --git a/src/ot_worker.c b/src/ot_worker.c index 432d272..ef50945 100644 --- a/src/ot_worker.c +++ b/src/ot_worker.c @@ -20,7 +20,7 @@ static size_t g_worker_count; static void *worker_trampoline(void *args) { struct ot_worker *me = args; - if (workstruct_init(&me->ws) == -1) + if (workstruct_init(&me->ws, G_INBUF_SIZE) == -1) fprintf(stderr, "Warning: a worker could not get its buffers and will not run.\n"); else { me->run(me); diff --git a/src/trackerlogic.c b/src/trackerlogic.c index 9565090..8e30a48 100644 --- a/src/trackerlogic.c +++ b/src/trackerlogic.c @@ -567,9 +567,11 @@ void trackerlogic_init(void) { stats_init(); } -int workstruct_init(struct ot_workstruct *ws) { +/* Live sync reads whole packets rather than requests and wants a larger + inbuf than the request path does, hence the size. */ +int workstruct_init(struct ot_workstruct *ws, size_t inbuf_size) { memset(ws, 0, sizeof(*ws)); - ws->inbuf = malloc(G_INBUF_SIZE); + ws->inbuf = malloc(inbuf_size); ws->outbuf = malloc(G_OUTBUF_SIZE); #ifdef _DEBUG_HTTPERROR ws->debugbuf = malloc(G_DEBUGBUF_SIZE); diff --git a/src/trackerlogic.h b/src/trackerlogic.h index 7292d36..692b6af 100644 --- a/src/trackerlogic.h +++ b/src/trackerlogic.h @@ -242,7 +242,7 @@ struct ot_workstruct { /* Everything a thread that serves requests needs for itself. Set up and torn down in one place so that a new kind of worker cannot miss a piece. Returns -1 if the buffers could not be had. */ -int workstruct_init(struct ot_workstruct *ws); +int workstruct_init(struct ot_workstruct *ws, size_t inbuf_size); void workstruct_deinit(struct ot_workstruct *ws); /* diff --git a/tests/integration/test_livesync.py b/tests/integration/test_livesync.py index 8dd754b..1659a41 100644 --- a/tests/integration/test_livesync.py +++ b/tests/integration/test_livesync.py @@ -102,6 +102,17 @@ class LiveSyncIn(TrackerTestCase): self.tracker.livesync_send(packet(99, [(info_hash, peer4("10.1.2.11", 6881, FLAG_SEEDING))])) self.assertFalse(self.tracker.wait_until(lambda: self.counts(info_hash) is not None, timeout=1)) + def test_synced_peers_are_counted(self): + """The live sync worker keeps its own tally like every other worker, + and ?mode=syncs is the sum of them.""" + before = int(self.tracker.stats("syncs").split()[0]) + hashes = [self.hash_for("sync-count-%d" % i) for i in range(3)] + self.tracker.livesync_send(packet(SYNC_PEER4, [(h, peer4("10.1.2.%d" % (20 + i), 6881, FLAG_SEEDING)) + for i, h in enumerate(hashes)])) + self.assertTrue(self.tracker.wait_until( + lambda: int(self.tracker.stats("syncs").split()[0]) == before + 3), + "syncs says %s" % self.tracker.stats("syncs").split()[0]) + def test_a_packet_with_nothing_but_a_header_is_ignored(self): before = self.tracker.torrent_count() self.tracker.livesync_send(struct.pack("!I", 0xDEADBEEF) + struct.pack("!I", SYNC_PEER4)) diff --git a/tests/t_trackerlogic.c b/tests/t_trackerlogic.c index f64977f..1cf9141 100644 --- a/tests/t_trackerlogic.c +++ b/tests/t_trackerlogic.c @@ -224,7 +224,7 @@ void test_trackerlogic(void) { pthread_sigmask(SIG_BLOCK, &signal_mask, NULL); trackerlogic_init(); - CHECK_INT(workstruct_init(&ws), 0); + CHECK_INT(workstruct_init(&ws, G_INBUF_SIZE), 0); case_an_announce_makes_a_torrent(); case_the_same_peer_twice_is_one_peer(); -- cgit v1.3