Write the flight dump and the memory map through a temporary file and a rename.

The window's close ends the process under the detached threads, so a dump file the flight recorder's thread was
writing stayed cut short under its own name. Its three files are now written as .part and renamed when complete
(runtime/part_file.h, with a check that kills a writer in the middle), and the allocator's map goes through
plat_file_write_cached. The shader identities and the other cache files already went through an atomic write.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-12 22:14:32 -05:00
parent cc0e2c4bf4
commit c5cb67645e
6 changed files with 237 additions and 10 deletions

100
tests/part_file_check.c Normal file
View File

@@ -0,0 +1,100 @@
// runtime/part_file.h: a file written piece by piece gets its name only when it is complete. Built with the platform
// layer and run by tests/part_file_check.py.
//
// part_file_check <folder> the checks below, in <folder>
// part_file_check <folder> hang writes half of <folder>/killed.tsv, prints "writing" and sleeps: the Python side
// kills it there, as a closed game ends the thread that writes a dump
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "part_file.h"
static int fails;
#define CHECK(c, ...) do { if (!(c)) { fails++; printf("FAIL line %d: ", __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0)
void rt_log(const char *fmt, ...) { // the platform layer's lines
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fputc('\n', stderr);
}
// The text of the file at path ("" when it holds none), or NULL when there is no such file.
static char *text_of(const char *path) {
if (!plat_file_exists(path)) return NULL;
uint64_t size = 0;
char *t = (char *)plat_file_read(path, &size);
return t ? t : calloc(1, 1);
}
static int holds(const char *path, const char *want) {
char *t = text_of(path);
int same = t && !strcmp(t, want);
free(t);
return same;
}
int main(int argc, char **argv) {
if (argc < 2) return 2;
char path[PLAT_PATH_MAX], part[PLAT_PATH_MAX + 64];
if (argc > 2 && !strcmp(argv[2], "hang")) {
snprintf(path, sizeof path, "%s/killed.tsv", argv[1]);
FILE *f = part_file_open(path, "wb");
if (!f) return 3;
fputs("the first half of the new file\n", f);
fflush(f);
puts("writing");
fflush(stdout);
for (;;) plat_sleep_ms(1000);
}
// a new file: nothing under its name while it is written, the whole file after
snprintf(path, sizeof path, "%s/new.tsv", argv[1]);
CHECK(part_file_name(path, part, sizeof part), "the part name fits");
FILE *f = part_file_open(path, "wb");
CHECK(f != NULL, "the part file opens");
if (!f) return 1;
fputs("line 1\n", f);
fflush(f);
CHECK(!plat_file_exists(path), "no file under the name while it is written");
CHECK(holds(part, "line 1\n"), "what was written so far is in the part file");
fputs("line 2\n", f);
CHECK(part_file_close(f, path, 1) == 1, "the complete file gets its name");
CHECK(holds(path, "line 1\nline 2\n"), "the file holds everything written");
CHECK(!plat_file_exists(part), "no part file stays");
// over an older file: the old one stays whole until the new one is complete
f = part_file_open(path, "wb");
CHECK(f != NULL, "the part file opens beside the older file");
if (!f) return 1;
fputs("the new text\n", f);
fflush(f);
CHECK(holds(path, "line 1\nline 2\n"), "the older file is untouched while the new one is written");
CHECK(part_file_close(f, path, 1) == 1, "the new file replaces the older one");
CHECK(holds(path, "the new text\n"), "the name holds the new file");
// given up (the writer met an error): the older file stays, the part goes
f = part_file_open(path, "wb");
if (!f) return 1;
fputs("half of a third", f);
CHECK(part_file_close(f, path, 0) == 0, "a file given up is not put in place");
CHECK(holds(path, "the new text\n"), "the older file stays after a write that was given up");
CHECK(!plat_file_exists(part), "the part of a write that was given up is deleted");
// a folder that is not there: nothing opens, and closing nothing is no success
snprintf(path, sizeof path, "%s/no such folder/x.tsv", argv[1]);
f = part_file_open(path, "wb");
CHECK(f == NULL, "no part file in a folder that does not exist");
CHECK(part_file_close(f, path, 1) == 0, "closing a file that never opened reports failure");
// a name that does not fit the path buffers
static char longp[PLAT_PATH_MAX + 200];
memset(longp, 'a', sizeof longp - 1);
CHECK(part_file_open(longp, "wb") == NULL, "a path too long for its part name is refused");
printf(fails ? "part_file_check: %d FAILED\n" : "part_file_check: all checks passed\n", fails);
return fails != 0;
}

76
tests/part_file_check.py Normal file
View File

@@ -0,0 +1,76 @@
"""Build and run tests/part_file_check.c: runtime/part_file.h, which the runtime's detached threads write their files
through (the flight recorder's dump). A file gets its name only when it is complete, so a game that closes under the
writing thread leaves the older file or none, never one cut short.
The C side checks the helper itself. This side then kills a process in the middle of a write, as the window's close
ends the dump thread, and looks at what it left: the older file whole under its name, the half in the .part file.
No game files, no GPU.
Usage: part_file_check.py
"""
import os
import shutil
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
MSYS_BIN = Path(r'C:\msys64\ucrt64\bin')
def compiler():
if os.environ.get('CC'):
return os.environ['CC']
if os.name == 'nt' and (MSYS_BIN / 'clang.exe').exists():
return str(MSYS_BIN / 'clang.exe')
return shutil.which('clang') or 'clang'
def main():
work = ROOT / 'build' / 'test' / 'part_file'
shutil.rmtree(work / 'files', ignore_errors=True)
(work / 'files').mkdir(parents=True)
win = os.name == 'nt'
exe = work / ('part_file_check.exe' if win else 'part_file_check')
env = dict(os.environ)
if win and MSYS_BIN.exists():
env['PATH'] = str(MSYS_BIN) + os.pathsep + env.get('PATH', '')
rt = ROOT / 'runtime'
plat = [rt / 'platform' / ('win32.c' if win else 'linux.c'), rt / 'platform' / 'common.c']
libs = ['-lbcrypt', '-lws2_32', '-lpsapi', '-lwinmm', '-ldbghelp', '-lshell32', '-lole32', '-luser32', '-ladvapi32'] if win \
else ['-lpthread']
subprocess.check_call([compiler(), '-O1', '-g', '-Wall', '-Wno-unused-function', '-fno-strict-aliasing',
'-march=x86-64-v3', '-I', str(rt),
str(ROOT / 'tests' / 'part_file_check.c'), *map(str, plat), '-o', str(exe), '-lm', *libs],
env=env)
files = work / 'files'
code = subprocess.run([str(exe), str(files)], env=env, timeout=120).returncode
if code:
return code
# a writer killed in the middle: the file of the dump before it is whole, the half is named as a part
killed = files / 'killed.tsv'
killed.write_text('the whole older file\n', encoding='utf-8')
p = subprocess.Popen([str(exe), str(files), 'hang'], env=env, stdout=subprocess.PIPE, text=True)
try:
said = p.stdout.readline().strip()
finally:
p.kill()
p.wait()
p.stdout.close()
fails = []
if said != 'writing':
fails.append(f'the writer did not start ({said!r})')
if killed.read_text(encoding='utf-8') != 'the whole older file\n':
fails.append('the older file changed although the new one was never complete')
part = killed.with_name('killed.tsv.part')
if not part.is_file() or part.read_text(encoding='utf-8') != 'the first half of the new file\n':
fails.append('the half that was written is not in killed.tsv.part')
for f in fails:
print('FAIL:', f)
print('part_file_check: the killed writer left %s' % ('a wrong file' if fails else 'the older file whole'))
return 1 if fails else 0
if __name__ == '__main__':
sys.exit(main())