getsentry / sentry-unity

Development of Sentry SDK for Unity
https://docs.sentry.io/platforms/unity/
MIT License
200 stars 51 forks source link

System.DllNotFoundException caused by dynamically linked CRT #1440

Open CliffCawley opened 10 months ago

CliffCawley commented 10 months ago

Environment

How do you use Sentry? sentry.io through Unity

Which version of the SDK? 1.5.1

How did you install the package? (Git-URL, Assetstore) Unity Package Manager

Which version of Unity? 2022.3.8f1

Is this happening in Unity (editor) or on a player like Android, iOS, Windows? Player, Windows 10 IL2CPP Production build

Steps to Reproduce

Difficult to reproduce, because this is happening in the wild, but the issue seems to be because you're compiling sentry.dll with the dynamic CRT linked in. This means we're required to ensure that the user has the correct CRT runtimes installed too.

Would be great if it was possible to statically link the CRT instead, so that we don't need additional runtimes installed:

Using depends you can see the currently dynamically linked dlls (see below)

On systems that don't have this installed, but run our IL2CPP Unity game just fine, we experience the System.DllNotFoundException

Expected Result

No exception

Actual Result

System.DllNotFoundException Unable to load DLL 'sentry'. Tried the load the following dynamic libraries: Unable to load dynamic library 'sentry' because of 'Failed to open the requested dynamic library (0x06000000) - The specified module could not be found. (WinError:0000007e)

Any logs or screenshots

image

bitsandfoxes commented 10 months ago

Hey, sorry to see you run into issues. This is the first time this has come up. I'll have to look into that. Do you know anything more about those environments this is happening on?

CliffCawley commented 9 months ago

Hey, sorry to see you run into issues. This is the first time this has come up. I'll have to look into that. Do you know anything more about those environments this is happening on?

90% are from Windows 10.0.19045 4% from Windows 10 3% from Windows 11

It seems to be a mix of machines and, various languages.

Some devices that appear: HP Pavilion Power Desktop 580-1xx (HP) X555LJ (ASUSTeK COMPUTER INC.) 20LTS2SW00 (LENOVO)

The issue here is that sentry is trying to reference dlls that don't exist on those systems, since sentry is linking to a specific version of the libraries.

I believe the fix is simple: statically link to the libraries, instead of dynamically link.

CliffCawley commented 9 months ago

Just to add to this, it looks like you have a dependency on the native sentry library, and the docs there have the following:

The following options can be set when running the cmake generator, for example using cmake -D BUILD_SHARED_LIBS=OFF ... SENTRY_BUILD_SHARED_LIBS (Default: ON): By default, sentry is built as a shared library. Setting this option to OFF will build sentry as a static library instead. If sentry is used as a subdirectory of another project, the value BUILD_SHARED_LIBS will be inherited by default. When using sentry as a static library, make sure to #define SENTRY_BUILD_STATIC 1 before including the sentry header.

So the built sentry dll that you're relying on, has been built as a shared library. You'll want to build it with SENTRY_BUILD_SHARED_LIBS set to OFF so that it's built as a static library instead.

I think that will solve it, but I could be wrong

bitsandfoxes commented 9 months ago

Hey, thanks for providing all the extra context. Just to confirm: This is caused by sentry-native trying to capture a native crash? Do you maybe have a callstack for the System.DllNotFoundException?

CliffCawley commented 8 months ago

@bitsandfoxes Sorry for the slow reply, it seems my github notifications haven't been working, should be fixed now.

This is using the Unity sentry library (This github).

The callstack appears to be:

System.DllNotFoundException: Unable to load DLL 'sentry'. Tried the load the following dynamic libraries: Unable to load dynamic library 'sentry' because of 'Failed to open the requested dynamic library (0x06000000) - The specified module could not be found.  (WinError:0000007e)
  Module "Sentry.Unity.Native.SentryNativeBridge", in Init
  Module "Sentry.Unity.Native.SentryNative", in Configure
  Module "Sentry.Unity.SentryInitialization", in Init

The Unity log reveals only that it was called immediately after the app loads:

image

stephanie-anderson commented 8 months ago

Thanks for the details, @CliffCawley - we'll have a look at this by the end of this week.

bitsandfoxes commented 8 months ago

I think that's a fair request. Especially on desktop platforms the increase in size should be alright? I'll make that work.

CliffCawley commented 8 months ago

Thanks @bitsandfoxes, that would be great!

jarek-p commented 3 weeks ago

Is there any progress with that feature? I believe this is the same as [https://github.com/getsentry/sentry-unity/issues/1647] We're also seeing this error on our unity linux server builds, every time they're initialized - is there any workaround to at least silence the error till this gets resolved?

Using v2.0.7 (We also tried disabling sentry native support on all platforms but it didn't help)

bitsandfoxes commented 3 weeks ago

Initially, I thought we could just switch to compiling sentry-native with SENTRY_BUILD_SHARED_LIBS=OFF and that'd be it. But in reality, we'd need to change the way the game gets compiled. In .NET with native AOT there's a built-in MSBuild task taking care of that so I'm not sure how'd we even do that in Unity.

bitsandfoxes commented 3 weeks ago

is there any workaround to at least silence the error till this gets resolved?

I'll wrap this so it just logs instead of breaks!

jarek-p commented 3 weeks ago

Thanks for the reply and looking into it!