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

Crash with glib #1471

Closed
ccremer opened this issue Oct 27, 2023 · 6 comments
Closed

Crash with glib #1471

ccremer opened this issue Oct 27, 2023 · 6 comments

Comments

@ccremer
Copy link

ccremer commented Oct 27, 2023

Magick.NET version

"Magick.NET-Q8-x64" Version="13.4.0"

Environment (Operating system, version and so on)

Linux (Mint 21.2 (Ubuntu-Based))

Description

When running in multi-threaded context (e.g. ASPNET Core), using MagickNET is hard crashing when another process is depending on glib (I think).

As described in https://rachelbythebay.com/w/2017/01/30/env/ and in glib manual:

Modifications of environment variables are not allowed in multi-threaded programs.

The app crashes particularly when reaching Environment.SetEnv("MAGICK_TEMPORARY_PATH", TemporaryDirectory);.

Here's a log excerpt:

info: WebApplication1.MagicNetInitialization[0]
      Initializing MagickNet /home/chris/source/scratchpad/TestProject1/WebApplication1
**
GLib-GObject:ERROR:../gobject/gvaluetypes.c:455:_g_value_types_init: assertion failed: (type == G_TYPE_CHAR)
Bail out! GLib-GObject:ERROR:../gobject/gvaluetypes.c:455:_g_value_types_init: assertion failed: (type == G_TYPE_CHAR)

Steps to Reproduce

https://gist.github.com/ccremer/ced17eb8c177003bd81ba727c2f5bda7

Included in the gist is IronPDF, a library to render PDFs. Without this library, MagickNET doesn't crash, but since IronPDF depends on Chrome, and Chrome itself on glib as well, the program will crash when you start it up.

However, if you swap the order of the boot from

builder.Services.AddHostedService<IronPdfInitialization>();
builder.Services.AddHostedService<MagicNetInitialization>();

to

builder.Services.AddHostedService<MagicNetInitialization>();
builder.Services.AddHostedService<IronPdfInitialization>();

then it works.

Still, calling setenv is forbidden in this context.
Is it possible we can skip this and setup the required environment variables by ourselves, without MagickNET trying to do it again?

@dlemstra
Copy link
Owner

Your error message looks like this one: #1126. And I really don't understand how you can to the conclusion that this is caused by setenv?

@ccremer
Copy link
Author

ccremer commented Oct 30, 2023

Apologies for not being detailed enough.
The error pops when the code (in the gist) runs to MagickNET.SetTempDirectory(tmpPath); and then Environment.SetEnv("MAGICK_TEMPORARY_PATH", TemporaryDirectory);.

It also isn't a race condition. I can set a breakpoint or delay and it still crashes when calling Environment.SetEnv("MAGICK_TEMPORARY_PATH", TemporaryDirectory); after IronPDF has loaded the chrome libraries.

Swapping the order of MagickNET and IronPDF initialization only delays the problem. While the crash might be avoided, it still fails as described in #1472 .

I'll try to figure out my options...

@ccremer
Copy link
Author

ccremer commented Oct 30, 2023

If I exclude the call to MagickNET.SetTemporaryDirectory, then later the crash happens in constructor new MagickImage(), particularly this line:

Instance = NativeMethods.X64.MagickSettings_Create();

@ccremer
Copy link
Author

ccremer commented Oct 30, 2023

Maybe related to this, I've noticed that IronPDF comes with a number of other dependencies besides Chrome:

BitMiracle.LibTiff.NET.dll
SixLabors.Fonts.dll
SixLabors.ImageSharp.dll
SixLabors.ImageSharp.Drawing.dll

Could there be any incompatibilities?

@dlemstra
Copy link
Owner

Could we close one of the two issues because this all seems to be related to IronPDF?

p.s. Would you be willing to share the versions of the SixLabors libraries?

@ccremer ccremer changed the title Crash with glib when calling native setenv in multi-threaded environment Crash with glib Oct 31, 2023
@ccremer
Copy link
Author

ccremer commented Oct 31, 2023

Yeah, it seems so, but I'm going to close both as it's not reproducible in Docker, as mentioned in #1472

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

No branches or pull requests

2 participants