From 1b5b4fab27169e3a63b6da16281c171fdef6855c Mon Sep 17 00:00:00 2001 From: lotherk Date: Mon, 23 Nov 2026 09:33:58 +0000 Subject: [PATCH] provstate/providers: a refused write still closes its stream Both writers chained the close: `fflush() || fsync() || fclose()` in write_state() and `write_all() || fclose()` in save_dropin(). Short-circuit evaluation meant a failing first call skipped fclose(), so every refused write leaked one descriptor. A daemon that answers 429s for hours, or one on a full disk, reaches EMFILE - which reads like a network failure, not like a file one. Measured on the real code path with RLIMIT_FSIZE: provstate 8 refused writes: 7 -> 15 open fds (8 lost), now 7 -> 7 providers (drop-in padded past one stdio buffer, else the write stays in the buffer and only fclose sees the failure) 7 -> 15, now 7 -> 7 Both tests fail without the fix and pass with it; make check all green (21 suites), check-asan clean over 46 binaries. --- src/backend/providers.c | 26 +++++++++---- src/backend/provstate.c | 21 +++++++++-- tests/test_providers.c | 81 +++++++++++++++++++++++++++++++++++++++++ tests/test_provstate.c | 52 ++++++++++++++++++++++++++ 4 files changed, 170 insertions(+), 10 deletions(-) diff --git a/src/backend/providers.c b/src/backend/providers.c index 04ab489..18019cc 100644 --- a/src/backend/providers.c +++ b/src/backend/providers.c @@ -987,13 +987,25 @@ save_dropin(const char *dir, const char *id, const char *line, rc = -1; } else { (void)fchmod(fileno(f), 0600); - if (write_all(f, &out) != 0 || fclose(f) != 0) { - if (err != NULL) - sp_buf_printf(err, "cannot write %s", - tmp); - (void)unlink(tmp); - rc = -1; - } else if (rename(tmp, path) != 0) { + /* + * Both calls, always: `write_all(...) || fclose(f)` + * skips the close as soon as the write fails, and a + * full disk then leaks a descriptor per attempt. + */ + { + int bad = write_all(f, &out) != 0; + + if (fclose(f) != 0) + bad = 1; + if (bad) { + if (err != NULL) + sp_buf_printf(err, "cannot write %s", + tmp); + (void)unlink(tmp); + rc = -1; + } + } + if (rc == 0 && rename(tmp, path) != 0) { if (err != NULL) sp_buf_printf(err, "cannot replace %s: %s", path, strerror(errno)); diff --git a/src/backend/provstate.c b/src/backend/provstate.c index b0025f4..6146ad4 100644 --- a/src/backend/provstate.c +++ b/src/backend/provstate.c @@ -131,10 +131,25 @@ write_state(const char *base, const struct sp_prov_state *s) * replace the good file. fclose() reports it, the temp is dropped and * whatever the last successful write said stays: a torn json parses as * all zeroes, which turns "this key is out of quota" into "admit it". + * + * Each call is made and its result kept, never chained with ||: a + * short-circuit skips fclose() as soon as fflush() fails, and every + * refused write then leaks one descriptor. A daemon that sees 429s for + * an hour reaches EMFILE, which reads like a network failure. */ - if (fflush(fp) != 0 || fsync(fileno(fp)) != 0 || fclose(fp) != 0) { - (void)unlink(tmp); - return -1; + { + int bad = 0; + + if (fflush(fp) != 0) + bad = 1; + if (fsync(fileno(fp)) != 0) + bad = 1; + if (fclose(fp) != 0) + bad = 1; + if (bad) { + (void)unlink(tmp); + return -1; + } } if (rename(tmp,path) != 0) { (void)unlink(tmp); diff --git a/tests/test_providers.c b/tests/test_providers.c index 8a556c1..5df66cf 100644 --- a/tests/test_providers.c +++ b/tests/test_providers.c @@ -5,9 +5,12 @@ * .d/20-connect.conf. The line has to survive a restart, so the test * writes it, loads the config again and looks at the delegate it produced. */ +#include +#include #include #include #include +#include #include #include @@ -17,6 +20,23 @@ static int failures; +/* every descriptor this process has open: /proc/self/fd, minus the dir handle */ +static int +open_fds(void) +{ + DIR *d = opendir("/proc/self/fd"); + struct dirent *e; + int n = 0; + + if (d == NULL) + return -1; + while ((e = readdir(d)) != NULL) + if (e->d_name[0] != '.') + n++; + closedir(d); + return n; +} + #define CHECK(cond) do { \ if (!(cond)) { \ fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \ @@ -286,6 +306,67 @@ main(void) sp_buf_free(&merr); } + /* + * A refused write must still close its stream. Both writers used to + * chain the close: `write_all(...) || fclose(f)` and `fflush() || + * fsync() || fclose()`. A failing write - a full disk, an + * RLIMIT_FSIZE - then skipped the fclose and leaked one descriptor + * per attempt; a daemon that keeps failing reaches EMFILE, which + * reads like a network error. + * + * The drop-in is padded past one stdio buffer on purpose: a short + * line stays in the buffer, fwrite() reports success and the failure + * only shows up in fclose(), which touches neither branch. Only the + * drop-in of /connect is padded: /model writes a few lines. + */ + { + char dpad[900]; + FILE *pf; + struct sp_buf merr; + struct rlimit rl; + struct sigaction sa, oldsa; + int before, after, i; + + snprintf(dpad, sizeof(dpad), "%s/20-connect.conf", drop); + pf = fopen(dpad, "w"); + CHECK(pf != NULL); + if (pf != NULL) { + for (i = 0; i < 200; i++) + fprintf(pf, "# padding line %04d " + "........................................................" + "........\n", i); + CHECK(fclose(pf) == 0); + } + CHECK(getrlimit(RLIMIT_FSIZE, &rl) == 0); + memset(&sa, 0, sizeof(sa)); + sa.sa_handler = SIG_IGN; + sigemptyset(&sa.sa_mask); + CHECK(sigaction(SIGXFSZ, &sa, &oldsa) == 0); + { + struct rlimit lim = rl; + + lim.rlim_cur = 8; /* shorter than the drop-in */ + CHECK(setrlimit(RLIMIT_FSIZE, &lim) == 0); + } + sp_buf_init(&merr); + before = open_fds(); + for (i = 0; i < 16; i++) { + char id[32]; + + snprintf(id, sizeof(id), "p%d", i); + sp_buf_clear(&merr); + (void)sp_provider_add(&back, drop, id, + "https://opencode.ai/zen/go/v1/chat/completions", + "other-model", "OCGO_KEY", &merr); + } + after = open_fds(); + sp_buf_free(&merr); + CHECK(setrlimit(RLIMIT_FSIZE, &rl) == 0); + CHECK(sigaction(SIGXFSZ, &oldsa, NULL) == 0); + CHECK(before >= 0 && after == before); + CHECK(unlink(dpad) == 0); + } + /* what /connect must refuse before it becomes a line nobody can read */ CHECK(sp_provider_id_ok("ocgo") == 1); CHECK(sp_provider_id_ok("my-prov_2") == 1); diff --git a/tests/test_provstate.c b/tests/test_provstate.c index 70027b9..df1e1f4 100644 --- a/tests/test_provstate.c +++ b/tests/test_provstate.c @@ -5,6 +5,7 @@ * quota: one writes, the other reads, and the answer is the same. HOME points * at a fresh directory, so the test never touches the real ~/.splinter. */ +#include #include #include #include @@ -18,6 +19,23 @@ static int failures; +/* every descriptor this process has open: /proc/self/fd, minus the dir handle */ +static int +open_fds(void) +{ + DIR *d = opendir("/proc/self/fd"); + struct dirent *e; + int n = 0; + + if (d == NULL) + return -1; + while ((e = readdir(d)) != NULL) + if (e->d_name[0] != '.') + n++; + closedir(d); + return n; +} + #define CHECK(cond) do { \ if (!(cond)) { \ fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \ @@ -126,6 +144,40 @@ main(void) reason, sizeof(reason)) == 1); } + /* + * ---- and it does not leak the stream while failing ---- + * + * The failure check used to be `fflush() || fsync() || fclose()`: + * short-circuit evaluation meant a failing fflush() left fclose() + * unrun, so every refused write kept one descriptor open. In a daemon + * that answers 429s for hours that ends in EMFILE, which looks like a + * network failure. Count the descriptors across 64 refused writes. + */ + { + struct rlimit rl; + struct sigaction sa, oldsa; + int before, after, i; + + CHECK(getrlimit(RLIMIT_FSIZE, &rl) == 0); + memset(&sa, 0, sizeof(sa)); + sa.sa_handler = SIG_IGN; + sigemptyset(&sa.sa_mask); + CHECK(sigaction(SIGXFSZ, &sa, &oldsa) == 0); + { + struct rlimit lim = rl; + + lim.rlim_cur = 8; + CHECK(setrlimit(RLIMIT_FSIZE, &lim) == 0); + } + before = open_fds(); + for (i = 0; i < 64; i++) + sp_provstate_account("leak", "k", 429, 1, 1, 3600); + after = open_fds(); + CHECK(setrlimit(RLIMIT_FSIZE, &rl) == 0); + CHECK(sigaction(SIGXFSZ, &oldsa, NULL) == 0); + CHECK(before >= 0 && after == before); + } + snprintf(path, sizeof(path), "rm -rf '%s'", dir); (void)system(path);