fix(settings): a crash or a kill while a setting is written no longer leaves its file empty or cut short #976

Open
mooki wants to merge 1 commit from mooki/Longshot:fix/settings-all-or-nothing into main
Member

Type: fix · Risk: low · Tested on: Linux (Arch, RTX 4090: game-free checks and one windowless game start); Windows 11 (Intel Arc laptop, the kill tests, no game) · Needs: Windows, any GPU

Why

If Longshot stops in the middle of writing one of its small settings files (a crash, the process killed, a power cut), the file can be left empty or cut short, and the next start reads it as the player's settings. language.txt (the Game language row and the first-start template) and mouse_sens.txt are written with fopen "w", which empties the file before it writes it, and so is the note saves-moved.txt. The one-time copy of a game folder's longshot_options.ini into the launcher's user folder, and each file the Linux move of the saves copies between drives, are made at their final names. A stop in the middle of any of them leaves an empty file or a part of one. An empty longshot_options.ini in the user folder makes the next start choose a preset again, as on a first start; one cut short keeps the lines before the cut and takes the defaults for the rest, and that folder is shared by every build the launcher starts.

The issue tracker was searched for longshot_options.ini, language.txt, mouse_sens, saves-moved, hosnew, "settings reset", "settings lost", "settings corrupt" and "empty settings": no issue is about this, so there is nothing to link. The game's own save files are the work of another pull request, #607, and the menu's write of longshot_options.ini was fixed by #550; this one leaves both alone (see Overlaps).

How to reproduce

Seen on: game-free, on Linux (Arch-based, kernel 7.2.15, gcc 16.2.1, Python 3.12.16), and on a Windows 11 laptop (Core Ultra 9 185H, Intel Arc, its own clang): the control at the end of tests/test_settings_atomic.py writes a file the way the game did and ends the process at each step. The old ways were not run in the game; the build with this change was (see Testing).
Settings and switches: defaults.
Steps: run python -I -B tests/test_settings_atomic.py on this branch and read its control line. On main's source the same test fails: the helper is not there and no writer uses it.
Expected: after every kill the file is the old one or the new one, whole.
Actual: on main the test cannot run: host/hos_atomic_file.h is not there and no writer uses it (18 source checks fail). Its control, which writes the old ways, ends the process at 5 steps of an fopen "w" write and at 5 steps of a copy made at the final name: 3 of 5 and 3 of 5 left an empty or part file, on Linux and on the Windows laptop alike.

What changed

A setting is now written beside its file as NAME.hosnew, flushed to the disk and moved over the old file in one step, so a stop leaves the old file or the new one. One header, host/hos_atomic_file.h, does it for language.txt, mouse_sens.txt and saves-moved.txt, for the options file's one-time copy into the user folder and for the files the saves' move copies, and the start removes the stand-in a stop leaves. The game's own save files (host/nn_fs.c) and the menu's write of longshot_options.ini are not touched.

  • helper: host/hos_atomic_file.h (new) has hos_atomic_write, hos_atomic_copy_file and hos_atomic_remove_stale. On Windows the move is MoveFileExA with MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH; on Linux the posix layer's MoveFileExA is a plain rename, so the helper flushes the folders with fsync after it. Where no stand-in can be made a setting is written in place as before: a file that is a link (a file moved over it would replace the link) and a folder that takes no new file though the file itself can be written. A full disk leaves the old file as it was.
  • options copy: hos_options_carry in host/options_path.c copied the game folder's longshot_options.ini into the user folder with a plain CopyFileA to the final name, so a kill in it could leave a short file that the next start took for the player's settings. It uses hos_atomic_copy_file now: a kill leaves no file there or the whole one, and the next start copies again; the helper keeps the failing call's error for the log line.
  • start: recover_writes in host/hos_main.c runs before anything reads a setting and removes a stale NAME.hosnew beside the settings file options_path.c chose (the user folder's with --user, the file HOS_OPTIONS_FILE names, else the game folder's), beside language.txt in the game folder and beside mouse_sens.txt in the user folder. copy_tree (the Linux move of the saves to another drive) copies each file with hos_atomic_copy_file and saves_dir writes saves-moved.txt with hos_atomic_write.
  • writers: host/nn_applet.c (the first-start language.txt), set_game_language in gpu/vk/vk_menu_i18n.cpp and mouse_sens_save in host/nn_hid.c use hos_atomic_write.
  • docs: docs/updates-and-saves.md has a short section on what a .hosnew file is.
  • tests: tests/test_settings_atomic.py with tests/settings_atomic_test.c kills a child at every step of a write over a file, a first write, a copy over a file and a copy to a new name; tests/test_options_path.py kills the options copy at each of its steps and runs recover_writes over a stand-in beside each settings file; tests/test_linux_saves_move.py includes the helper its fixture of copy_tree now needs.

What the menu's write of longshot_options.ini already does since #550, and what this change leaves as it is: save() in gpu/vk/vk_menu.cpp writes longshot_options.ini.new beside the file (beside the target of a link), flushes it with fflush and fclose, and moves it over the file with rename (MoveFileExA with MOVEFILE_REPLACE_EXISTING on Windows). It does not flush the data to the disk before the move, does not ask Windows for a write-through move and does not flush the folder, by that change's own choice (a save comes with every arrow press in the menu), and a kill leaves longshot_options.ini.new beside the file until the next save overwrites it. This change does not duplicate any of that and adds nothing to it.

Not covered: a NAME.hosnew beside saves-moved.txt, or inside a saves folder that a killed move left half copied, is not removed at the start; it is harmless and can be deleted by hand.

Measurements

Nothing to measure: this fixes how small files are written, with no change to a frame rate, a switch, a menu row or an ini key. What was checked is under Testing.

Flags and settings

