Skip to content

Keep the guest gate in the d3d9 alloc_app and free_app callbacks - #55

Open
kanapitsas wants to merge 2 commits into
mainfrom
fix/gate-alloc-app-no-yield
Open

kanapitsas wants to merge 2 commits into
mainfrom
fix/gate-alloc-app-no-yield

Conversation

@kanapitsas

Copy link
Copy Markdown
Owner

Problem

rt_guest_enter gives the guest gate to a waiting thread once in 1024 calls, even when the caller already holds it. The d3d9 alloc_app and free_app callbacks take the gate this way. mtld3d can call them while it holds one of its own locks: on a staging miss, alloc_app runs under the page box pool's mutex. During the yield, another guest thread can take the gate, call into d3d9 and wait for that mutex, while the first thread waits for the gate. The game hangs. Nobody has seen it happen, but nothing prevents it.

Change

  • rt_guest_enter_no_yield in runtime/scheduler.c takes the gate like rt_guest_enter, but never gives it up.
  • host_alloc_app and host_free_app use it.

Tests

  • New check d3d9_alloc_app_keeps_the_guest_gate: a thread waits for the gate while the test, holding it, calls alloc_app and free_app 64 Ki times. The waiting thread must not get in before the test lets go. It fails 5 times out of 5 with the old rt_guest_enter.
  • make check (217 Python tests, 265 abi-smoke checks) and abi-smoke-ubsan pass.
  • Not run: the game, and make d3d9metal-check against the real library.

Not covered

A separate risk, older than this change: under D3DCREATE_MULTITHREADED, mtld3d holds its ApiLock through Present, including the unix calls where the gate is released. A second guest thread could take the gate and wait for that lock. Not reproduced.

🤖 Generated with Claude Code

kanapitsas and others added 2 commits November 26, 2026 15:06
rt_guest_enter yields the gate once in 1024 calls when a thread waits for
it, even when the caller already holds it. mtld3d may call alloc_app or
free_app while it holds one of its own locks. During such a yield another
guest thread could take the gate, call into d3d9 and wait for that lock,
while the first thread waits for the gate: a deadlock. The callbacks now
take the gate with rt_guest_enter_no_yield, which never gives it up.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant