Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unhandled Exception: System.Threading.ThreadStateException: #154

Open
4164696f73 opened this issue Sep 23, 2022 · 5 comments
Open

Unhandled Exception: System.Threading.ThreadStateException: #154

4164696f73 opened this issue Sep 23, 2022 · 5 comments

Comments

@4164696f73
Copy link

4164696f73 commented Sep 23, 2022

I've noticed that a program gives me this error (expanded below), and other programs that use .NET work as they should. Though I'm also not 100% sure if it's a Mono issue or not, as the program that spits this error is a modding tool from an old game that was never actually been fixed, and has many bugs anyways. Sending this report in case it's actually NOT the issue of the program itself, though.
The program worked as intended with .NET installled via Wine (needs .NET 4.0+, if that detail helps), but current version of Wine messes it up again (mscoree.dll not found).

Wine version: wine-7.16
winetricks --version: 20220411-next - sha256sum: 704cdd73c31f6b8492219a76c1b1d31aaa0ca8908177b09abde14707d1650657
wine-mono-7.3.0-1

System.Threading.ThreadStateException: Thread was in an invalid state for the operation being executed.
  at System.Threading.Thread.TrySetApartmentState (System.Threading.ApartmentState state) [0x0000a] in <e70d6e9587d64cb3abb4b3f99bbf5a0d>:0
  at System.Threading.Thread.set_ApartmentState (System.Threading.ApartmentState value) [0x00007] in <e70d6e9587d64cb3abb4b3f99bbf5a0d>:0
  at <Module>.WinMain (HINSTANCE__* hInstance, HINSTANCE__* hPrevInstance, System.SByte* lpCmdLine, System.Int32 nCmdShow) [0x00005] in <6511cfd9e67b4e36a3834c78a140cc36>:0
  at (wrapper native-to-managed) <Module>.WinMain(HINSTANCE__*,HINSTANCE__*,sbyte modopt(System.Runtime.CompilerServices.IsSignUnspecifiedByte)*,int)
  at (wrapper managed-to-native) <Module>._WinMainCRTStartup()
[ERROR] FATAL UNHANDLED EXCEPTION: System.Threading.ThreadStateException: Thread was in an invalid state for the operation being executed.
  at System.Threading.Thread.TrySetApartmentState (System.Threading.ApartmentState state) [0x0000a] in <e70d6e9587d64cb3abb4b3f99bbf5a0d>:0
  at System.Threading.Thread.set_ApartmentState (System.Threading.ApartmentState value) [0x00007] in <e70d6e9587d64cb3abb4b3f99bbf5a0d>:0
  at <Module>.WinMain (HINSTANCE__* hInstance, HINSTANCE__* hPrevInstance, System.SByte* lpCmdLine, System.Int32 nCmdShow) [0x00005] in <6511cfd9e67b4e36a3834c78a140cc36>:0
  at (wrapper native-to-managed) <Module>.WinMain(HINSTANCE__*,HINSTANCE__*,sbyte modopt(System.Runtime.CompilerServices.IsSignUnspecifiedByte)*,int)
003c:fixme:ver:GetCurrentPackageId (0068FEA4 00000000): stub```
@madewokherd
Copy link
Owner

It's probably an interaction between mixed-mode and managed APIs that we don't handle correctly.

Does the program in question have its source code posted, or is it publicly available? That could help with figuring out exactly what it's doing and making a test case.

If not, I could try setting Thread.ApartmentState directly inside WinMain in a mixed-mode program and see what happens.

@madewokherd
Copy link
Owner

I wrote a test for this, it works on Windows and fails in Wine Mono with a similar exception: madewokherd/wine-mono-vstests@b23ef43

@madewokherd
Copy link
Owner

This is really interesting. Mono's version of TrySetApartmentState throws this exception if the thread is running. So it's not possible in Mono to set the thread state on the currently thread. https://github.com/madewokherd/mono/blob/develop/mcs/class/corlib/System.Threading/Thread.cs#L682

There's also a test for it, which passes on Windows: https://github.com/madewokherd/mono/blob/develop/mcs/class/corlib/Test/System.Threading/ThreadTest.cs#L1291

The other test works in .NET Framework, so there must be some circumstance where you can set the state of a running thread. Hypotheses:

  • Threads started by unmanaged code are special, and their apartment state can be set after they are started.
  • The apartment state of a currently-running thread can only be set from inside that thread. This would make sense, because it needs to call a Windows API function to do this. Of course, it'd also require a special case to make it work.

@madewokherd
Copy link
Owner

Testing in managed code shows that new threads started by managed code have a thread state of MTA. Calling TrySetApartmentState fails by returning false rather than throwing an exception.

@madewokherd
Copy link
Owner

It should be fixed by this commit: 6fae2df

If you want to retest before that makes it into a release, you can try a build from the "Artifacts" section near the bottom of this page: https://github.com/madewokherd/wine-mono/actions/runs/3555468427

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 a pull request may close this issue.

2 participants