fix(vk): at Windows' commit limit the renderer's C++ allocations wait for memory, then say out of memory #1291
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
3 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!1291
Loading…
Reference in a new issue
No description provided.
Delete branch "HumanBean/Longshot:push-lvooylormqzq"
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: medium · Tested on: Linux, RTX 3090 · Needs: Windows, any GPU
Why
When Windows runs out of memory to hand out (RAM plus the page file full, its "commit limit"), the renderer could not get memory for one of its own C++ objects and the game closed at once with "An unexpected error". There was no wait for memory to come back and no out-of-memory notice. The crash record says
exception: 0x20474343: a C++ exception (std::bad_alloc) that nothing caught inlongshot_vk.dll. Fourteen player reports in two days were this crash (16 GB and 32 GB PCs, rc.4 to rc.5), now closed as duplicates of #1280.#341 made the host's own C allocations wait at the limit and then say out of memory (#333). The renderer's C++ allocations don't go through that path, so they still crashed like this. With this PR they're handled the same way: wait for memory, and if none comes back, show the out-of-memory notice and stop.
The growth that brings a PC to the limit is #388's. This PR is only about how the game ends there.
How to reproduce
Seen on: Windows 11, RTX 3050 6 GB laptop, 16 GB RAM (#1260); also RTX 4060 with 32 GB (#1101), RTX 3060 Ti with 16 GB (#1217), and 11 more in #1280
Settings and switches: defaults are enough; any setting that uses more memory gets there sooner
Steps: play until the PC reaches its commit limit (the log says
commit limit near: … of commit left), from 3 minutes to over an hour of play in the reports. On a PC with more memory, this PR'sHOS_TEST_ALLOC_REFUSE=rendererreaches the same path (maindoesn't have it).Expected: the game waits for memory, then says it is out of memory (log line and notice) before it stops.
Actual:
CRASH: exception 0x20474343 … at KERNELBASE.dll+0xc569a, "An unexpected error", no notice.What changed
The renderer now has a C++
new_handler. When an allocation is refused, it waits 50 ms and lets the allocation try again, for up to 5 s, as #341's host allocations do. Then it stops through the host's out-of-memory line and notice. Allocations that already have a fallback for a refusal keep getting their null at once.new_refused()ingpu/vk/vk_main.cpp, installed bymkwvk_initwithstd::set_new_handler:operator new(plain, aligned, array) call it each timemallocrefuses, and try again when it returns.out_of_memory, which does not return. A host older than version 28 has noout_of_memory, so the renderer throwsstd::bad_allocas before.NewNothrow(gpu/vk/vk_new.h, new). Inside its scope the handler throws at once, which the C++ libraries' nothrownewturns into its null. It wraps the three nothrow allocations that have a fallback: the recorder's ring and its arena's chunks (gpu/vk/vk_recorder.h) and a capture's staging block (gpu/vk/vk_memory.cpp). It also wraps the shader translation (gpu/vk/vk_shader.cpp, bothtryblocks): a bad shader can ask for a size no PC has, and that fails the one program, as before.MkwVkHost.out_of_memory(const char *what), appended aftermain_thread_times.MKWVK_API_VERSION27 → 28. The host's side ishost_out_of_memoryinhost/rx_backend.c.hos_alloc_gave_up(n, what)inhost/mkw_glue.c(declared inhost/hos.h) is the end of #341'shos_alloc_failed, split out: the line, the notice (waited for), the fatal.n0 means the size is not known, as for the renderer's allocations, and the lines then give no byte count. #341's messages are unchanged when the size is known.HOS_TEST_ALLOC_REFUSE=renderer(#341's switch, a new label). At the replay's 600th present,new_refuse_test()asks for one allocation of 1 PB, which no PC can give, to show what a player sees at the limit.tests/test_renderer_new_handler.py(new) withtests/renderer_new_handler_test.cppcompiles the realnew_refusedcut fromvk_main.cpp. Its refusals are real: 1 PB asked of the C++ library's ownoperator new.new; the per-thread budget and its reset; nothrow forms in and out of aNewNothrowscope; and an old host or none.mkwvk_initinstalls the handler after it has the host, and that everystd::nothrowingpu/vk/is in a scope.tests/test_host_alloc_wait.pycutshos_alloc_gave_uptoo and has three new cases: no size, not the limit, and the test switch.tests/test_audio_output_devices.pytakes version 28's slot.tests/test_recorder_sync.pycopiesvk_new.hbeside the recorder.docs/development.md, theHOS_TEST_ALLOC_REFUSErow and API version 28 in the host interface paragraph.Measurements
Verdict: passes in the game. A refused renderer allocation waited 5.0 s (18.37 s to 23.38 s) on the
hostthread (the thread in the player reports), then gave the out-of-memory line and fatal instead of the uncaught exception. Nothing to measure for speed: the handler runs only when an allocation is refused.Hardware: Linux (CachyOS), RTX 3090 (NVIDIA 620.70.06), Ryzen 9 7900X, 32 GB RAM
Scene: boot to the title screen, headless (
HOS_HEADLESS=1: the notice is the log line only)Method: the game's log, with each line stamped with its arrival time; two runs, one before and one after the review's fixes, the same both times (5.0 s). The runs were on this branch over
main8e9cfc3a. The rebase onto181571bdbrought in one commit, a script the game doesn't runHow to reproduce:
HOS_TEST_ALLOC_REFUSE=renderer HOS_HEADLESS=1 ./longshotin a runtime of this branch (scripts/dev_build.py rt)8e9cfc3aefd580acstd::bad_alloc, exception 0x20474343 (the player reports in #1280)FATAL: out of memory (RAM plus swap): the renderer's C++ allocationsFlags and settings
One new value for an existing test switch, off when unset.
HOS_TEST_ALLOC_REFUSE=rendererTesting
Tested:
HOS_TEST_ALLOC_REFUSE=renderer: the run above.main8e9cfc3a(scripts/dev_build.py pack): the Linux game andlongshot_vk.socompiled and linked.new (std::nothrow)[]case keeps its result in a volatile (clang may fold anewcompared with null alone), and the shader translation went into aNewNothrowscope. It found the handler right under both libraries (plain, aligned, array, nothrow), with nothing re-entering it, and the tests failing onmainfor the bug's reason.Not tested:
mainand passing here: left to CI.Must be tested:
HOS_TEST_ALLOC_REFUSE=rendererand start the game, expecting at about 10 s of the title a 5 s pause, then the "Longshot: out of memory" notice, and the game stopping once it is closed.longshot_log.txtshould saymem: the renderer's C++ allocations: still refused after 5 sand have noCRASH: exception 0x20474343. (Test Farm: @Beanstalk windows, HOS_TEST_ALLOC_REFUSE=renderer in longshot_env.txt, the title screen)python -I -B tests/run_all.py,test_renderer_new_handlerandtest_host_alloc_waitabove all, expecting them to pass, and the new test to fail onmainwith "mkwvk_init installs no new_handler" (by hand or CI: the run's table; not run on this PC by its owner's rule)Risks
What could break:
newgoes through the handler whenmallocrefuses. A refusal that used to throw and be caught now waits up to 5 s and stops the game with the out-of-memory notice. One case: a background job'sstd::bad_allocwas caught byrun_guarded(gpu/vk/vk_jobs.cpp), logged, and the game went on.vk_new.h): the C++ library's own nothrow buffers (std::stable_sort,std::inplace_merge) and the renderer's other catch blocks around allocations (the caches' inserts, thread starts). At the limit they now wait with every other allocation, then stop with the notice. Before, the unwind to them often failed there anyway (0xc00000ffafter the throw in #1217, #1182 and #981).newadded later outside aNewNothrowscope waits and stops too, instead of getting its null. The new test's text check names any such line that writesstd::nothrow.Turning it off: revert the commits.
Compatibility: the vk ABI,
MKWVK_API_VERSION27 → 28: the host and the renderer must come from one build, as always. Saves, settings and caches don't change.Issues
Fixes #1280
See #333, #341, #388
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). Not run on this PC (the owner's rule): left to CI.third_party/untouched (a new folder with aPROVENANCE.mdinstead).When it applies
Assisted-by:/Co-Authored-By:trailers.MKWVK_API_VERSIONbumped (gpu/vk/vk_api.h'sMkwVkHostorMkwVkRendererchanged).mainand passing here. Added, not run here: CI.tests/that reads changed source updated in this PR, not weakened.🤖 Generated with Claude Code
A refused operator new in longshot_vk.dll threw std::bad_alloc that nothing caught, and the game ended with exception 0x20474343 ("An unexpected error"): no wait and no out-of-memory notice. mkwvk_init now installs a new_handler (new_refused) that tries again every 50 ms for up to 5 s, then stops through the host's out-of-memory line and notice: MkwVkHost out_of_memory (MKWVK_API_VERSION 27 -> 28), hos_alloc_gave_up split out of #341's hos_alloc_failed. Nothrow news with a fallback of their own keep their null at once (NewNothrow, vk_new.h). HOS_TEST_ALLOC_REFUSE=renderer asks for 1 PB at the 600th present to see it. Assisted-by: Claude Code Co-Authored-By: Claude Opus 5.5 <[email protected]>Tested in game: RTX 3090, NVIDIA 620.70.06, Linux (CachyOS), headless to the title screen with HOS_TEST_ALLOC_REFUSE=renderer, twice (before and after the review fixes): the renderer's refused 1 PB allocation waited 5.0 s on the host thread (18.373 s to 23.375 s), then 'mem: the renderer's C++ allocations: still refused after 5 s at the commit limit', the out-of-memory line and 'FATAL: out of memory (RAM plus swap): the renderer's C++ allocations' (exit 3), no uncaught exception. Not run on Windows.
@Beanstalk windows, any GPU: with HOS_TEST_ALLOC_REFUSE=renderer in longshot_env.txt, start the game and wait at the title screen. Expected: about 10 s in, a 5 s pause, then the "Longshot: out of memory" notice, and the game stops once it is closed. longshot_log.txt should have "mem: the renderer's C++ allocations: still refused after 5 s at the commit limit" and "FATAL: out of memory (RAM plus the page file): the renderer's C++ allocations", and no "CRASH: exception 0x20474343". Please also run the game-free checks (python -I -B tests/run_all.py), test_renderer_new_handler and test_host_alloc_wait above all.
🤖 Generated with Claude Code
Beanstalk test farm
Commit
efd580a· tier reviewed · approved by HumanBean · dashboardWhat will be tested · AI plan (
gpt-6.1-terra)This Windows plan compares a normal title-screen start on the pull request and main for about 11 minutes per tester. It fails on a crash or a large title-screen image difference. The farm cannot reliably automate the requested out-of-memory notice and dismissal, expected fatal exit, or game-free Python checks, so those remain untested by this plan.
Done (started 01:02 UTC). Comment
@Beanstalkto run it again.Possibly related, for context:
WIP: fix(vk): at Windows' commit limit the renderer's C++ allocations wait for memory, then say out of memoryto fix(vk): at Windows' commit limit the renderer's C++ allocations wait for memory, then say out of memoryTrashPanda referenced this pull request2026-11-17 01:26:41 +00:00