fix(audio): SDL starts each subsystem once, so the microphone's stream is never freed under the game #3228
No reviewers
Labels
No labels
area/accessibility
area/audio
area/input
area/install
area/menu
area/mods
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!3228
Loading…
Reference in a new issue
No description provided.
Delete branch "cuecuemoar/Longshot:fix/mic-sdl-init-once"
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?
Summary
With a microphone chosen by name in the Insert menu (Audio, Microphone), the game crashed after a while of play, inside SDL3.dll (
SDL_GetAudioStreamAvailable): #1524, #1928, #2310 and #2498, the last through the menu's Test microphone. The workaround so far was Microphone: Off.Cause. SDL counts each subsystem's starts in 8 bits (
static Uint8 SDL_SubsystemRefCount[32]in SDL'ssrc/SDL.c, in SDL 3.4.20 and SDL2 2.32 alike). Its only overflow check is anSDL_assert, which isn't in a release build.hos_sdl_initasked SDL for a start every time it was called, andrec_readycalls it on everyhos_sdl_mic_listandhos_sdl_mic_open.host/nn_audio_in.cpump,CHECK_NS), and each Insert menu open lists the microphones once more.SDL_InitAudio, which callsSDL_QuitAudiobecause a driver is up, and that destroys every stream opened withSDL_OpenAudioDeviceStream, the microphone's included.This changes
hos_sdl_init(host/hos_sdl.c, read it first) to remember the subsystems it has started and ask SDL only for the others. A failed start is still tried again. It runs under a lock, becauseSDL_InitSubSystemisn't safe from two threads, and the microphones and the sound's output can ask for the first time together. Nothing in Longshot stops an SDL subsystem (noSDL_QuitorSDL_QuitSubSystem), so once started is started for the run. The pads' and the window's single starts go through the same function, unchanged in effect.To see it on main: choose a microphone by name in Audio, Microphone, and leave Test microphone running for about 22 minutes. Opening and closing the Insert menu about 250 times first gets there in about a minute. The game crashes in SDL3.dll.
Fixes #1524. Fixes #1928. Fixes #2310. Fixes #2498.
Testing
Tested:
3a0f7d8f7against this PR476f38a51built the same way. The run used the title screen andkokiri-start's test profile.RX_VK_MENU_SCRIPTopened and closed the Insert menu 240 times, each open listing the microphones.option set microphone 2(read back asMicrophone (C922 Pro Stream Webcam)), and Test microphone started with the menu left open.audio in: Microphone (C922 Pro Stream Webcam) open (48000 Hz, 1 channel), then about 70 s laterCRASH: exception 0xc0000005 (read 0xd) at SDL3.dll+0x11b625. The crash record readsthread: host,module_offset: SDL3.dll+0x11b625,uptime_s: 284, and the game exited with code 5.SDL3.dll+0x11b625is the offset in #1928's and #2310's reports.Testing: 1%in the last shot). No crash record, exit code 0.tests/test_sdl_init_once.py(new) cuts the realhos_sdl_init,rec_readyandhos_sdl_mic_listand runs them with a fake SDL3 that counts as SDL does: 8 bits, sound restarted at 0. It lists the microphones 600 times with a stream open.FAIL: list 257 restarted SDL's sound (2 starts, 1 quits): the microphone's stream was freed(gcc and clang).PASS(gcc and clang).started |= wantmakes it fail again.tests/test_headless_sigterm.pycuts the same function: its fixture gets the lock's stand-ins, and its placeholders now match whole lines, becauseSRWLOCK_INITheld the wordINITit replaced. Its ten cases still pass.hos_sdl.cornn_audio_in.cpass: test_microphone_test_gain, test_audio_in, test_pad_appcontainer, test_builder_pads_route, test_display_pointer.scripts/check_host_compile.py), head and main.3a0f7d8f7: 846 passed, 53 failed, 13 skipped here, against 845, 53 and 13 on main. The 53 are the same files on both, listed below.Still to test:
The 53 files that fail on main too (this WSL setup, strict)
tests/f2i_immediate tests/global_load_16 tests/native_suatom tests/test_cloth_kernels.py tests/test_cloth_loops.py tests/test_cloth_step8.py tests/test_coop_spirv.py tests/test_curve_pass.py tests/test_dead_targets_sweep.py tests/test_default_point_size.py tests/test_depth_sample_types.py tests/test_dev_update.py tests/test_dlssg.py tests/test_draw_parameters.py tests/test_drawn_page_map.py tests/test_fsr4_tune.py tests/test_image_arrays.py tests/test_image_view_retry.py tests/test_lens_eye_pass.py tests/test_lifter.py tests/test_lower_depbar.py tests/test_lower_lds_after_sts.py tests/test_lower_register_markers.py tests/test_menu_hooks.py tests/test_mesh_fallback_capacity.py tests/test_mesh_fallback_layout.py tests/test_mesh_first_types.py tests/test_mesh_unreachable_outputs.py tests/test_native_suatom.py tests/test_no_game_title.py tests/test_no_map_reload.py tests/test_nonuniform_image_arrays.py tests/test_present_acquire_ahead.py tests/test_present_geometry_trace.py tests/test_present_held_image.py tests/test_present_image_free.py tests/test_prof_clock.py tests/test_program_answer_domains.py tests/test_reshade_fx.py tests/test_sim_turf_pools.py tests/test_spirv_struct_identity.py tests/test_sync_watch_event.py tests/test_target_pool_cap.py tests/test_target_retention.py tests/test_tex_desc_memo.py tests/test_texture_phi_dominance.py tests/test_thinlto_tls.py tests/test_turf_budget.py tests/test_ui_renderer.py tests/test_unsigned_image_types.py tests/test_vk_draw_overhead_lean.py tests/test_vk_tex_btree_get_fast.py tests/test_wmma16_spirv_val.py
Measurements
Verdict: the crash is gone. On main, the same steps crash in SDL3.dll about 70 s after Test microphone starts; with this PR the test runs 602 s without one. There is nothing to time:
hos_sdl_initruns a few times a minute at most, and now asks SDL less often.Setup: Windows 10, RTX 4090, the C922's microphone chosen by name; headless on the title screen, 240 Insert menu opens, then Test microphone (Testing above).
3a0f7d8f7476f38a51test_sdl_init_once.py(gcc, clang)Risks
SDL_InitSubSystemused to repair, it no longer gets that call. Nothing in the tree relies on that: the sound's output already started once (playback_initialized), and the pads and window start once at start-up. A failed start is still retried.longshot_options.inior the caches.hos_sdl.c.Checklist
Ready for review
main; the title is a Conventional Commit.mise run testpasses (any failure that also fails onmainis named).main, or Measurements says why there is nothing to measure.third_party/untouched; nothing GPL-incompatible.When it applies
mainand passes here.tests/that reads changed source is updated, not weakened.Assisted-by:/Co-Authored-By:trailers.Ready to merge
main, measured again or a comment says why the numbers still hold.🤖 Generated with Claude Code
https://claude.ai/code/session_01VxeaZjFD5sZgue2tRx5H5v
Tested in game: Windows 10, RTX 4090, Logitech C922 microphone chosen by name; headless, title screen: 240 Insert menu opens, then Test microphone. main
3a0f7d8f7crashed ~70 s in at SDL3.dll+0x11b625 (crash record, exit 5); this PR476f38a51ran the test 602 s with the meter reading, no crash, exit 0.WIP: fix(audio): SDL starts each subsystem once, so the microphone's stream is never freed under the gameto fix(audio): SDL starts each subsystem once, so the microphone's stream is never freed under the game@review
Recommendation: Approve. The Windows reproduction supports the fix: the base crashed after repeated microphone listings, while this head kept the meter running for 602 seconds. Linux audio and the requested Windows gameplay run remain untested. Nothing blocking is left: what remains is for the author before merging (below). Recommend approval; not approved on the forge.
The change prevents repeated microphone enumeration from wrapping SDL’s subsystem reference count and destroying an active stream. It adds a lock and a game-free regression test; the added work is limited to subsystem initialization calls.
Should fix (1): not blocking, but worth doing before merging.
tests/test_sdl_init_once.py:133: The new test treats the compiler setting as a single executable and defaults to gcc when run directly, so it does not fully honor CC. (more on the line) Do: Use the repository’s compiler_command and parse_compiler helpers for the default and explicit --cc command.Testing before merging (2) (runs on the systems it can change; it doesn't hold the recommendation)
@fixcan do L1.Checks: Still running: 10 checks.
More: does it help, other systems, switches, what was read
Does it help: Yes for the reproduced Windows case: main crashed about 70 seconds after microphone testing began following 240 menu opens; this head ran for 602 seconds without a crash and still showed input. The game-free test also reportedly fails on main and passes here.
Other systems: The same initialization function serves SDL audio, controllers and the Linux window. SDL2 and Linux playback share the changed path; the supplied game run covers Windows SDL3, not Linux or SDL2. The lock serializes first starts, and there are no SDL subsystem shutdown calls in the tree.
Switches: None added, changed or removed.
Reviewed:
476f38a513against3a0f7d8f7f: Read AGENTS.md, CONTRIBUTING.md, the pull request template, standards, both commits, the diff, both changed tests, and the changed initialization and related audio code. Skimmed the rest of host/hos_sdl.c and its callers. docs/review-checklist.md and ../Tooling/AGENTS.md were absent. No generated files or translations changed.; nothing run (a code review).Checked and fine:
Testing: No code or tests were run, as requested. CI is still running; the reported Windows game run and test results were reviewed as evidence.
Overlaps and merge order: The description identifies WIP #3116 as touching a different part of hos_sdl.c; this fix remains useful for its in-game fallback.
Automated code review by BeanBot (gpt-6.1-terra), asked for by HumanBean. Nothing was run; a maintainer decides.
@ -0,0 +130,4 @@with tempfile.TemporaryDirectory(prefix="sdl-init-once-") as folder:work = Path(folder)(work / "fixture.c").write_text(source, encoding="utf-8")exe = work / ("fixture.exe" if os.name == "nt" else "fixture")L1 (should fix): The new test treats the compiler setting as a single executable and defaults to gcc when run directly, so it does not fully honor CC.
When: With CC set to a compiler command such as
ccache gcc, run_all passes that entire string as --cc; subprocess then looks for an executable literally namedccache gccand the check fails. A direct invocation also ignores CC.Do: Use the repository’s compiler_command and parse_compiler helpers for the default and explicit --cc command.