Files
splinter/tests/test_provstate.c
lotherk 1b5b4fab27 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.
2026-11-23 09:33:58 +00:00

188 lines
5.7 KiB
C

/*
* splinter -- shared provider/key state.
*
* The state is what makes two splinter processes agree that a key is out of
* 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 <dirent.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include "provstate.h"
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); \
failures++; \
} \
} while (0)
int
main(void)
{
char dir[] = "/tmp/splinter-prov-XXXXXX";
char path[700], fp1[32], fp2[32];
struct sp_prov_state st;
struct stat sb;
long now, ready = 0;
char reason[128];
CHECK(mkdtemp(dir) != NULL);
CHECK(setenv("HOME", dir, 1) == 0);
now = (long)time(NULL);
/* no state yet: admit */
CHECK(sp_provstate_load("openai", "key-a", &st) != 0);
CHECK(sp_provstate_preflight("openai", "key-a", now, &ready, reason,
sizeof(reason)) == 0);
CHECK(ready == 0);
/* a 429 with Retry-After becomes a quota wait both processes can see */
sp_provstate_account("openai", "key-a", 429, 10, 20, 3600);
CHECK(sp_provstate_load("openai", "key-a", &st) == 0);
CHECK(st.last_status == 429);
CHECK(st.req_count == 1);
CHECK(st.tok_in == 10 && st.tok_out == 20);
CHECK(st.quota_ready_at > now && st.quota_ready_at <= now + 3601);
CHECK(sp_provstate_preflight("openai", "key-a", now, &ready, reason,
sizeof(reason)) == 1);
CHECK(ready == st.quota_ready_at);
CHECK(strstr(reason, "quota") != NULL);
/* the state file is private */
snprintf(path, sizeof(path), "%s/.splinter/providers/openai", dir);
CHECK(stat(path, &sb) == 0);
CHECK((sb.st_mode & 077) == 0);
CHECK(sp_provstate_base("openai", "key-a", path, sizeof(path)) != NULL);
/* a 503 is a short backoff, on its own key */
sp_provstate_account("openai", "key-b", 503, 0, 0, 30);
CHECK(sp_provstate_load("openai", "key-b", &st) == 0);
CHECK(st.backoff_until > now);
CHECK(sp_provstate_preflight("openai", "key-b", now, &ready, reason,
sizeof(reason)) == 2);
CHECK(strstr(reason, "backoff") != NULL);
/* keys are separate: another key is still admitted */
CHECK(sp_provstate_preflight("openai", "key-c", now, &ready, reason,
sizeof(reason)) == 0);
/* the fingerprint is stable and does not contain the secret */
CHECK(sp_key_fingerprint("secret-one", fp1, sizeof(fp1)) != NULL);
CHECK(sp_key_fingerprint("secret-one", fp2, sizeof(fp2)) != NULL);
CHECK(strcmp(fp1, fp2) == 0);
CHECK(strstr(fp1, "secret") == NULL);
CHECK(sp_key_fingerprint("secret-two", fp2, sizeof(fp2)) != NULL);
CHECK(strcmp(fp1, fp2) != 0);
CHECK(sp_key_fingerprint("", fp2, sizeof(fp2)) == NULL);
/*
* ---- a write that cannot finish must not rewrite the state ----
*
* The file used to be written with fprintf() into a temp, fclose() was
* ignored and the temp renamed over the good file. A full disk then
* replaced "this key is out of quota until ready_at" with a torn json
* that parses as all zeroes - the preflight admitted a key the state
* says to wait for. RLIMIT_FSIZE makes the short write deterministic.
*/
{
struct rlimit rl;
struct sigaction sa, oldsa;
long short_ready = 0;
sp_provstate_account("short", "k", 429, 1, 1, 3600);
CHECK(sp_provstate_load("short", "k", &st) == 0);
CHECK(sp_provstate_preflight("short", "k", now, &short_ready,
reason, sizeof(reason)) == 1);
memset(&sa, 0, sizeof(sa));
sa.sa_handler = SIG_IGN;
sigemptyset(&sa.sa_mask);
CHECK(sigaction(SIGXFSZ, &sa, &oldsa) == 0);
CHECK(getrlimit(RLIMIT_FSIZE, &rl) == 0);
{
struct rlimit lim = rl;
lim.rlim_cur = 8; /* shorter than the json */
CHECK(setrlimit(RLIMIT_FSIZE, &lim) == 0);
}
/* this write cannot finish: the good state must survive */
sp_provstate_account("short", "k", 200, 1, 1, 0);
CHECK(setrlimit(RLIMIT_FSIZE, &rl) == 0);
CHECK(sigaction(SIGXFSZ, &oldsa, NULL) == 0);
CHECK(sp_provstate_load("short", "k", &st) == 0);
CHECK(st.last_status == 429);
short_ready = 0;
CHECK(sp_provstate_preflight("short", "k", now, &short_ready,
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);
if (failures == 0)
printf("test_provstate: ok\n");
return failures == 0 ? 0 : 1;
}