None. No switch, menu row or ini key is added or changed; the files keep their names, places and lines.

Testing

Tested: environment: Linux 7.2.15 (Arch-based), Intel Core i9-13900K, 93 GiB, gcc 16.2.1, Python 3.12.16; a Windows 11 laptop (Core Ultra 9 185H, Intel Arc); forge main 0407ae243, this PR 0e2228c96; 2026-11-15. The suites ran with a fresh home folder, core dumps off and three tests at a time. Every result below is for that head on that main, except where a line says otherwise.

  • the new tests fail without the change and pass with it. On main plus only the new tests: test_settings_atomic, test_options_path and test_linux_saves_move all fail (for the first, 18 source checks: host/hos_atomic_file.h is not there). With the header alone added: test_settings_atomic fails (17 checks), test_options_path fails (host/hos_main.c has no recover_writes()), test_linux_saves_move passes. On this branch all three pass (10.4 s, 0.2 s, 0.2 s).
  • four deliberate breaks of this branch, one at a time, are each caught: the options copy back to CopyFileA (test_options_path and test_settings_atomic fail), the options file's stand-in clean-up taken out of recover_writes (both fail), the write made in place (test_settings_atomic fails), mouse_sens_save back to fopen "w" (test_settings_atomic fails).
  • kill tests on Linux, as the tests print them: write over a file: 5 kill steps, each left the old file or the new one, whole; first write: 5 kill steps; copy over a file: 3 kill steps; copy to a new name: 3 kill steps; stale: a leftover settings.ini.hosnew is removed at the start; loop: 60 kills at random moments of repeated writes: every file whole, no stand-in after the start; in place: through a link, in a folder that takes no new file: written where the file is, the link kept; control (the old ways): 3 of 5 kill steps of fopen "w" and 3 of 5 of a copy at the final name left an empty or part file. test_options_path: copy_is_whole_or_not_there: 3 kill steps, each left no file or the whole one, start_removes_what_a_kill_left: the stand-ins of the user folder's settings, HOS_OPTIONS_FILE's, language.txt and mouse_sens.txt removed, failed_copy_says_why: the log says error 5, no file or stand-in left.
  • the same two tests natively on the Windows laptop, in two ways each: replayed (the executables cross-built on Linux, run there) and native (compiled by the laptop's own clang). All four runs exit 0 with the test's own PASS line: test_settings_atomic 12.4 s and 13.3 s, 7 of 7 report lines (the in place case is skipped on Windows, a link needs a privilege there), test_options_path 0.5 s and 0.9 s, 3 of 3 lines (failed_copy_says_why is skipped on Windows). The counts are the Linux ones: 5, 5, 3 and 3 kill steps, 60 kills at random moments, and the control's 3 of 5 and 3 of 5 torn files.
  • python -I -B tests/run_all.py: this PR 609 passed, 21 failed, 17 skipped (171.6 s); forge main 0407ae243, the same way, 608 passed, 21 failed, 17 skipped (162.1 s). The same 21 fail on both and none fails only here: test_appimage_player_build, test_crash_dialog, test_crt_options, test_env_on, test_fsr_uniform_ownership, test_lift_cleanup_error, test_linux_preflight, test_linux_update, test_make_game_cache, test_missing_nup, test_mods_size_table, test_nonuniform_image_arrays, test_options_budget_once, test_parallel_init_order, test_pipeline_tour, test_readback_late, test_replay_write_order, test_run_all_changed, test_vk_occlusion_queries, test_vk_pgo, test_walk_key. Their causes were not looked into here. The one more pass is test_settings_atomic.
  • Windows-target compile with llvm-mingw of this head's build plan (Release flags): the 206 renderer units (gpu/vk/vk_menu_i18n.cpp among them) and the 204 host units (the 56 C files of host/ among them, host/hos_main.c, host/options_path.c, host/nn_hid.c and host/nn_applet.c included) compiled, none failed, and the renderer library linked. No game executable was linked (there is no Windows lift here), so this is a compile of the host code, not a Windows run of the game.
  • Windows-target syntax check of the test fixtures (the project's fixture check, against main): test_settings_atomic passes for both its compiled parts (tests/settings_atomic_test.c and the header as C++). test_options_path does not compile in that check, and it does not on main's own copy of that test either: both fail with errors of the kind 'getenv' is deprecated [-Werror,-Wdeprecated-declarations], in host/hos_paths.h, host/options_path.c and the generated main.c, and the check's own note says main.c uses getenv; define _CRT_SECURE_NO_WARNINGS before the first include. This change adds getenv calls to that generated main.c (5 errors there against 1 on main) and no other kind of error. The same test compiled with the laptop's own clang and passed there (above). test_linux_saves_move has no compile that the check reaches. The check is syntax only: it is not a Windows run.
  • in the game, on Linux (RTX 4090, driver 615.78.08, no window): a build of this change (head 461e9a030, one rebase before the head above; the patch is the same, see the rebase note) made with nx2/make_game.py, its shaders prepared as a first start does, then started once with the control bridge on, with its game folder set up as an older install would leave it: language.txt removed and a language.txt.hosnew put beside its place (what a killed write leaves), and a longshot_options.ini with text_speed=2 in the game folder, the game started with --user. What the run showed: the game exited 0 on the bridge's quit, version v3.0.0-rc.4-69-g461e9a030 (source 89bd8149) in its log, 0 fatal, 0 crash, 0 device-lost lines, no driver lines. The stale language.txt.hosnew was gone after the start and language.txt was there whole (the first-start template, 507 bytes, en-US as its language line). The log has options: the settings in <game folder>/longshot_options.ini were copied to <user folder>/longshot_options.ini, where every build the launcher starts reads them now, and the bridge read Text speed as 2 (4x). Then option set game_language 1 (the menu's own setter): language.txt was replaced under a new inode (not rewritten in place), its language line went from en-US to en-GB, the row read back English (UK), and after the quit no .hosnew or .new file was left in the game folder or the user folder. The Insert menu's picture taken at the end of the run (1280x720, looked at, not attached here) shows the Gameplay tab with Game language English (UK) and its restart marker, and Text speed 4x. Not shown: a kill in the game; the kill tests above end the process at each step.
  • rebase: tested on 0e2228c96 on main 0407ae243; rebased last onto main dbad7d126 (new head 7c1330519). Patch: identical at both rebases (git range-diff shows the one commit as =), and the two commits of the last step (a nightly's release notes and a Steam Deck docs page) edit none of the files this change touches. Rerun: everything above except the game start, on 0e2228c96, after the first rebase (the one that moved host/hos_main.c and host/nn_hid.c under this change); nothing after the last. Game: the start above is on 461e9a030, before that first rebase, and was not rerun.

Not tested: a power cut (the tests end the process; the helper flushes the file to the disk before the move, and only a power cut would show that this works); the Windows game itself (the Windows runs above are the two tests, and the host code was compiled for Windows, not run); MoveFileExA over a file that a virus scanner holds open; a settings file that is a link on Windows (the in place case is skipped there); mouse_sens.txt and saves-moved.txt in the game (the kill tests cover the helper they use and test_settings_atomic checks the source of both writers, but no game run set HOS_MOUSE_SENS or moved saves between drives); the Steam Deck and macOS.

Must be tested:

  • Windows, launcher build, any GPU: put an older longshot_options.ini with one setting in the game folder and none in the launcher's user folder, start the game once, expecting the file copied to the user folder, the log line options: the settings in ... were copied to ..., and no .hosnew beside it (by hand: the log and the two folders).
  • Windows, any GPU: change the Game language row in the Insert menu, leave the row and quit, expecting language.txt in the game folder to hold the new code and no language.txt.hosnew beside it (by hand: the file and the folder).
  • Linux, with the saves on another drive from the game: start once, expecting the saves moved, saves-moved.txt written whole and no .hosnew in the folder (by hand: the folders and the log).

Risks

What could break: every setting write named above now goes through the helper, so a fault in it would show as a setting that is not saved. The ways out are built in: a link, or a folder that takes no new file, is written in place as before, and a move that stays refused for a second (a virus scanner holding the new file open on Windows) leaves the old file and no stand-in. The helper flushes the file to the disk before it moves it, and on Linux the folder as well. The writes are a few bytes each and happen on a key press or a menu choice (mouse_sens.txt only with HOS_MOUSE_SENS set, language.txt when the Game language row is left); no time was measured for them. A stand-in can sit beside a setting after a crash until the next start removes it.

Turning it off: revert the commit.

Compatibility: nothing changes for a player: the settings keep their names, places and lines, and longshot_options.ini's menu write is as it was. After a crash a NAME.hosnew can sit beside a setting until the next start of this version removes it.

Overlaps

Read on 2026-11-15 against forge main dbad7d126 and the 229 open pull requests: the forge's file list of each, then git merge-tree of each head with this branch and with main. 34 change a file this one changes. 18 of them merge cleanly with this branch and with main (#737, which changes host/options_path.c and tests/test_options_path.py, is one). 14 conflict with main already, in the same files, and get no more conflict hunks from this change (607 is one of them). 2 get more conflict hunks because of this change: 218 and 659, below.

  • #607 (the save files whole at Commit; open, work in progress, head 0f30a75f1, made before #557): its files are host/nn_fs.c and others, which this change does not touch, and the two pull requests do different things. Both change host/hos_main.c and tests/test_linux_saves_move.py. 607's head does not merge with main as it is: one conflict in each of those two files, where its save-work skip in saves_dir and its new test cases meet #557, which is merged. Merged with this branch the same one conflict appears in each file, on the same lines, and nothing else: this change adds no conflict to 607. On main 7bacf8468, before #557 landed, this change merged with 607's head 0f30a75f1 without a conflict and the seven tests of both passed: test_settings_atomic, test_options_path, test_linux_saves_move, 607's test_fs_save_commit, and test_vcore_fs_open, test_guest_copies, test_longshot_saves (7 passed, 0 failed, 13 s). That run is not repeated on the current main: 607 has to be rebased onto #557 first.
  • #557 (merged: a saves move that fails part way brings back what it moved): it changed saves_dir in host/hos_main.c and added cases to tests/test_linux_saves_move.py, where this change edits copy_tree and the saves-moved note. This branch was rebased over it without a conflict and git range-diff shows this change's patch unchanged. The files copy_tree copies are now made with the helper inside #557's rollback, and tests/test_linux_saves_move.py, with #557's cases, passes.
  • #550 (merged): the menu's write of longshot_options.ini, described under What changed. This change does not touch it.
  • #577 (merged): removed gpu/render/rx_present_fx.cpp, the only writer of present.txt, so this change has nothing to do for that file.
  • #218 (open, work in progress, security): the real overlap, and a design clash. It replaces the same writes (language.txt, mouse_sens.txt, the first-start template, saves-moved.txt and copy_tree) with hos_fopen_write, which refuses a symbolic link, and its test tests/test_game_writes_no_links.py fails any write open in host/ or gpu/vk/ that does not go through it. Merged with this branch it has more conflict hunks than with main alone: host/hos_main.c 7 against 5, host/nn_applet.c 3 against 1, host/nn_hid.c 2 against 1, gpu/vk/vk_menu_i18n.cpp 2 against 0 and tests/test_linux_saves_move.py 1 against 0. The helper here writes a linked settings file in place, as fopen "w" does and as #550 does, where 218 refuses the link, and the new header would fail 218's rule until it is in its list of helpers. Whoever lands second adapts: the helper's link case would refuse (one line in hos_atomic_write) and the header would be added to 218's list. Nothing is decided here; 218 is work in progress and its author and the maintainers choose.
  • #659 (work in progress, host/nn_hid.c): it conflicts with main already in 3 places and with this branch in 4; the added one is a pair of adjacent #include lines (this change adds hos_atomic_file.h).

Checklist

Everyone

  • One topic.
  • The title is a Conventional Commit (type(scope): description) of the status line's type.
  • Why gives the reason plainly and links the issues that fit (the tracker searched).
  • Measurements has a verdict, or one line saying why there is nothing to measure.
  • Testing names what was tested, what wasn't, and what must still be tested, where and how.
  • Rebased on current main.
  • python -I -B tests/run_all.py passes (any failure that also fails on main is named).
  • No game files, dumps, build output or generated files.
  • third_party/ untouched (a new folder with a PROVENANCE.md instead).
  • The license is OK: GPL-3.0-or-later, nothing incompatible and nothing decompiled.

When it applies

  • AI assistance recorded with Assisted-by: / Co-Authored-By: trailers.
  • A game-free test added for the fix, failing on main and passing here.
  • A check under tests/ that reads changed source updated in this PR, not weakened.
  • Host code builds on both Windows (llvm-mingw) and Linux, with UTF-8 paths.
  • Docs updated.

New on this forge; not opened on GitHub.

**Type:** fix · **Risk:** low · **Tested on:** Linux (Arch, RTX 4090: game-free checks and one windowless game start); Windows 11 (Intel Arc laptop, the kill tests, no game) · **Needs:** Windows, any GPU ## Why If Longshot stops in the middle of writing one of its small settings files (a crash, the process killed, a power cut), the file can be left empty or cut short, and the next start reads it as the player's settings. `language.txt` (the Game language row and the first-start template) and `mouse_sens.txt` are written with `fopen "w"`, which empties the file before it writes it, and so is the note `saves-moved.txt`. The one-time copy of a game folder's `longshot_options.ini` into the launcher's user folder, and each file the Linux move of the saves copies between drives, are made at their final names. A stop in the middle of any of them leaves an empty file or a part of one. An empty `longshot_options.ini` in the user folder makes the next start choose a preset again, as on a first start; one cut short keeps the lines before the cut and takes the defaults for the rest, and that folder is shared by every build the launcher starts. The issue tracker was searched for `longshot_options.ini`, `language.txt`, `mouse_sens`, `saves-moved`, `hosnew`, "settings reset", "settings lost", "settings corrupt" and "empty settings": no issue is about this, so there is nothing to link. The game's own save files are the work of another pull request, https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/607, and the menu's write of `longshot_options.ini` was fixed by https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/550; this one leaves both alone (see Overlaps). ## How to reproduce **Seen on:** game-free, on Linux (Arch-based, kernel 7.2.15, gcc 16.2.1, Python 3.12.16), and on a Windows 11 laptop (Core Ultra 9 185H, Intel Arc, its own clang): the control at the end of `tests/test_settings_atomic.py` writes a file the way the game did and ends the process at each step. The old ways were not run in the game; the build with this change was (see Testing). **Settings and switches:** defaults. **Steps:** run `python -I -B tests/test_settings_atomic.py` on this branch and read its `control` line. On main's source the same test fails: the helper is not there and no writer uses it. **Expected:** after every kill the file is the old one or the new one, whole. **Actual:** on main the test cannot run: `host/hos_atomic_file.h` is not there and no writer uses it (18 source checks fail). Its control, which writes the old ways, ends the process at 5 steps of an `fopen "w"` write and at 5 steps of a copy made at the final name: 3 of 5 and 3 of 5 left an empty or part file, on Linux and on the Windows laptop alike. ## What changed A setting is now written beside its file as `NAME.hosnew`, flushed to the disk and moved over the old file in one step, so a stop leaves the old file or the new one. One header, `host/hos_atomic_file.h`, does it for `language.txt`, `mouse_sens.txt` and `saves-moved.txt`, for the options file's one-time copy into the user folder and for the files the saves' move copies, and the start removes the stand-in a stop leaves. The game's own save files (`host/nn_fs.c`) and the menu's write of `longshot_options.ini` are not touched. - helper: `host/hos_atomic_file.h` (new) has `hos_atomic_write`, `hos_atomic_copy_file` and `hos_atomic_remove_stale`. On Windows the move is `MoveFileExA` with `MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH`; on Linux the posix layer's `MoveFileExA` is a plain rename, so the helper flushes the folders with `fsync` after it. Where no stand-in can be made a setting is written in place as before: a file that is a link (a file moved over it would replace the link) and a folder that takes no new file though the file itself can be written. A full disk leaves the old file as it was. - options copy: `hos_options_carry` in `host/options_path.c` copied the game folder's `longshot_options.ini` into the user folder with a plain `CopyFileA` to the final name, so a kill in it could leave a short file that the next start took for the player's settings. It uses `hos_atomic_copy_file` now: a kill leaves no file there or the whole one, and the next start copies again; the helper keeps the failing call's error for the log line. - start: `recover_writes` in `host/hos_main.c` runs before anything reads a setting and removes a stale `NAME.hosnew` beside the settings file `options_path.c` chose (the user folder's with `--user`, the file `HOS_OPTIONS_FILE` names, else the game folder's), beside `language.txt` in the game folder and beside `mouse_sens.txt` in the user folder. `copy_tree` (the Linux move of the saves to another drive) copies each file with `hos_atomic_copy_file` and `saves_dir` writes `saves-moved.txt` with `hos_atomic_write`. - writers: `host/nn_applet.c` (the first-start `language.txt`), `set_game_language` in `gpu/vk/vk_menu_i18n.cpp` and `mouse_sens_save` in `host/nn_hid.c` use `hos_atomic_write`. - docs: `docs/updates-and-saves.md` has a short section on what a `.hosnew` file is. - tests: `tests/test_settings_atomic.py` with `tests/settings_atomic_test.c` kills a child at every step of a write over a file, a first write, a copy over a file and a copy to a new name; `tests/test_options_path.py` kills the options copy at each of its steps and runs `recover_writes` over a stand-in beside each settings file; `tests/test_linux_saves_move.py` includes the helper its fixture of `copy_tree` now needs. What the menu's write of `longshot_options.ini` already does since https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/550, and what this change leaves as it is: `save()` in `gpu/vk/vk_menu.cpp` writes `longshot_options.ini.new` beside the file (beside the target of a link), flushes it with `fflush` and `fclose`, and moves it over the file with `rename` (`MoveFileExA` with `MOVEFILE_REPLACE_EXISTING` on Windows). It does not flush the data to the disk before the move, does not ask Windows for a write-through move and does not flush the folder, by that change's own choice (a save comes with every arrow press in the menu), and a kill leaves `longshot_options.ini.new` beside the file until the next save overwrites it. This change does not duplicate any of that and adds nothing to it. Not covered: a `NAME.hosnew` beside `saves-moved.txt`, or inside a saves folder that a killed move left half copied, is not removed at the start; it is harmless and can be deleted by hand. ## Measurements Nothing to measure: this fixes how small files are written, with no change to a frame rate, a switch, a menu row or an ini key. What was checked is under Testing. ### Flags and settings None. No switch, menu row or ini key is added or changed; the files keep their names, places and lines. ## Testing **Tested:** environment: Linux 7.2.15 (Arch-based), Intel Core i9-13900K, 93 GiB, gcc 16.2.1, Python 3.12.16; a Windows 11 laptop (Core Ultra 9 185H, Intel Arc); forge main `0407ae243`, this PR `0e2228c96`; 2026-11-15. The suites ran with a fresh home folder, core dumps off and three tests at a time. Every result below is for that head on that main, except where a line says otherwise. - the new tests fail without the change and pass with it. On main plus only the new tests: `test_settings_atomic`, `test_options_path` and `test_linux_saves_move` all fail (for the first, 18 source checks: `host/hos_atomic_file.h` is not there). With the header alone added: `test_settings_atomic` fails (17 checks), `test_options_path` fails (`host/hos_main.c has no recover_writes()`), `test_linux_saves_move` passes. On this branch all three pass (10.4 s, 0.2 s, 0.2 s). - four deliberate breaks of this branch, one at a time, are each caught: the options copy back to `CopyFileA` (`test_options_path` and `test_settings_atomic` fail), the options file's stand-in clean-up taken out of `recover_writes` (both fail), the write made in place (`test_settings_atomic` fails), `mouse_sens_save` back to `fopen "w"` (`test_settings_atomic` fails). - kill tests on Linux, as the tests print them: `write over a file: 5 kill steps, each left the old file or the new one, whole`; `first write: 5 kill steps`; `copy over a file: 3 kill steps`; `copy to a new name: 3 kill steps`; `stale: a leftover settings.ini.hosnew is removed at the start`; `loop: 60 kills at random moments of repeated writes: every file whole, no stand-in after the start`; `in place: through a link, in a folder that takes no new file: written where the file is, the link kept`; `control (the old ways): 3 of 5 kill steps of fopen "w" and 3 of 5 of a copy at the final name left an empty or part file`. `test_options_path`: `copy_is_whole_or_not_there: 3 kill steps, each left no file or the whole one`, `start_removes_what_a_kill_left: the stand-ins of the user folder's settings, HOS_OPTIONS_FILE's, language.txt and mouse_sens.txt removed`, `failed_copy_says_why: the log says error 5, no file or stand-in left`. - the same two tests natively on the Windows laptop, in two ways each: replayed (the executables cross-built on Linux, run there) and native (compiled by the laptop's own clang). All four runs exit 0 with the test's own PASS line: `test_settings_atomic` 12.4 s and 13.3 s, 7 of 7 report lines (the `in place` case is skipped on Windows, a link needs a privilege there), `test_options_path` 0.5 s and 0.9 s, 3 of 3 lines (`failed_copy_says_why` is skipped on Windows). The counts are the Linux ones: 5, 5, 3 and 3 kill steps, 60 kills at random moments, and the control's 3 of 5 and 3 of 5 torn files. - `python -I -B tests/run_all.py`: this PR 609 passed, 21 failed, 17 skipped (171.6 s); forge main `0407ae243`, the same way, 608 passed, 21 failed, 17 skipped (162.1 s). The same 21 fail on both and none fails only here: `test_appimage_player_build`, `test_crash_dialog`, `test_crt_options`, `test_env_on`, `test_fsr_uniform_ownership`, `test_lift_cleanup_error`, `test_linux_preflight`, `test_linux_update`, `test_make_game_cache`, `test_missing_nup`, `test_mods_size_table`, `test_nonuniform_image_arrays`, `test_options_budget_once`, `test_parallel_init_order`, `test_pipeline_tour`, `test_readback_late`, `test_replay_write_order`, `test_run_all_changed`, `test_vk_occlusion_queries`, `test_vk_pgo`, `test_walk_key`. Their causes were not looked into here. The one more pass is `test_settings_atomic`. - Windows-target compile with llvm-mingw of this head's build plan (Release flags): the 206 renderer units (`gpu/vk/vk_menu_i18n.cpp` among them) and the 204 host units (the 56 C files of `host/` among them, `host/hos_main.c`, `host/options_path.c`, `host/nn_hid.c` and `host/nn_applet.c` included) compiled, none failed, and the renderer library linked. No game executable was linked (there is no Windows lift here), so this is a compile of the host code, not a Windows run of the game. - Windows-target syntax check of the test fixtures (the project's fixture check, against main): `test_settings_atomic` passes for both its compiled parts (`tests/settings_atomic_test.c` and the header as C++). `test_options_path` does not compile in that check, and it does not on main's own copy of that test either: both fail with errors of the kind `'getenv' is deprecated [-Werror,-Wdeprecated-declarations]`, in `host/hos_paths.h`, `host/options_path.c` and the generated `main.c`, and the check's own note says `main.c uses getenv; define _CRT_SECURE_NO_WARNINGS before the first include`. This change adds `getenv` calls to that generated `main.c` (5 errors there against 1 on main) and no other kind of error. The same test compiled with the laptop's own clang and passed there (above). `test_linux_saves_move` has no compile that the check reaches. The check is syntax only: it is not a Windows run. - in the game, on Linux (RTX 4090, driver 615.78.08, no window): a build of this change (head `461e9a030`, one rebase before the head above; the patch is the same, see the rebase note) made with `nx2/make_game.py`, its shaders prepared as a first start does, then started once with the control bridge on, with its game folder set up as an older install would leave it: `language.txt` removed and a `language.txt.hosnew` put beside its place (what a killed write leaves), and a `longshot_options.ini` with `text_speed=2` in the game folder, the game started with `--user`. What the run showed: the game exited 0 on the bridge's quit, version `v3.0.0-rc.4-69-g461e9a030 (source 89bd8149)` in its log, 0 fatal, 0 crash, 0 device-lost lines, no driver lines. The stale `language.txt.hosnew` was gone after the start and `language.txt` was there whole (the first-start template, 507 bytes, `en-US` as its language line). The log has `options: the settings in <game folder>/longshot_options.ini were copied to <user folder>/longshot_options.ini, where every build the launcher starts reads them now`, and the bridge read `Text speed` as 2 (`4x`). Then `option set game_language 1` (the menu's own setter): `language.txt` was replaced under a new inode (not rewritten in place), its language line went from `en-US` to `en-GB`, the row read back `English (UK)`, and after the quit no `.hosnew` or `.new` file was left in the game folder or the user folder. The Insert menu's picture taken at the end of the run (1280x720, looked at, not attached here) shows the Gameplay tab with Game language `English (UK)` and its restart marker, and Text speed `4x`. Not shown: a kill in the game; the kill tests above end the process at each step. - rebase: tested on `0e2228c96` on main `0407ae243`; rebased last onto main `dbad7d126` (new head `7c1330519`). Patch: identical at both rebases (`git range-diff` shows the one commit as `=`), and the two commits of the last step (a nightly's release notes and a Steam Deck docs page) edit none of the files this change touches. Rerun: everything above except the game start, on `0e2228c96`, after the first rebase (the one that moved `host/hos_main.c` and `host/nn_hid.c` under this change); nothing after the last. Game: the start above is on `461e9a030`, before that first rebase, and was not rerun. **Not tested:** a power cut (the tests end the process; the helper flushes the file to the disk before the move, and only a power cut would show that this works); the Windows game itself (the Windows runs above are the two tests, and the host code was compiled for Windows, not run); `MoveFileExA` over a file that a virus scanner holds open; a settings file that is a link on Windows (the `in place` case is skipped there); `mouse_sens.txt` and `saves-moved.txt` in the game (the kill tests cover the helper they use and `test_settings_atomic` checks the source of both writers, but no game run set `HOS_MOUSE_SENS` or moved saves between drives); the Steam Deck and macOS. **Must be tested:** - Windows, launcher build, any GPU: put an older `longshot_options.ini` with one setting in the game folder and none in the launcher's user folder, start the game once, expecting the file copied to the user folder, the log line `options: the settings in ... were copied to ...`, and no `.hosnew` beside it (by hand: the log and the two folders). - Windows, any GPU: change the Game language row in the Insert menu, leave the row and quit, expecting `language.txt` in the game folder to hold the new code and no `language.txt.hosnew` beside it (by hand: the file and the folder). - Linux, with the saves on another drive from the game: start once, expecting the saves moved, `saves-moved.txt` written whole and no `.hosnew` in the folder (by hand: the folders and the log). ## Risks **What could break:** every setting write named above now goes through the helper, so a fault in it would show as a setting that is not saved. The ways out are built in: a link, or a folder that takes no new file, is written in place as before, and a move that stays refused for a second (a virus scanner holding the new file open on Windows) leaves the old file and no stand-in. The helper flushes the file to the disk before it moves it, and on Linux the folder as well. The writes are a few bytes each and happen on a key press or a menu choice (`mouse_sens.txt` only with `HOS_MOUSE_SENS` set, `language.txt` when the Game language row is left); no time was measured for them. A stand-in can sit beside a setting after a crash until the next start removes it. **Turning it off:** revert the commit. **Compatibility:** nothing changes for a player: the settings keep their names, places and lines, and `longshot_options.ini`'s menu write is as it was. After a crash a `NAME.hosnew` can sit beside a setting until the next start of this version removes it. ## Overlaps Read on 2026-11-15 against forge main `dbad7d126` and the 229 open pull requests: the forge's file list of each, then `git merge-tree` of each head with this branch and with main. 34 change a file this one changes. 18 of them merge cleanly with this branch and with main (https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/737, which changes `host/options_path.c` and `tests/test_options_path.py`, is one). 14 conflict with main already, in the same files, and get no more conflict hunks from this change (607 is one of them). 2 get more conflict hunks because of this change: 218 and 659, below. - https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/607 (the save files whole at Commit; open, work in progress, head `0f30a75f1`, made before #557): its files are `host/nn_fs.c` and others, which this change does not touch, and the two pull requests do different things. Both change `host/hos_main.c` and `tests/test_linux_saves_move.py`. 607's head does not merge with main as it is: one conflict in each of those two files, where its `save-work` skip in `saves_dir` and its new test cases meet https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/557, which is merged. Merged with this branch the same one conflict appears in each file, on the same lines, and nothing else: this change adds no conflict to 607. On main `7bacf8468`, before #557 landed, this change merged with 607's head `0f30a75f1` without a conflict and the seven tests of both passed: `test_settings_atomic`, `test_options_path`, `test_linux_saves_move`, 607's `test_fs_save_commit`, and `test_vcore_fs_open`, `test_guest_copies`, `test_longshot_saves` (7 passed, 0 failed, 13 s). That run is not repeated on the current main: 607 has to be rebased onto #557 first. - https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/557 (merged: a saves move that fails part way brings back what it moved): it changed `saves_dir` in `host/hos_main.c` and added cases to `tests/test_linux_saves_move.py`, where this change edits `copy_tree` and the saves-moved note. This branch was rebased over it without a conflict and `git range-diff` shows this change's patch unchanged. The files `copy_tree` copies are now made with the helper inside #557's rollback, and `tests/test_linux_saves_move.py`, with #557's cases, passes. - https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/550 (merged): the menu's write of `longshot_options.ini`, described under What changed. This change does not touch it. - https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/577 (merged): removed `gpu/render/rx_present_fx.cpp`, the only writer of `present.txt`, so this change has nothing to do for that file. - https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/218 (open, work in progress, security): the real overlap, and a design clash. It replaces the same writes (`language.txt`, `mouse_sens.txt`, the first-start template, `saves-moved.txt` and `copy_tree`) with `hos_fopen_write`, which refuses a symbolic link, and its test `tests/test_game_writes_no_links.py` fails any write open in `host/` or `gpu/vk/` that does not go through it. Merged with this branch it has more conflict hunks than with main alone: `host/hos_main.c` 7 against 5, `host/nn_applet.c` 3 against 1, `host/nn_hid.c` 2 against 1, `gpu/vk/vk_menu_i18n.cpp` 2 against 0 and `tests/test_linux_saves_move.py` 1 against 0. The helper here writes a linked settings file in place, as `fopen "w"` does and as #550 does, where 218 refuses the link, and the new header would fail 218's rule until it is in its list of helpers. Whoever lands second adapts: the helper's link case would refuse (one line in `hos_atomic_write`) and the header would be added to 218's list. Nothing is decided here; 218 is work in progress and its author and the maintainers choose. - https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/659 (work in progress, `host/nn_hid.c`): it conflicts with main already in 3 places and with this branch in 4; the added one is a pair of adjacent `#include` lines (this change adds `hos_atomic_file.h`). ## Checklist ### Everyone - [x] One topic. - [x] The title is a Conventional Commit (`type(scope): description`) of the status line's type. - [x] Why gives the reason plainly and links the issues that fit (the tracker searched). - [x] Measurements has a verdict, or one line saying why there is nothing to measure. - [x] Testing names what was tested, what wasn't, and what must still be tested, where and how. - [x] Rebased on current `main`. - [x] `python -I -B tests/run_all.py` passes (any failure that also fails on `main` is named). - [x] No game files, dumps, build output or generated files. - [x] `third_party/` untouched (a new folder with a `PROVENANCE.md` instead). - [x] The license is OK: GPL-3.0-or-later, nothing incompatible and nothing decompiled. ### When it applies - [x] AI assistance recorded with `Assisted-by:` / `Co-Authored-By:` trailers. - [x] A game-free test added for the fix, failing on `main` and passing here. - [x] A check under `tests/` that reads changed source updated in this PR, not weakened. - [x] Host code builds on both Windows (llvm-mingw) and Linux, with UTF-8 paths. - [x] Docs updated. New on this forge; not opened on GitHub.
fix(settings): a crash or a kill while a setting is written no longer leaves its file empty or cut short
Some checks failed
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Has been skipped
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Has been skipped
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Has been skipped
Third-party parts / The same as upstream (pull_request) Has been skipped
CI / Conventional Commit title (pull_request) Successful in 3s
Website / Website on Cloudflare (pull_request_target) Has been skipped
CI / Workflow lint (actionlint) (pull_request) Successful in 6s
Guide screenshots / Guide images and local UI source review (pull_request) Failing after 6s
Third-party parts / As their notes say (pull_request) Successful in 8s
PR description / Description follows the template (pull_request_target) Successful in 4s
7c13305196
The game wrote its small settings files with fopen "w", which empties the file before it writes
it: language.txt, mouse_sens.txt and the note saves-moved.txt. The one-time copy of
a game folder's longshot_options.ini into the user folder (hos_options_carry) and each file the
Linux move of the saves copies between drives were made at their final name. A crash, a kill or
a power cut in the middle left an empty file or a part of one, and the next start took it for
the player's settings: an empty longshot_options.ini in the user folder makes the first-start
choice again, a cut one keeps its lines before the cut and the defaults after it.

Every such write now goes through one header, host/hos_atomic_file.h. A file is written beside
itself as <name>.hosnew, flushed to the disk, and moved over the old file in one step, so a stop
leaves the old file or the new one. On Windows the move is MoveFileExA (a narrow call, like the
host's other folder calls under the UTF-8 manifest) with MOVEFILE_REPLACE_EXISTING |
MOVEFILE_WRITE_THROUGH. On Linux the posix layer's MoveFileExA is a plain rename that does not
flush the folder, so the helper flushes the folders itself with fsync after the move.

- host/hos_atomic_file.h (new): hos_atomic_write, hos_atomic_copy_file and
  hos_atomic_remove_stale. Where no stand-in can be made a setting is written in place, as it
  was: a file that is a link (a file moved over it would replace the link, not the file it
  names), or a folder that takes no new file though the file itself can be written. A full disk
  leaves the old file as it was. hos_atomic_copy_file keeps the failing call's error for the log
  line.
- host/options_path.c hos_options_carry: the one-time copy of the game folder's
  longshot_options.ini into the user folder was a plain CopyFileA to the final name, so a kill
  in it could leave a short file that the next start took for the player's settings. It is
  hos_atomic_copy_file now: a kill leaves no file there or the whole one, and the next start
  copies again.
- host/hos_main.c: recover_writes runs at the start, before anything reads a setting, and
  removes a stale <name>.hosnew beside the settings file options_path.c chose (the user folder's
  with --user, the file HOS_OPTIONS_FILE names, else the game folder's: the options' copy leaves
  its stand-in there), beside language.txt in the game folder, and beside mouse_sens.txt in the
  user folder. copy_tree, the copy that moves the saves to another drive on
  Linux, copies each file with hos_atomic_copy_file, and saves_dir writes saves-moved.txt with
  hos_atomic_write.
- host/nn_applet.c (the first-start language.txt), gpu/vk/vk_menu_i18n.cpp set_game_language
  (the Game language row's language.txt) and host/nn_hid.c mouse_sens_save (mouse_sens.txt) use
  hos_atomic_write.
- docs/updates-and-saves.md: a short section on what a .hosnew file is.

Not changed: the game's own save files (host/nn_fs.c), which
#607 changes by another design, and the
menu's write of longshot_options.ini, which is already replaced whole by
#550 (it writes <name>.new beside the file
and renames it).

Not covered: a <name>.hosnew beside saves-moved.txt, or inside a saves folder that a killed move
left half copied, is not removed at the start.

tests/test_settings_atomic.py and tests/settings_atomic_test.c compile the helper and kill the
process at every step of a write over a file, a first write, a copy over a file and a copy to a
new name. After each kill the file is the old one or the new one, whole, the next write goes
over the stand-in the kill left, and after the start's clean-up no stand-in is left. They also
kill endless writes at random moments, write a file that is a link and a file in a folder that
takes no new file, run the old ways as a control (so the checks are seen to find a tear), and
check that every settings writer goes through the helper. tests/test_options_path.py kills the
options copy at each of its steps (no file in the user folder or the whole one, and the next
start copies again), runs recover_writes, cut out of hos_main.c, over a stand-in beside each
settings file, and checks that a failed copy logs its real error. tests/test_linux_saves_move.py
includes the helper its fixture of copy_tree now needs.

Assisted-by: Claude Sonnet 5.5
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Author
Member

Linux, RTX 4090, driver 615.78.08, windowless: a build of this change (head 461e9a030, the same patch as the pull request's head, which has only been rebased since) started once with its game folder as an older install leaves it: no language.txt, a stale language.txt.hosnew beside its place, and a longshot_options.ini with Text speed 2 in the game folder, started with --user. The stale file was removed at the start and language.txt was written whole by the first-start template; the options were copied to the user folder (the log says so) and read (Text speed 4x); then the Game language row set from en-US to en-GB through the control bridge replaced language.txt (a new inode, language line en-GB), the row read English (UK) with its restart marker, the game quit with exit 0, no .hosnew or .new file was left in either folder, and the log has 0 fatal, crash and device-lost lines.

Linux, RTX 4090, driver 615.78.08, windowless: a build of this change (head 461e9a030, the same patch as the pull request's head, which has only been rebased since) started once with its game folder as an older install leaves it: no language.txt, a stale language.txt.hosnew beside its place, and a longshot_options.ini with Text speed 2 in the game folder, started with --user. The stale file was removed at the start and language.txt was written whole by the first-start template; the options were copied to the user folder (the log says so) and read (Text speed 4x); then the Game language row set from en-US to en-GB through the control bridge replaced language.txt (a new inode, language line en-GB), the row read English (UK) with its restart marker, the game quit with exit 0, no .hosnew or .new file was left in either folder, and the log has 0 fatal, crash and device-lost lines.
Author
Member

Marking this ready for review. It still merges with current main (71b1edd89) without a conflict, and the description's checks pass.

One red check is not from this change: "Guide screenshots" fails on main itself, because the published Graphics picture still shows help text that the menu no longer has. The Python source checks were skipped while this was a draft and should run now.

Marking this ready for review. It still merges with current main (`71b1edd89`) without a conflict, and the description's checks pass. One red check is not from this change: "Guide screenshots" fails on main itself, because the published Graphics picture still shows help text that the menu no longer has. The Python source checks were skipped while this was a draft and should run now.
mooki changed title from WIP: fix(settings): a crash or a kill while a setting is written no longer leaves its file empty or cut short to fix(settings): a crash or a kill while a setting is written no longer leaves its file empty or cut short 2026-11-16 12:22:23 +00:00
Some checks failed
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Has been skipped
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Has been skipped
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Has been skipped
Third-party parts / The same as upstream (pull_request) Has been skipped
CI / Conventional Commit title (pull_request) Successful in 3s
Website / Website on Cloudflare (pull_request_target) Has been skipped
CI / Workflow lint (actionlint) (pull_request) Successful in 6s
Guide screenshots / Guide images and local UI source review (pull_request) Failing after 6s
Third-party parts / As their notes say (pull_request) Successful in 8s
PR description / Description follows the template (pull_request_target) Successful in 4s
This pull request doesn't have enough approvals yet. 0 of 1 approvals granted.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u 'fix/settings-all-or-nothing:mooki-fix/settings-all-or-nothing'
git switch 'mooki-fix/settings-all-or-nothing'
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
MagicBeanDev/Longshot!976
No description provided.