Skip to content

mtl_finish_gpu: publish the replay block before waiting on g_up_pending (the QueueFinish sibling of the up_begin fix) - #141

Closed
LNPANDY wants to merge 1 commit into
seedds:mainfrom
LNPANDY:fix/upload-wait-queuefinish
Closed

LNPANDY wants to merge 1 commit into
seedds:mainfrom
LNPANDY:fix/upload-wait-queuefinish

Conversation

@LNPANDY

@LNPANDY LNPANDY commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

The up_begin fix (Little Nightmares II) closes one window of the upload deadlock: an upload batch's commit sits as a record in the open replay block (rec_commit_upload), the submit thread sleeps on a wait only the encode thread can resolve, and nothing hands the open block over mid-submission — the submission cannot end while its own thread blocks.

The sibling window this PR closes. mtl_finish_gpu (nvnQueueFinish) waits on g_up_pending the same way, on the submit thread, mid-submission: a loader that calls QueueFinish inside a submission while one or more upload commits sit recorded in the open block sleeps forever. The loader this file already documents — Fire Emblem: Three Houses "submits a copy and calls QueueFinish for each resource it uploads" — is exactly that shape.

The fix is the same publish up_begin now does: hand the open block to the encode thread before the wait, so the pending upload batches commit, their completion handlers drop the counter and wake the waiter. Non-submit threads (suspend, diagnostics) cannot publish the submit thread's open block and keep the old behavior.

