DLSSTweaks icon indicating copy to clipboard operation
DLSSTweaks copied to clipboard

Deadlock in ThreadFreezer::ThreadFreezer()

Open Kaldaien opened this issue 2 years ago • 13 comments

I was diagnosing a startup deadlock in Atomic Heart and discovered the code being used to freeze all threads during hook install deadlocks 9 times out of 10.

I went through hell implementing this logic safely in Special K and the big takeaway from my experience is NOT to use Toolhelp32 snapshots. They are slow as hell to capture and unsafe to walk from within a running process. In truth, that API is meant to be used by a separate debugger process on a suspended process.

NtQuerySystemInformation (SystemProcessInformation, ...) should be used instead.

If you would like, I can provide you with a working implementation written in C.


The only way I was able to get DLSSTweaks working in Atomic Heart (Microsoft Store version, if it matters) was by disabling the SuspendThread (...), Get|SetThreadContext (...) and ResumeThread (...) calls.

I effectively gimped your thread freezing system, but deadlock goes away and hooks are successful despite multiple threads running.

I believe if you throw out the dependency on Toolhelp32 and snapshot using NtQuerySystemInformation instead, you can walk over these threads and safely suspend/resume.


Perhaps an INI option to forgo freezing and thawing threads during hook installation could serve as a temporary solution?

Kaldaien avatar Feb 24 '23 07:02 Kaldaien

This is pinned to an older safetyhook atm since latest seemed to cause problems with the LoadLibraryExW hook, looks like they did add some deadlock fixes there recently though, and the issue I had seems to have been fixed now too... I'll push an update to use that in a sec if you'd like to see if that helps at all.

