Skip to content

Resource Manager: Fix a done fence never being released by data streams - #1199

Closed
Ed-Fe wants to merge 1 commit into
mackron:devfrom
Ed-Fe:fix-rm-stream-done-fence
Closed

Ed-Fe wants to merge 1 commit into
mackron:devfrom
Ed-Fe:fix-rm-stream-done-fence

Conversation

@Ed-Fe

@Ed-Fe Ed-Fe commented Nov 12, 2026

Copy link
Copy Markdown

Hi,

I use miniaudio in a game engine called NonVisual Gaming Toolkit (NVGT), in a private fork with my own modifications. This PR to the resource manager was made entirely by AI. Here is the problem:

ma_resource_manager_data_stream_init_ex() calls ma_resource_manager_pipeline_notifications_acquire_all_fences(), which acquires both the init and the done fence. The LOAD_DATA_STREAM job only receives pInitFence and only releases that one, and no other stream job references the done fence. So a done fence passed to a stream stays held forever, and ma_fence_wait() on it never returns.

In our case the engine registers the same fence on notifications.done for every sound and waits on it before uninitializing the sound, to make sure the load job is done with it. That worked for decoded and encoded buffers, but closing a streamed sound hung forever. ma_sound_config.pDoneFence is documented as "Not used with streams", but initNotifications.done.pFence reaches the stream anyway. Even through pDoneFence, "not used" reads as "ignored", not as "never released".

A stream has no separate done stage, so the fix only acquires the init fence in ma_resource_manager_data_stream_init_ex(), and the error path after a failed ma_resource_manager_post_job() only releases that one. A done fence passed to a stream is now ignored, as the documentation says. An alternative would be to pass the done fence to the load job and release it together with the init fence. I'm happy to change it to that if you prefer.

This PR also adds tests/resource_manager, registered with CTest. It initializes a stream asynchronously with an init fence, a done fence, and both, then polls each fence's counter with a 5 second timeout instead of waiting on it, so a failure doesn't hang the test. It builds with -std=c89 -Wall -Wextra -Wpedantic without warnings.

Results on Windows 11 (GCC 16.2, MinGW-w64):

Result
dev (631161e4) "done fence still held after 5000 ms (counter = 1)" with the done fence and with both fences
dev + this fix all three cases pass, 5 of 5 runs

#1198 also adds tests/resource_manager, with a different test. If both are accepted, the second one to be merged will conflict in tests/resource_manager/resource_manager.c and CMakeLists.txt. I'll rebase it to register both tests.

This was found while investigating a hang reported by players of an audio game built on NVGT. The investigation, the fix and the test were done by an AI assistant (Claude).

I release this contribution into the public domain / under MIT No Attribution, as in miniaudio's license.

🤖 Generated with Claude Code

ma_resource_manager_data_stream_init_ex() acquired both the init and the done fence, but the LOAD_DATA_STREAM job only ever releases the init fence. A done fence passed to a stream stayed held forever, so ma_fence_wait() on it never returned. A stream has no separate done stage, so only the init fence is acquired now. Adds a resource manager test that checks every fence passed to a stream is released.
mackron added a commit that referenced this pull request Nov 13, 2026
The issue with this one is that is that when a "done" fence is used with
a stream it'll never actually get signaled because there's no notion of
a "done" stage.

Public issue #1199
@mackron

mackron commented Nov 13, 2026

Copy link
Copy Markdown
Owner

Thanks. I fixed this manually with a different approach to your suggestion, and I also didn't want your tests. In the dev and dev-0.12 branches.

@mackron mackron closed this Nov 13, 2026
niu2x pushed a commit to niu2x/miniaudio that referenced this pull request Nov 13, 2026
The issue with this one is that is that when a "done" fence is used with
a stream it'll never actually get signaled because there's no notion of
a "done" stage.

Public issue mackron#1199

(cherry picked from commit 6ec7f05)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants