Skip to content

Stop two threads from hooking the same Metal class - #340

Closed
woahitsraj wants to merge 1 commit into
NotProtonNot:mainfrom
woahitsraj:fix-overlay-hook-race
Closed

woahitsraj wants to merge 1 commit into
NotProtonNot:mainfrom
woahitsraj:fix-overlay-hook-race

Conversation

@woahitsraj

Copy link
Copy Markdown
Contributor

Summary

Sometimes Metaphor: ReFantazio opens to a black window with the Steam Overlay on. Two threads both hook endEncoding. The second saves our hook as the original, so the hook calls itself forever and no frame is submitted.

 ensure_encoder_hooks
-  if class is already claimed, return
-  claim class
+  if class is already hooked, return
+  lock
+  if another thread hooked it first, unlock and return
   hook setRenderPipelineState: and endEncoding
+  mark class as hooked
+  unlock

All six ensure_*_hooks functions had the same race, so all six use the lock.

Evidence

I sampled a stuck launch on 1.2.8. Two threads were spinning in hook_end_encoding, and the swap chain thread was in D3DMCommandQueue::WaitUntilSubmission. The log had two ensure_encoder_hooks lines.

hookrace starts 8 threads on a new class, 2,000 times, and checks that the saved originals aren't our hooks.

  • Before: failed 25 of 25 runs on arm64 and 25 of 25 on x86_64. Thread Sanitizer (arm64) reports a race in stage_claimed.
  • After: passed 25 of 25 on each. Thread Sanitizer reports nothing. make overlay-shim-tests: 12 passed, 0 skipped.

I ran the fix on 1.2.8 (the shim source is the same on main), CrossOver 26.3 (Rosetta, D3DMetal), an M4 Max and macOS 27.0.1. Metaphor started each time and the overlay opened. I haven't tested FEX.

@NotProtonNot

Copy link
Copy Markdown
Owner

Nice catch! I'll get this merged for a potential hotfix.

@NotProtonNot

Copy link
Copy Markdown
Owner

Merged!

@woahitsraj

Copy link
Copy Markdown
Contributor Author

Thanks!

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