How it was found: analyzing a Hades II save-load freeze on an iPhone 14 Pro (iOS 26.7.1) where the hung stack was the up_begin path (report: LNPANDY/NX2Apple#4). The up_begin half of the fix was derived independently from that report and came out word for word the same as yours — this PR builds on your version and adds the mtl_finish_gpu half.

Checked: the host builds with the change; Hades II v0 and v1.0.137848 save loads on the Mac host with the encode thread forced on (DM_NVN_ENC_THREAD=1, the iOS default), both smooth, no regression. Not checked: an iPhone (the reporter is testing the full fix on one).

The up_begin fix (Little Nightmares II) closes one window of the upload
deadlock: an upload batch's commit sits as a record in the open replay
block (rec_commit_upload), the submit thread sleeps on a wait only the
encode thread can resolve, and nothing hands the open block over
mid-submission — the submission cannot end while its own thread blocks.

mtl_finish_gpu has the same wait on the same thread: a loader calling
nvnQueueFinish inside a submission while upload commits sit recorded in
the open block sleeps on g_up_pending forever (the loader this file
already documents — Three Houses waits once per resource it uploads —
is exactly that shape). Publish the block first, as up_begin now does;
the encode thread commits the batches, the completion handlers drop the
counter and wake the waiter. Non-submit threads (suspend, diagnostics)
cannot publish the submit thread's open block and keep the old
behavior.

Found analyzing a Hades II save-load freeze on an iPhone 14 Pro where
the up_begin path was the one that hung (report:
LNPANDY/NX2Apple#4; our independently
derived up_begin fix came out word for word the same as yours).

Checked: the host builds; Hades II v0 and v1.0.137848 save loads on the
Mac host with the encode thread forced on (the iOS default), both
smooth. Not checked: an iPhone.
seedds added a commit that referenced this pull request Oct 24, 2026
…thread at once (general, from PR #141). With the encode thread on (the iOS default, DM_NVN_ENC_THREAD), an upload batch's commit is a record in the encode thread's current block (rec_commit_upload), which is handed over when the block fills or a submission ends. Outside a submission (no replay command buffer: a texture zeroed at creation, tex_zero_fill, or a packaged texture uploaded between submissions) up_flush meant to commit the batch on the spot, but the record stayed in the block until the next submission, and every wait for it waited for that submission: nvnQueueFinish (mtl_finish_gpu, on any thread) or up_begin on a busy slot. A game whose last submitting thread creates a texture and then calls nvnQueueFinish before it submits again waited for good. up_flush now hands the block over when it runs outside a submission (it runs on the submit thread only, up_owner_check). From PR #141 (LNPANDY, found analysing a Hades II save-load freeze on an iPhone 14 Pro): mtl_finish_gpu hands over the block before it waits when it runs on the submit thread, as up_begin does since 9cb7af3 and q_cb does; nvnQueueFinish cannot run inside a submission (one nvnQueueSubmitCommands call, which ends with rec_drain), so that line matters for the window above and for the diagnostics that wait mid-submission (td_dump, DM_NVN_T2T_TRACE). Checked: dmhost builds for Super Mario Bros. Wonder (NVN) and Hades (Vulkan), the host unit tests pass, the graphics run list passes, and with DM_NVN_ENC_THREAD=1 every test passes but t_rt_range, which stops on main alike (DmRecEnc has no stand-in for the copyFromTexture:...toBuffer:...options: form). Not checked: a game, the iPhone
@seedds

seedds commented Oct 24, 2026

Copy link
Copy Markdown
Owner

Thanks! Ported in b87a0cc with your mtl_finish_gpu line as is, plus one more change.

nvnQueueFinish can't actually run inside a submission: a submission is one nvnQueueSubmitCommands call, and it ends with rec_drain. The window your line closes is the one between submissions. Outside a submission, up_flush commits a batch on the spot (a texture zeroed at creation, or a packaged texture uploaded between submissions). With the encode thread on, that commit stayed a record in the current block until the next submission. So nvnQueueFinish waited for good on the last thread that submitted, and also on any other thread, which your on_sub_thread() guard leaves out.

To cover every waiter, up_flush now hands the block over itself when it runs outside a submission (if (!g_cb) rec_publish();). Your line stays, for the submit thread and for the diagnostics that wait mid-submission.

Checked: the host builds, the host unit tests and the graphics run list pass, and so do the graphics tests with DM_NVN_ENC_THREAD=1, except t_rt_range, which already fails the same way on main (a missing record stand-in for one copyFromTexture form). Not checked on an iPhone yet, so a result from your reporter would be welcome.

@seedds seedds closed this Oct 24, 2026
@LNPANDY

LNPANDY commented Oct 24, 2026

Copy link
Copy Markdown
Contributor Author

Thank you for the port and the correction — much appreciated.

Your analysis of the window is cleaner than mine: I had reasoned from the Three Houses comment that QueueFinish could arrive mid-submission, but as you say a submission is one nvnQueueSubmitCommands call ending in rec_drain, so the real window is between submissions — an up_flush that means to commit on the spot leaving its commit as a record instead, and every waiter (QueueFinish on any thread, up_begin on a busy slot) then waiting on a submission that may never come. The if (!g_cb) rec_publish(); in up_flush covers all of those, which my on_sub_thread() guard deliberately left out; glad the line itself stays useful for the submit thread and the mid-submission diagnostics.

We'll relay a result from the reporter as soon as he rebuilds (his device is an iPhone 14 Pro on iOS 26.7.1).

Know-It-All-Brother pushed a commit to Know-It-All-Brother/nxnative that referenced this pull request Nov 9, 2026
…thread at once (general, from PR seedds#141). With the encode thread on (the iOS default, DM_NVN_ENC_THREAD), an upload batch's commit is a record in the encode thread's current block (rec_commit_upload), which is handed over when the block fills or a submission ends. Outside a submission (no replay command buffer: a texture zeroed at creation, tex_zero_fill, or a packaged texture uploaded between submissions) up_flush meant to commit the batch on the spot, but the record stayed in the block until the next submission, and every wait for it waited for that submission: nvnQueueFinish (mtl_finish_gpu, on any thread) or up_begin on a busy slot. A game whose last submitting thread creates a texture and then calls nvnQueueFinish before it submits again waited for good. up_flush now hands the block over when it runs outside a submission (it runs on the submit thread only, up_owner_check). From PR seedds#141 (LNPANDY, found analysing a Hades II save-load freeze on an iPhone 14 Pro): mtl_finish_gpu hands over the block before it waits when it runs on the submit thread, as up_begin does since 874c7c5 and q_cb does; nvnQueueFinish cannot run inside a submission (one nvnQueueSubmitCommands call, which ends with rec_drain), so that line matters for the window above and for the diagnostics that wait mid-submission (td_dump, DM_NVN_T2T_TRACE). Checked: dmhost builds for Super Mario Bros. Wonder (NVN) and Hades (Vulkan), the host unit tests pass, the graphics run list passes, and with DM_NVN_ENC_THREAD=1 every test passes but t_rt_range, which stops on main alike (DmRecEnc has no stand-in for the copyFromTexture:...toBuffer:...options: form). Not checked: a game, the iPhone
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