From 71d7926328282a9d8d5f0238ff65d8e7350c77ba Mon Sep 17 00:00:00 2001 From: Joel Teply Date: Sat, 21 Nov 2026 17:48:49 -0600 Subject: [PATCH 1/6] Retry Windows persona block publication while a transient reader holds the file --- src/llama-persona-file.cpp | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/llama-persona-file.cpp b/src/llama-persona-file.cpp index da03d9950e82..623987382fe1 100644 --- a/src/llama-persona-file.cpp +++ b/src/llama-persona-file.cpp @@ -22,6 +22,7 @@ # endif # include #endif +#include #include #include @@ -85,7 +86,23 @@ struct reader { // refuses one (Codex on #155, UCRT on BigMama: -1 with the old body intact), so a second save to her fixed path failed. bool replace_file(const std::string & tmp, const char * path) { #ifdef _WIN32 - return MoveFileExA(tmp.c_str(), path, MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH) != 0; + // A short-lived reader without FILE_SHARE_DELETE can hold the complete temporary + // file across fclose. Retry only the atomic rename, never the block write. + constexpr ULONGLONG sharing_deadline_ms = 2000; + const auto deadline = GetTickCount64() + sharing_deadline_ms; + DWORD delay_ms = 10; + for (;;) { + if (MoveFileExA(tmp.c_str(), path, MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH)) { + return true; + } + const auto error = GetLastError(); + const auto now = GetTickCount64(); + if (error != ERROR_SHARING_VIOLATION || now >= deadline) { + return false; + } + Sleep(static_cast(std::min(delay_ms, deadline - now))); + delay_ms = std::min(2 * delay_ms, 100); + } #else return std::rename(tmp.c_str(), path) == 0; #endif From 601b7b5f38ba8c6b8b7782da056148ba882daf44 Mon Sep 17 00:00:00 2001 From: Joel Teply Date: Mon, 23 Nov 2026 15:52:33 -0600 Subject: [PATCH 2/6] Pin WebGPU CI to the Emscripten release verified with the Dawn package --- .github/workflows/build-wasm.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build-wasm.yml b/.github/workflows/build-wasm.yml index 2e4680f385be..6fd947898512 100644 --- a/.github/workflows/build-wasm.yml +++ b/.github/workflows/build-wasm.yml @@ -64,8 +64,9 @@ jobs: run: | git clone https://github.com/emscripten-core/emsdk.git cd emsdk - ./emsdk install latest - ./emsdk activate latest + EMSDK_VERSION=7.0.0 + ./emsdk install "$EMSDK_VERSION" + ./emsdk activate "$EMSDK_VERSION" - name: Fetch emdawnwebgpu run: | From 278a907ed2d503948a36d9cbfd13784ef5e0bc59 Mon Sep 17 00:00:00 2001 From: Joel Teply Date: Mon, 23 Nov 2026 17:03:54 -0600 Subject: [PATCH 3/6] Return the failed checkpoint stage and native error before cleanup --- include/llama.h | 12 ++++++++- src/llama-persona-file.cpp | 49 ++++++++++++++++++++++++----------- tests/test-persona-blocks.cpp | 4 +-- 3 files changed, 47 insertions(+), 18 deletions(-) diff --git a/include/llama.h b/include/llama.h index 715483cb588c..f1ff2228ddca 100644 --- a/include/llama.h +++ b/include/llama.h @@ -973,7 +973,17 @@ extern "C" { // `.tmp` and a rename. read_file fills a persona made by llama_persona_init on a context of the same memory // layout, for the owner and epoch her file names; file_mismatch (another owner, epoch, layout or size) and io_error // leave her as she was. Bind her afterwards as after a save. - LLAMA_API int32_t llama_persona_write_file(const struct llama_persona * persona, const char * path); + enum llama_persona_file_stage { + LLAMA_PERSONA_FILE_NONE, LLAMA_PERSONA_FILE_OPEN, LLAMA_PERSONA_FILE_WRITE, + LLAMA_PERSONA_FILE_FLUSH, LLAMA_PERSONA_FILE_CLOSE, LLAMA_PERSONA_FILE_RENAME, + }; + struct llama_persona_write_result { + int32_t status; + enum llama_persona_file_stage stage; + // errno, except for RENAME on Windows where this is GetLastError(). + uint32_t os_error; + }; + LLAMA_API struct llama_persona_write_result llama_persona_write_file(const struct llama_persona * persona, const char * path); LLAMA_API int32_t llama_persona_read_file(struct llama_persona * persona, const char * path); LLAMA_API const char * llama_persona_status_name(int32_t status); diff --git a/src/llama-persona-file.cpp b/src/llama-persona-file.cpp index 623987382fe1..ccc0b7e0e3f6 100644 --- a/src/llama-persona-file.cpp +++ b/src/llama-persona-file.cpp @@ -12,6 +12,7 @@ #include "llama.h" #include "llama-persona.h" +#include #include #ifdef _WIN32 # ifndef WIN32_LEAN_AND_MEAN @@ -33,11 +34,16 @@ constexpr uint32_t VERSION = 1; struct writer { std::FILE * f; - bool ok = true; - template void put(const T & v) { ok = ok && std::fwrite(&v, sizeof(T), 1, f) == 1; } + int error = 0; + void write(const void * data, size_t size) { + if (!error && size && std::fwrite(data, 1, size, f) != size) { + error = errno; + } + } + template void put(const T & v) { write(&v, sizeof(v)); } void bytes(const std::vector & b) { put(b.size()); - ok = ok && (b.empty() || std::fwrite(b.data(), 1, b.size(), f) == b.size()); + write(b.data(), b.size()); } void rows(const std::vector> & layers) { put((uint32_t) layers.size()); @@ -84,7 +90,7 @@ struct reader { // Replace `path` with the finished `tmp` in one step. POSIX rename replaces an existing destination; the Windows CRT's // refuses one (Codex on #155, UCRT on BigMama: -1 with the old body intact), so a second save to her fixed path failed. -bool replace_file(const std::string & tmp, const char * path) { +uint32_t replace_file(const std::string & tmp, const char * path) { #ifdef _WIN32 // A short-lived reader without FILE_SHARE_DELETE can hold the complete temporary // file across fclose. Retry only the atomic rename, never the block write. @@ -93,18 +99,18 @@ bool replace_file(const std::string & tmp, const char * path) { DWORD delay_ms = 10; for (;;) { if (MoveFileExA(tmp.c_str(), path, MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH)) { - return true; + return 0; } const auto error = GetLastError(); const auto now = GetTickCount64(); if (error != ERROR_SHARING_VIOLATION || now >= deadline) { - return false; + return error; } Sleep(static_cast(std::min(delay_ms, deadline - now))); delay_ms = std::min(2 * delay_ms, 100); } #else - return std::rename(tmp.c_str(), path) == 0; + return std::rename(tmp.c_str(), path) == 0 ? 0 : errno; #endif } @@ -115,15 +121,15 @@ struct closes { } // namespace -int32_t llama_persona_write_file(const llama_persona * persona, const char * path) { +llama_persona_write_result llama_persona_write_file(const llama_persona * persona, const char * path) { const auto & b = persona->blocks; if (!b.owned) { - return (int32_t) llama_persona_move::file_mismatch; // never saved: these blocks hold nothing of hers to write + return {(int32_t) llama_persona_move::file_mismatch, LLAMA_PERSONA_FILE_NONE, 0}; // never saved: these blocks hold nothing of hers to write } const std::string tmp = std::string(path) + ".tmp"; std::FILE * f = std::fopen(tmp.c_str(), "wb"); if (f == nullptr) { - return (int32_t) llama_persona_move::io_error; + return {(int32_t) llama_persona_move::io_error, LLAMA_PERSONA_FILE_OPEN, (uint32_t) errno}; } writer w { f }; w.put(MAGIC); @@ -161,13 +167,26 @@ int32_t llama_persona_write_file(const llama_persona * persona, const char * pat w.put(b.rs.rs_idx); w.rows(b.rs.r); w.rows(b.rs.s); - const bool flushed = w.ok && std::fflush(f) == 0; - const bool closed = std::fclose(f) == 0; - if (!flushed || !closed || !replace_file(tmp, path)) { + llama_persona_write_result result {(int32_t) llama_persona_move::done, LLAMA_PERSONA_FILE_NONE, 0}; + const auto io_error = (int32_t) llama_persona_move::io_error; + if (w.error) { + result = {io_error, LLAMA_PERSONA_FILE_WRITE, (uint32_t) w.error}; + } else if (std::fflush(f) != 0) { + result = {io_error, LLAMA_PERSONA_FILE_FLUSH, (uint32_t) errno}; + } + if (std::fclose(f) != 0 && result.status == 0) { + result = {io_error, LLAMA_PERSONA_FILE_CLOSE, (uint32_t) errno}; + } + if (result.status == 0) { + const uint32_t error = replace_file(tmp, path); + if (error) { + result = {io_error, LLAMA_PERSONA_FILE_RENAME, error}; + } + } + if (result.status != 0) { std::remove(tmp.c_str()); - return (int32_t) llama_persona_move::io_error; } - return (int32_t) llama_persona_move::done; + return result; } int32_t llama_persona_read_file(llama_persona * persona, const char * path) { diff --git a/tests/test-persona-blocks.cpp b/tests/test-persona-blocks.cpp index 84b9cd4f2fa9..6235b101f1d6 100644 --- a/tests/test-persona-blocks.cpp +++ b/tests/test-persona-blocks.cpp @@ -490,9 +490,9 @@ int main(int argc, char ** argv) { llama_memory_clear(llama_get_memory(f.ctx), true); f.prompt(0, 1); require_move(st(llama_persona_save(f.ctx, 0, saved)), llama_persona_move::done, "a saved"); - require_move(st(llama_persona_write_file(saved, path.c_str())), llama_persona_move::done, "a written to her file"); + require_move(st(llama_persona_write_file(saved, path.c_str()).status), llama_persona_move::done, "a written to her file"); // a second save to her fixed path replaces the first (Windows CRT rename refuses an existing destination) - require_move(st(llama_persona_write_file(saved, path.c_str())), llama_persona_move::done, "a written again over her file"); + require_move(st(llama_persona_write_file(saved, path.c_str()).status), llama_persona_move::done, "a written again over her file"); llama_persona_free(saved); llama_persona * other = llama_persona_init(f.ctx, 0xB, 1); From 4fa41ed0eed3e03c37cbc52d10ca4a23a0b489a6 Mon Sep 17 00:00:00 2001 From: Joel Teply Date: Mon, 23 Nov 2026 18:29:51 -0600 Subject: [PATCH 4/6] Give the checkpoint status API its own C symbol and cover Windows sharing --- include/llama.h | 2 +- src/llama-persona-file.cpp | 2 +- tests/test-persona-blocks.cpp | 27 +++++++++++++++++++++++++-- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/include/llama.h b/include/llama.h index f1ff2228ddca..b8a28894f780 100644 --- a/include/llama.h +++ b/include/llama.h @@ -983,7 +983,7 @@ extern "C" { // errno, except for RENAME on Windows where this is GetLastError(). uint32_t os_error; }; - LLAMA_API struct llama_persona_write_result llama_persona_write_file(const struct llama_persona * persona, const char * path); + LLAMA_API struct llama_persona_write_result llama_persona_write_checkpoint(const struct llama_persona * persona, const char * path); LLAMA_API int32_t llama_persona_read_file(struct llama_persona * persona, const char * path); LLAMA_API const char * llama_persona_status_name(int32_t status); diff --git a/src/llama-persona-file.cpp b/src/llama-persona-file.cpp index ccc0b7e0e3f6..4a66a32a68e8 100644 --- a/src/llama-persona-file.cpp +++ b/src/llama-persona-file.cpp @@ -121,7 +121,7 @@ struct closes { } // namespace -llama_persona_write_result llama_persona_write_file(const llama_persona * persona, const char * path) { +llama_persona_write_result llama_persona_write_checkpoint(const llama_persona * persona, const char * path) { const auto & b = persona->blocks; if (!b.owned) { return {(int32_t) llama_persona_move::file_mismatch, LLAMA_PERSONA_FILE_NONE, 0}; // never saved: these blocks hold nothing of hers to write diff --git a/tests/test-persona-blocks.cpp b/tests/test-persona-blocks.cpp index 6235b101f1d6..4bfa25a9dcdb 100644 --- a/tests/test-persona-blocks.cpp +++ b/tests/test-persona-blocks.cpp @@ -16,6 +16,12 @@ #include "../src/llama-context.h" +#ifdef _WIN32 +# ifndef NOMINMAX +# define NOMINMAX +# endif +# include +#endif #include #include #include @@ -490,9 +496,26 @@ int main(int argc, char ** argv) { llama_memory_clear(llama_get_memory(f.ctx), true); f.prompt(0, 1); require_move(st(llama_persona_save(f.ctx, 0, saved)), llama_persona_move::done, "a saved"); - require_move(st(llama_persona_write_file(saved, path.c_str()).status), llama_persona_move::done, "a written to her file"); + require_move(st(llama_persona_write_checkpoint(saved, path.c_str()).status), llama_persona_move::done, "a written to her file"); // a second save to her fixed path replaces the first (Windows CRT rename refuses an existing destination) - require_move(st(llama_persona_write_file(saved, path.c_str()).status), llama_persona_move::done, "a written again over her file"); + require_move(st(llama_persona_write_checkpoint(saved, path.c_str()).status), llama_persona_move::done, "a written again over her file"); + // The file already exists, so opening a temporary beneath it must fail. + const auto open_error = llama_persona_write_checkpoint(saved, (path + "/child").c_str()); + require_move(st(open_error.status), llama_persona_move::io_error, "open failure is an IO refusal"); + require(open_error.stage == LLAMA_PERSONA_FILE_OPEN && open_error.os_error != 0, "open refusal carries errno"); +#ifdef _WIN32 + // Keep a real reader on the temporary across the entire retry budget. + const std::string tmp = path + ".tmp"; + HANDLE reader = CreateFileA(tmp.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, + nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); + require(reader != INVALID_HANDLE_VALUE, "temporary reader opened"); + const auto rename_error = llama_persona_write_checkpoint(saved, path.c_str()); + CloseHandle(reader); + std::remove(tmp.c_str()); + require_move(st(rename_error.status), llama_persona_move::io_error, "held temporary refuses publication"); + require(rename_error.stage == LLAMA_PERSONA_FILE_RENAME && rename_error.os_error == ERROR_SHARING_VIOLATION, + "rename refusal retains its Win32 cause across cleanup"); +#endif llama_persona_free(saved); llama_persona * other = llama_persona_init(f.ctx, 0xB, 1); From 38c837e9b59a5e1c2666f470936046678e6db703 Mon Sep 17 00:00:00 2001 From: Joel Teply Date: Mon, 23 Nov 2026 18:31:58 -0600 Subject: [PATCH 5/6] Exercise transient and persistent Windows checkpoint sharing holds --- tests/test-persona-blocks.cpp | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/tests/test-persona-blocks.cpp b/tests/test-persona-blocks.cpp index 4bfa25a9dcdb..57ce83ba0bd3 100644 --- a/tests/test-persona-blocks.cpp +++ b/tests/test-persona-blocks.cpp @@ -17,6 +17,8 @@ #include "../src/llama-context.h" #ifdef _WIN32 +# include "../src/llama-persona.h" +# include # ifndef NOMINMAX # define NOMINMAX # endif @@ -509,12 +511,27 @@ int main(int argc, char ** argv) { HANDLE reader = CreateFileA(tmp.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); require(reader != INVALID_HANDLE_VALUE, "temporary reader opened"); - const auto rename_error = llama_persona_write_checkpoint(saved, path.c_str()); + // Keep serialization out of the deadline measurement, independent of the model under test. + llama_persona checkpoint_fixture{}; + checkpoint_fixture.blocks.owned = true; + const auto start = GetTickCount64(); + const auto rename_error = llama_persona_write_checkpoint(&checkpoint_fixture, path.c_str()); + const auto elapsed = GetTickCount64() - start; CloseHandle(reader); std::remove(tmp.c_str()); require_move(st(rename_error.status), llama_persona_move::io_error, "held temporary refuses publication"); require(rename_error.stage == LLAMA_PERSONA_FILE_RENAME && rename_error.os_error == ERROR_SHARING_VIOLATION, "rename refusal retains its Win32 cause across cleanup"); + require(elapsed >= 2000 && elapsed < 5000, "persistent sharing conflict consumes the bounded retry budget"); + const std::string transient = path + ".transient"; + reader = CreateFileA((transient + ".tmp").c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, + nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); + require(reader != INVALID_HANDLE_VALUE, "transient temporary reader opened"); + std::thread release([reader] { Sleep(150); CloseHandle(reader); }); + const auto published = llama_persona_write_checkpoint(&checkpoint_fixture, transient.c_str()); + release.join(); + require_move(st(published.status), llama_persona_move::done, "publication retries until the transient reader releases"); + std::remove(transient.c_str()); #endif llama_persona_free(saved); From 292ea40615982398e0711d2cae931d19c5a6a4ac Mon Sep 17 00:00:00 2001 From: Joel Teply Date: Mon, 23 Nov 2026 18:40:45 -0600 Subject: [PATCH 6/6] Avoid Windows far macro in rollback test --- tests/test-persona-blocks.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test-persona-blocks.cpp b/tests/test-persona-blocks.cpp index 57ce83ba0bd3..19ed57736e92 100644 --- a/tests/test-persona-blocks.cpp +++ b/tests/test-persona-blocks.cpp @@ -268,9 +268,9 @@ int main(int argc, char ** argv) { require_move(ctx.persona_load(0, 1 - occupied, bad, A), llama_persona_move::layout_mismatch, "a layout mismatch"); require(f.pos_max(0) < 0, "a refused load writes nothing"); if (a.rs.has_state) { // hybrid: a rollback index past the snapshot groups is refused before any write - llama_persona_blocks far = a; - far.rs.rs_idx = 1u << 20; - require_move(ctx.persona_load(0, 1 - occupied, far, A), llama_persona_move::invalid_rollback, "a rollback out of bounds"); + llama_persona_blocks invalid_rollback = a; + invalid_rollback.rs.rs_idx = 1u << 20; + require_move(ctx.persona_load(0, 1 - occupied, invalid_rollback, A), llama_persona_move::invalid_rollback, "a rollback out of bounds"); require(f.pos_max(0) < 0, "a refused hybrid load writes neither half"); }