(if it doesn't help there's also a PR at https://github.com/cursey/safetyhook/pull/17 which apparently helps with a different deadlock, not sure if it applies with the way I use it though but could be worth trying)

Looks like their ThreadFreezer code is still relying on toolhelp though, maybe worth bringing up over at their repo.

E: pushed the safetyhook update, here's a build with it too: dlsstweaks-0.123.12beta1a.zip

E2: hm, that build seems to make AH start to hang for me, the older safetyhook always worked fine though, odd.

E3: seem to have that fixed now, latest commit works fine for me with gamepass AH at least, posted build & more info at https://github.com/emoose/DLSSTweaks/issues/22#issuecomment-1443624026

emoose avatar Feb 24 '23 08:02 emoose

when launching atomic heart with your latest release OR with your .12-beta3, the game is freezing almost immediately.

am i missing something? playing with microsoft store version

snakeeater4526 avatar Feb 24 '23 17:02 snakeeater4526

@snakeeater4526 are you using SK or any other kind of DLL wrappers? Looks like there is some issue with deadlocking when used with SK here unfortunately, but don't really have a fix for that yet.

(gutting the ThreadFreezer code seems to stop the deadlock, but then doesn't look like any hooks here are actually active... but using the gutted ThreadFreezer version with SK removed does seem to let it work. I didn't try loading via SK's plugin system yet though, maybe that would help make the hooks here take effect, not sure)

Here's a build with the ThreadFreezer stuff removed anyway, maybe it can help even if you aren't using SK: dlsstweaks-0.123.12beta3a.zip

emoose avatar Feb 25 '23 08:02 emoose

@snakeeater4526 are you using SK or any other kind of DLL wrappers? Looks like there is some issue with deadlocking when used with SK here unfortunately, but don't really have a fix for that yet.

(gutting the ThreadFreezer code seems to stop the deadlock, but then doesn't look like any hooks here are actually active... keeping the gutted ThreadFreezer with SK removed does seem to let it work. I didn't try loading via SK's plugin system yet though, maybe that would help make the hooks here take effect, not sure)

Here's a build with the ThreadFreezer stuff removed anyway, maybe it can help even if you aren't using SK: dlsstweaks-0.123.12beta3a.zip

This fixed Hogwarts Legacy's freeze at launch for me. Thanks emoose 👍 And I'm using SK too.

SnowMaw avatar Feb 25 '23 12:02 SnowMaw

@SnowMaw could you check if your dlsstweaks.log file mentions this line inside it?

DLSS functions found & parameter hooks applied!

If it does then it should all be working fine, otherwise it might be having the issue I mentioned with hooks not being active.

btw how are you loading it in with SK? did you just rename the tweaks DLL or did you set it as a plugin?

emoose avatar Feb 25 '23 13:02 emoose

@SnowMaw could you check if your dlsstweaks.log file mentions this line inside it?

DLSS functions found & parameter hooks applied!

If it does then it should all be working fine, otherwise it might be having the issue I mentioned with hooks not being active.

btw how are you loading it in with SK? did you just rename the tweaks DLL or did you set it as a plugin?

Yeah I got that line: [2023-02-25 19:25:31.007] [info] DLSS functions found & parameter hooks applied! I'm using it as dxgi.dll and SK (global injection)

SnowMaw avatar Feb 25 '23 13:02 SnowMaw

@snakeeater4526 are you using SK or any other kind of DLL wrappers? Looks like there is some issue with deadlocking when used with SK here unfortunately, but don't really have a fix for that yet.

(gutting the ThreadFreezer code seems to stop the deadlock, but then doesn't look like any hooks here are actually active... but using the gutted ThreadFreezer version with SK removed does seem to let it work. I didn't try loading via SK's plugin system yet though, maybe that would help make the hooks here take effect, not sure)

Here's a build with the ThreadFreezer stuff removed anyway, maybe it can help even if you aren't using SK: dlsstweaks-0.123.12beta3a.zip

It's working with this special build you provided, i'm using no special software at all or injector that i'm aware. btw what is " SK " ?

snakeeater4526 avatar Feb 25 '23 17:02 snakeeater4526

I've gone ahead and put in an alternative way of freezing threads (https://github.com/cursey/safetyhook/pull/21). If you run into any issues with SafetyHook please let me know by creating an issue on my repo. I found out about this issue through a friend. I'm very motivated to make SafetyHook as reliable and easy to use as possible.

Thanks.

cursey avatar Feb 26 '23 08:02 cursey

@snakeeater4526 are you using SK or any other kind of DLL wrappers? Looks like there is some issue with deadlocking when used with SK here unfortunately, but don't really have a fix for that yet. (gutting the ThreadFreezer code seems to stop the deadlock, but then doesn't look like any hooks here are actually active... but using the gutted ThreadFreezer version with SK removed does seem to let it work. I didn't try loading via SK's plugin system yet though, maybe that would help make the hooks here take effect, not sure) Here's a build with the ThreadFreezer stuff removed anyway, maybe it can help even if you aren't using SK: dlsstweaks-0.123.12beta3a.zip

It's working with this special build you provided, i'm using no special software at all or injector that i'm aware. btw what is " SK " ?

SK is https://special-k.info/ I recommend to join his discord in case you need assistance > https://discord.gg/specialk

spajdrEX avatar Feb 26 '23 15:02 spajdrEX

I tryed dlsstweaks-0.123.12beta3a with hogwarts legacy. Here are my results:

  • It does not get stuck at the starting warning messages. 👍

  • The DLSS presets are selected correctly (using DLSS 3.1.1). 👍

  • The Ultra quality preset is selectable and seems to be working. 👍

  • Custom DLSS quality levels seem to not be working, they default to one of the predefined levels no matter what I choose in the "dlsstweaks.ini". 👎

I haven't been able to get the DLSS quality levels to work in this game with any of the dlsstweaks builds so far, and at this point I don't know if it is a problem with my system, the game, or your software.

jpgsantos avatar Feb 26 '23 15:02 jpgsantos

I tryed dlsstweaks-0.123.12beta3a with hogwarts legacy. Here are my results:

  • It does not get stuck at the starting warning messages. 👍
  • The DLSS presets are selected correctly (using DLSS 3.1.1). 👍
  • The Ultra quality preset is selectable and seems to be working. 👍
  • Custom DLSS quality levels seem to not be working, they default to one of the predefined levels no matter what I choose in the "dlsstweaks.ini". 👎

I haven't been able to get the DLSS quality levels to work in this game with any of the dlsstweaks builds so far, and at this point I don't know if it is a problem with my system, the game, or your software.

I'm using 0.123.11 with Hogwarts and overriding quality levels seems to work for me. I changed quality to 0.777778 and it now says 78% in the in-game render resolution settings (and fps is approximately between native and original 0.666667 quality). I have not enabled the ultra quality preset, I'm using the dlss 3.1.1v2 DLL and I'm not using any other injectors.

I'm not sure that this is the appropriate issue for this discussion though.

sam3000 avatar Feb 27 '23 06:02 sam3000

Are you able to override the quality levels for any of the non default ones? So something that is NOT 0.33333334, 0.5, 0.58, 0.66666667, 0.777778? I agree I should probably create a thread only for hogwarts legacy related problems.

jpgsantos avatar Feb 27 '23 13:02 jpgsantos

Are you able to override the quality levels for any of the non default ones? So something that is NOT 0.33333334, 0.5, 0.58, 0.66666667, 0.777778? I agree I should probably create a thread only for hogwarts legacy related problems.

I just tried quality at 0.72 and that worked fine (and, again, it showed 4k @ 72% resolution in the game render settings).

sam3000 avatar Feb 27 '23 17:02 sam3000