[code] [blog] [Repos] [GPS Tracker] [Music] [VNC] [Restart VNC] [E-Mail me]

browse files at this commit · zip

commit 5df915e612b2184ae4c342b000c87b5096cae9fa
Author: no-body-in-particular <admin@coredump.ws>
Date:   Wed Oct 14 22:04:21 2026 +0200

    Stop the worker leaking per connection, and cap its malloc arenas
    
    After five days the worker held 1.9 GB of address space. Most of that was
    cell.db (the next commit); the rest was found by five independent passes -
    reading the code, LSan and valgrind under thousands of replayed device
    sessions, and watching the live worker for forty minutes.
    
    copy_range() kept its 256 KB trim buffer in a __thread pointer. Trims run
    on the device's own connection thread when one of its files passes its cap,
    and nothing freed the buffer when that thread ended, so every connection
    that trimmed leaked the whole chunk. Over 6,000 replayed sessions idle RSS
    ended at 98.5 MB instead of 26 MB. Every caller holds trim_lock, so one
    buffer for the process is enough.
    
    do_geolocate_as() never freed its curl header list: about 80 bytes per
    positioning request.
    
    The other half gigabyte was glibc, not us. Each connection is a thread, and
    threads that contend for malloc get their own 64 MB arena, never returned:
    seven of them after five days, from moments when more connections
    overlapped than before. And glibc raises its mmap threshold to the size of
    any mapped block that is freed, so after the first 565 KB connection struct
    was freed every later one came out of an arena. Two arenas and a fixed
    128 KB threshold: a burst test went from 552 MB to 162 MB of VmSize.
    
    Also zero the multilaterate points in the JIMI LBS handler; valgrind found
    haversineDistance reading the never-set accuracy fields.
    
    Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---
 device_server/device/jimi_protocol.c |  2 +-
 device_server/logfiles.c             |  8 +++++++-
 device_server/main.c                 | 19 +++++++++++++++++++
 device_server/web_geolocate.c        |  3 ++-
 4 files changed, 29 insertions(+), 3 deletions(-)

diff --git a/device_server/device/jimi_protocol.c b/device_server/device/jimi_protocol.c
index 7f6709b..601cd4b 100644
--- a/device_server/device/jimi_protocol.c
+++ b/device_server/device/jimi_protocol.c
@@ -92,7 +92,7 @@ void process_lbs_info(connection * conn, information_package package) {
 
     lbs_information_package lbs_information;
     cell_tower tower[6];
-    multilaterate_point points[6];
+    multilaterate_point points[6] = {0};
     //test if we have a recent gps position - if not we add the lbs position
     memcpy(&lbs_information, package.information, sizeof(lbs_information_package));
     lbs_information.mcc = SWAP_UINT16(lbs_information.mcc);
diff --git a/device_server/logfiles.c b/device_server/logfiles.c
index ba2e105..99b0e5d 100644
--- a/device_server/logfiles.c
+++ b/device_server/logfiles.c
@@ -717,7 +717,13 @@ static pthread_mutex_t trim_lock = PTHREAD_MUTEX_INITIALIZER;
 #define TRIM_CHUNK (256 * 1024)
 
 static bool copy_range(int in, int out, off_t from, off_t to, size_t io_rate, struct timespec * t0, off_t * copied) {
-    static __thread char * buf = NULL;
+    /*
+     * One buffer for the process, not one per thread. Every caller holds trim_lock, so only
+     * one copy runs at a time. As __thread it was allocated by each connection thread that
+     * ever trimmed a file and never freed when the thread ended - 256 KB lost per such
+     * connection, outside any arena that could reuse it.
+     */
+    static char * buf = NULL;
 
     if (!buf && !(buf = malloc(TRIM_CHUNK))) {
         return false;
diff --git a/device_server/main.c b/device_server/main.c
index d8bf2a3..05c5084 100644
--- a/device_server/main.c
+++ b/device_server/main.c
@@ -16,6 +16,7 @@
 #include "util.h"
 #include <string.h>
 #include <errno.h>
+#include <malloc.h>
 
 /*
  * Crash report for SIGSEGV/SIGFPE/SIGBUS/SIGILL/SIGABRT. Only async-signal-safe calls:
@@ -162,7 +163,25 @@ void server_loop() {
     }
 }
 
+/*
+ * Every connection is its own thread, and glibc gives threads that contend for malloc their
+ * own arena - up to eight per core, each a 64 MB reservation that is never given back. A
+ * live worker gathered seven of them in five days (450 MB of VmSize) while serving a handful
+ * of devices. Two arenas is plenty for this load.
+ *
+ * And a fixed mmap threshold. glibc raises the threshold to the size of any mmapped block
+ * that is freed, so after the first connection ended its 565 KB struct - and every later one
+ * - came out of an arena instead of a private mapping, and what a burst of connections
+ * touched stayed resident in the arena afterwards. Pinned at 128 KB, a connection struct is
+ * mapped on accept and unmapped when it closes.
+ */
+static void tune_malloc(void) {
+    mallopt(M_ARENA_MAX, 2);
+    mallopt(M_MMAP_THRESHOLD, 128 * 1024);
+}
+
 int main(int argc, char * argv[]) {
+    tune_malloc();
     /*
      * Everything this daemon creates is owner read/write, group read: the group is the one it
      * shares with the web app, which only reads its output. The command queue - the one file
diff --git a/device_server/web_geolocate.c b/device_server/web_geolocate.c
index d87f1ff..4d149e8 100644
--- a/device_server/web_geolocate.c
+++ b/device_server/web_geolocate.c
@@ -87,8 +87,9 @@ static location_result do_geolocate_as( char * url, char * query, bool metered)
             fprintf(stderr, "curl_easy_perform() failed: %s\n",
                     curl_easy_strerror(res));
 
-        /* always cleanup */
+        /* always cleanup - the header list is ours, curl_easy_cleanup does not free it */
         curl_easy_cleanup(curl);
+        curl_slist_free_all(headers);
     }
 
     if (chunk.size) {