From 4783705e6b27eebfc6d0318cb8fc1aaf2b7f1172 Mon Sep 17 00:00:00 2001 From: lotherk Date: Mon, 23 Nov 2026 00:23:12 +0000 Subject: [PATCH] embed: a failed batch frees the vectors it built catch_up() allocated one vector per record and only commit() ever freed them: a remote encoder that refused the batch (no endpoint, a non-2xx, a wrong dimension count) leaked dims doubles per record on every retry, and the worker retried until it parked. valgrind on tests/test_embed before the fix: 512 bytes definitely lost in 4 blocks; after: 0 errors. `make check`: all tests passed. --- src/backend/embed.c | 21 +++++++++++++++++++++ tests/test_embed.c | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/src/backend/embed.c b/src/backend/embed.c index fa1a052..1d0f3c2 100644 --- a/src/backend/embed.c +++ b/src/backend/embed.c @@ -806,6 +806,23 @@ pends_free(struct pend *p, size_t n) } } +/* + * Free the vectors of a batch that did not hand them over: commit() nulls the + * ones it wrote, so everything still non-NULL here belongs to a batch that + * failed. Without this the retries of a failing remote encoder pile up until + * the worker parks. + */ +static void +vecs_free(double **vec, size_t n) +{ + size_t i; + + for (i = 0; i < n; i++) { + free(vec[i]); + vec[i] = NULL; + } +} + /* * Read one session record into p. 1 = read, 0 = end of file, 2 = a line that * holds nothing to embed (skipped), -1 = out of memory. 'pos' is the byte @@ -993,6 +1010,7 @@ catch_up(struct sp_embed_ctx *c) } if (i < n) { sp_buf_puts(&err, "out of memory"); + vecs_free(vec, n); rc = -1; break; } @@ -1008,6 +1026,7 @@ catch_up(struct sp_embed_ctx *c) texts[i] = p[i].text; if (encode_remote(c, texts, n, vec, &err) != 0) { free(texts); + vecs_free(vec, n); rc = -1; break; } @@ -1016,11 +1035,13 @@ catch_up(struct sp_embed_ctx *c) pthread_mutex_lock(&c->lock); if (c->stop) { pthread_mutex_unlock(&c->lock); + vecs_free(vec, n); rc = -1; break; } if (commit(c, p, vec, n) < 0) { pthread_mutex_unlock(&c->lock); + vecs_free(vec, n); rc = -1; break; } diff --git a/tests/test_embed.c b/tests/test_embed.c index d7715d9..3003c81 100644 --- a/tests/test_embed.c +++ b/tests/test_embed.c @@ -262,6 +262,42 @@ test_torn(void) sp_embed_free(c); } +/* + * A batch that never reaches the index must not leak the vectors it built. + * The remote encoder is the easy way to make one fail: with no endpoint it + * refuses before anything is written, and every retry rebuilt the whole batch. + */ +static void +test_failed_batch_frees_vectors(void) +{ + char tmpl[] = "/tmp/splinter-embf-XXXXXX"; + char *dir = mkdtemp(tmpl); + char err[256]; + const char *lines[] = { + "{\"role\":\"user\",\"text\":\"erste nachricht ueber den dienst\"}", + "{\"role\":\"assistant\",\"text\":\"zweite nachricht ueber die ports\"}" + }; + struct sp_embed_ctx *c; + struct sp_config conf; + + if (dir == NULL) + return; + sp_config_init(&conf); + conf.embed = SP_EMBED_REMOTE; + snprintf(conf.embed_model, sizeof(conf.embed_model), "m"); + conf.embed_dims = 16; + /* no embed_endpoint: the batch is refused, nothing is written */ + write_session(dir, "s4", lines, 2); + c = sp_embed_new(&conf); + CHECK(c != NULL); + if (c == NULL) + return; + sp_embed_bind(c, dir, "s4"); + CHECK(sp_embed_index_now(c, err, sizeof(err)) == -1); + fprintf(stderr, " refused batch: %s\n", err); + sp_embed_free(c); +} + /* * The end-to-end shape: a session with a lot of history, one compaction, and * the question that a compaction always makes unanswerable from the context. @@ -331,6 +367,8 @@ main(void) test_index(); fprintf(stderr, "== torn tail\n"); test_torn(); + fprintf(stderr, "== refused batch\n"); + test_failed_batch_frees_vectors(); fprintf(stderr, "== compaction recall\n"); test_compaction_recall(); if (failures != 0) {