fix(settings): a crash or a kill while a setting is written no longer leaves its file empty or cut short #976
No reviewers
Labels
No labels
area/accessibility
area/audio
area/input
area/install
area/menu
area/renderer
area/saves
area/tooling
area/upscaling
beanstalk: approved
beanstalk: failed
beanstalk: needs approval
beanstalk: running
beanstalk: tested
ci: approved
good first issue
gpu/amd
gpu/igpu
gpu/intel
gpu/nvidia
help wanted
kind
bug
kind
chore
kind
docs
kind
feature
kind
performance
kind
question
needs measurements
platform/linux
platform/macos
platform/steam-deck
platform/windows
preview
priority
critical
priority
high
priority
low
regression
risk
high
risk
low
risk
medium
status
blocked
status
confirmed
status
duplicate
status
invalid
status
needs-info
status
wontfix
ci: approved
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!976
Loading…
Reference in a new issue
No description provided.
Delete branch "mooki/Longshot:fix/settings-all-or-nothing"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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) andmouse_sens.txtare written withfopen "w", which empties the file before it writes it, and so is the notesaves-moved.txt. The one-time copy of a game folder'slongshot_options.iniinto 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 emptylongshot_options.iniin 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 oflongshot_options.iniwas 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.pywrites 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.pyon this branch and read itscontrolline. 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.his 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 anfopen "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 forlanguage.txt,mouse_sens.txtandsaves-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 oflongshot_options.iniare not touched.host/hos_atomic_file.h(new) hashos_atomic_write,hos_atomic_copy_fileandhos_atomic_remove_stale. On Windows the move isMoveFileExAwithMOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH; on Linux the posix layer'sMoveFileExAis a plain rename, so the helper flushes the folders withfsyncafter 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.hos_options_carryinhost/options_path.ccopied the game folder'slongshot_options.iniinto the user folder with a plainCopyFileAto the final name, so a kill in it could leave a short file that the next start took for the player's settings. It useshos_atomic_copy_filenow: 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.recover_writesinhost/hos_main.cruns before anything reads a setting and removes a staleNAME.hosnewbeside the settings fileoptions_path.cchose (the user folder's with--user, the fileHOS_OPTIONS_FILEnames, else the game folder's), besidelanguage.txtin the game folder and besidemouse_sens.txtin the user folder.copy_tree(the Linux move of the saves to another drive) copies each file withhos_atomic_copy_fileandsaves_dirwritessaves-moved.txtwithhos_atomic_write.host/nn_applet.c(the first-startlanguage.txt),set_game_languageingpu/vk/vk_menu_i18n.cppandmouse_sens_saveinhost/nn_hid.cusehos_atomic_write.docs/updates-and-saves.mdhas a short section on what a.hosnewfile is.tests/test_settings_atomic.pywithtests/settings_atomic_test.ckills 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.pykills the options copy at each of its steps and runsrecover_writesover a stand-in beside each settings file;tests/test_linux_saves_move.pyincludes the helper its fixture ofcopy_treenow needs.What the menu's write of
longshot_options.inialready does since #550, and what this change leaves as it is:save()ingpu/vk/vk_menu.cppwriteslongshot_options.ini.newbeside the file (beside the target of a link), flushes it withfflushandfclose, and moves it over the file withrename(MoveFileExAwithMOVEFILE_REPLACE_EXISTINGon 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 leaveslongshot_options.ini.newbeside 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.hosnewbesidesaves-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 PR0e2228c96; 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.test_settings_atomic,test_options_pathandtest_linux_saves_moveall fail (for the first, 18 source checks:host/hos_atomic_file.his not there). With the header alone added:test_settings_atomicfails (17 checks),test_options_pathfails (host/hos_main.c has no recover_writes()),test_linux_saves_movepasses. On this branch all three pass (10.4 s, 0.2 s, 0.2 s).CopyFileA(test_options_pathandtest_settings_atomicfail), the options file's stand-in clean-up taken out ofrecover_writes(both fail), the write made in place (test_settings_atomicfails),mouse_sens_saveback tofopen "w"(test_settings_atomicfails).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.test_settings_atomic12.4 s and 13.3 s, 7 of 7 report lines (thein placecase is skipped on Windows, a link needs a privilege there),test_options_path0.5 s and 0.9 s, 3 of 3 lines (failed_copy_says_whyis 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 main0407ae243, 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 istest_settings_atomic.gpu/vk/vk_menu_i18n.cppamong them) and the 204 host units (the 56 C files ofhost/among them,host/hos_main.c,host/options_path.c,host/nn_hid.candhost/nn_applet.cincluded) 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.test_settings_atomicpasses for both its compiled parts (tests/settings_atomic_test.cand the header as C++).test_options_pathdoes 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], inhost/hos_paths.h,host/options_path.cand the generatedmain.c, and the check's own note saysmain.c uses getenv; define _CRT_SECURE_NO_WARNINGS before the first include. This change addsgetenvcalls to that generatedmain.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_movehas no compile that the check reaches. The check is syntax only: it is not a Windows run.461e9a030, one rebase before the head above; the patch is the same, see the rebase note) made withnx2/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.txtremoved and alanguage.txt.hosnewput beside its place (what a killed write leaves), and alongshot_options.iniwithtext_speed=2in the game folder, the game started with--user. What the run showed: the game exited 0 on the bridge's quit, versionv3.0.0-rc.4-69-g461e9a030 (source 89bd8149)in its log, 0 fatal, 0 crash, 0 device-lost lines, no driver lines. The stalelanguage.txt.hosnewwas gone after the start andlanguage.txtwas there whole (the first-start template, 507 bytes,en-USas its language line). The log hasoptions: 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 readText speedas 2 (4x). Thenoption set game_language 1(the menu's own setter):language.txtwas replaced under a new inode (not rewritten in place), its language line went fromen-UStoen-GB, the row read backEnglish (UK), and after the quit no.hosnewor.newfile 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 languageEnglish (UK)and its restart marker, and Text speed4x. Not shown: a kill in the game; the kill tests above end the process at each step.0e2228c96on main0407ae243; rebased last onto maindbad7d126(new head7c1330519). Patch: identical at both rebases (git range-diffshows 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, on0e2228c96, after the first rebase (the one that movedhost/hos_main.candhost/nn_hid.cunder this change); nothing after the last. Game: the start above is on461e9a030, 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);
MoveFileExAover a file that a virus scanner holds open; a settings file that is a link on Windows (thein placecase is skipped there);mouse_sens.txtandsaves-moved.txtin the game (the kill tests cover the helper they use andtest_settings_atomicchecks the source of both writers, but no game run setHOS_MOUSE_SENSor moved saves between drives); the Steam Deck and macOS.Must be tested:
longshot_options.iniwith 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 lineoptions: the settings in ... were copied to ..., and no.hosnewbeside it (by hand: the log and the two folders).language.txtin the game folder to hold the new code and nolanguage.txt.hosnewbeside it (by hand: the file and the folder).saves-moved.txtwritten whole and no.hosnewin 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.txtonly withHOS_MOUSE_SENSset,language.txtwhen 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 aNAME.hosnewcan sit beside a setting until the next start of this version removes it.Overlaps
Read on 2026-11-15 against forge main
dbad7d126and the 229 open pull requests: the forge's file list of each, thengit merge-treeof 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 changeshost/options_path.candtests/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.0f30a75f1, made before #557): its files arehost/nn_fs.cand others, which this change does not touch, and the two pull requests do different things. Both changehost/hos_main.candtests/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 itssave-workskip insaves_dirand 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 main7bacf8468, before #557 landed, this change merged with 607's head0f30a75f1without a conflict and the seven tests of both passed:test_settings_atomic,test_options_path,test_linux_saves_move, 607'stest_fs_save_commit, andtest_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.saves_dirinhost/hos_main.cand added cases totests/test_linux_saves_move.py, where this change editscopy_treeand the saves-moved note. This branch was rebased over it without a conflict andgit range-diffshows this change's patch unchanged. The filescopy_treecopies are now made with the helper inside #557's rollback, andtests/test_linux_saves_move.py, with #557's cases, passes.longshot_options.ini, described under What changed. This change does not touch it.gpu/render/rx_present_fx.cpp, the only writer ofpresent.txt, so this change has nothing to do for that file.language.txt,mouse_sens.txt, the first-start template,saves-moved.txtandcopy_tree) withhos_fopen_write, which refuses a symbolic link, and its testtests/test_game_writes_no_links.pyfails any write open inhost/orgpu/vk/that does not go through it. Merged with this branch it has more conflict hunks than with main alone:host/hos_main.c7 against 5,host/nn_applet.c3 against 1,host/nn_hid.c2 against 1,gpu/vk/vk_menu_i18n.cpp2 against 0 andtests/test_linux_saves_move.py1 against 0. The helper here writes a linked settings file in place, asfopen "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 inhos_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.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#includelines (this change addshos_atomic_file.h).Checklist
Everyone
type(scope): description) of the status line's type.main.python -I -B tests/run_all.pypasses (any failure that also fails onmainis named).third_party/untouched (a new folder with aPROVENANCE.mdinstead).When it applies
Assisted-by:/Co-Authored-By:trailers.mainand passing here.tests/that reads changed source updated in this PR, not weakened.New on this forge; not opened on GitHub.
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.
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.
WIP: fix(settings): a crash or a kill while a setting is written no longer leaves its file empty or cut shortto fix(settings): a crash or a kill while a setting is written no longer leaves its file empty or cut shortView command line instructions
Checkout
From your project repository, check out a new branch and test the changes.