store: a write that cannot finish no longer replaces the good file

jobrec_write(), write_sidecar() and provstate's write_state() wrote into a
temp, ignored the result and renamed it over the live file. A full disk (or
EIO) then left a torn json where a job record or a key's quota state used to
be. For provstate that is the dangerous direction: a truncated state parses as
all zeroes, so "this key is out of quota until 19:00" became "admit it" and the
harness hammered a spent account.

All three now fflush+fsync+fclose, unlink the temp when any step fails, and
only then rename. Test proof, both tests fail on the old code:

  test_provstate.c  RLIMIT_FSIZE=8, write_state fails -> last_status still 429,
                    preflight still waits
  test_jobs.c       RLIMIT_FSIZE=8, delegate_retry cannot write -> the record on
                    disk is still a whole json, no .tmp left behind

make check: all tests passed. tests/check_asan.sh: all 45 binaries clean.
This commit is contained in:
lotherk committed 2026-11-23 06:33:12 +00:00
1 parent b1acf1629b
commit 4cdaf2680b
4 files changed
+152 -7

No files matched your search

+35 -5
View File
@@ -961,9 +961,23 @@ write_sidecar(const char *base, const char *ext, const struct sp_buf *b)
snprintf(tmp, sizeof(tmp), "%s.tmp", path);
fp = fopen(tmp, "w");
if (!fp) return;
fwrite(b->p, 1, b->len, fp);
fclose(fp);
rename(tmp, path);
/*
* The job record is the only copy of a task, a result or the error that
* explains a failed one. A write that cannot finish must not be renamed
* over the good file: unlink the temp and keep what was there.
*/
{
int bad = fwrite(b->p, 1, b->len, fp) != b->len;
if (fflush(fp) != 0 || fsync(fileno(fp)) != 0)
bad = 1;
if (fclose(fp) != 0)
bad = 1;
if (bad)
(void)unlink(tmp);
else
(void)rename(tmp, path);
}
}
static void
@@ -1008,8 +1022,24 @@ jobrec_write(const struct del_ctx *c, struct jobrec *j)
sp_buf_printf(&b, " \"has_result\":%s,\n \"has_error\":%s\n", (j->result.len>0?"true":"false"), (j->err.len>0?"true":"false"));
sp_buf_puts(&b, "}\n");
fp = fopen(tmp, "w");
if (fp) { fputs(b.p?b.p:"{}", fp); fclose(fp); rename(tmp, path); }
else unlink(tmp);
/*
* The record says which state a job is in; a torn one loses the job and
* its remote id, so an unfinished write is dropped instead of renamed.
* The writes are all attempted (no short-circuit) so the stream is
* always closed.
*/
if (fp != NULL) {
int bad = fputs(b.p != NULL ? b.p : "{}", fp) < 0;
if (fflush(fp) != 0 || fsync(fileno(fp)) != 0)
bad = 1;
if (fclose(fp) != 0)
bad = 1;
if (bad)
(void)unlink(tmp);
else if (rename(tmp, path) != 0)
(void)unlink(tmp);
}
sp_buf_free(&b);
if (j->task.len > 0) write_sidecar(base, "task", &j->task);
if (j->result.len > 0) write_sidecar(base, "result", &j->result);
+15 -2
View File
@@ -126,8 +126,21 @@ write_state(const char *base, const struct sp_prov_state *s)
fprintf(fp, "{\n \"quota_ready_at\":%ld,\n \"backoff_until\":%ld,\n \"req_count\":%u,\n \"tok_in\":%lu,\n \"tok_out\":%lu,\n \"last_status\":%ld,\n \"last_update\":%ld,\n \"last_429_at\":%ld,\n \"note\":\"%s\"\n}\n",
s->quota_ready_at, s->backoff_until, s->req_count, s->tok_in, s->tok_out,
s->last_status, s->last_update, s->last_429_at, s->note);
fclose(fp);
return rename(tmp,path)==0 ? 0 : -1;
/*
* A write that cannot finish - a full disk, RLIMIT_FSIZE, EIO - must not
* 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".
*/
if (fflush(fp) != 0 || fsync(fileno(fp)) != 0 || fclose(fp) != 0) {
(void)unlink(tmp);
return -1;
}
if (rename(tmp,path) != 0) {
(void)unlink(tmp);
return -1;
}
return 0;
}
int
+58
View File
@@ -5,9 +5,11 @@
* the store, retry resumes a job, and sp_delegate_tick is the one step the
* scheduler plugin takes when a job is due. The fake ssh is the far side.
*/
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <unistd.h>
@@ -198,6 +200,62 @@ main(void)
CHECK(out.p != NULL && strstr(out.p, "state=done") != NULL);
CHECK(out.p != NULL && strstr(out.p, "antwort vom job") != NULL);
/*
* ---- a job record that cannot be written keeps the old one ----
*
* jobrec_write() used to fputs() into a temp, ignore fclose() and rename
* the temp over the record. A full disk then replaced a job's state and
* remote id with a torn json, and reading it back gave a job with no
* delegate and no state. RLIMIT_FSIZE makes the short write certain.
*/
{
struct rlimit rl;
struct sigaction sa, oldsa;
char rec[700], args2[200];
char fbuf[2048];
size_t got;
snprintf(rec, sizeof(rec), "%s/jobs/%s.json", tdir, id);
CHECK(access(rec, R_OK) == 0);
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; /* far less than a record */
CHECK(setrlimit(RLIMIT_FSIZE, &lim) == 0);
}
/* this retry cannot write its record: the old one must survive */
snprintf(args2, sizeof(args2), "{\"id\":\"%s\",\"force\":\"1\"}", id);
sp_buf_clear(&out);
t = sp_registry_find(&r, "delegate_retry");
if (t != NULL)
(void)t->run(args2, &out, t->ctx);
CHECK(setrlimit(RLIMIT_FSIZE, &rl) == 0);
CHECK(sigaction(SIGXFSZ, &oldsa, NULL) == 0);
/* the record on disk is still a whole one, not a truncated line */
f = fopen(rec, "r");
CHECK(f != NULL);
got = 0;
if (f != NULL) {
got = fread(fbuf, 1, sizeof(fbuf) - 1, f);
fclose(f);
}
fbuf[got] = '\0';
CHECK(got > 8);
CHECK(fbuf[0] == '{');
CHECK(strstr(fbuf, "\"id\"") != NULL);
CHECK(strchr(fbuf, '}') != NULL);
/* no debris beside it */
snprintf(rec, sizeof(rec), "%s/jobs/%s.json.tmp", tdir, id);
CHECK(access(rec, F_OK) != 0);
}
/* the delegate host is done; the scheduler gets its own registry */
sp_host_free(host);
sp_registry_free(&r);
+44
View File
@@ -5,9 +5,11 @@
* 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 <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>
@@ -82,6 +84,48 @@ main(void)
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);
}
snprintf(path, sizeof(path), "rm -rf '%s'", dir);
(void)system(path);