From: Fabio Manganiello <fabio@manganiello.tech>
Date: Tue, 15 Jul 2025 15:41:47 +0200
Subject: soup-init: Use libdl instead of gmodule in `soup2_is_loaded` check

Calling `g_module_open` in the library constructor can cause deadlocks
when libsoup is used with other libraries that also contend for GLib
mutexes. `dlopen` should be used instead.

Co-authored-by: Nirbheek Chauhan <nirbheek@centricular.com>
Bug: https://gitlab.gnome.org/GNOME/libsoup/-/issues/463
Bug: https://gitlab.gnome.org/GNOME/glib/-/issues/1443
Bug-Debian: https://bugs.debian.org/1109685
Origin: https://gitlab.gnome.org/GNOME/libsoup/-/merge_requests/475
Applied-upstream: 3.7.0, commit:1296cbf983f036f20262c453926dff77e1d6a852
Applied-upstream: 3.6.6, commit:2316e56a5502ac4c41ef4ff56a3266e680aca129
---
 libsoup/soup-init.c | 28 +++++++++++++++++-----------
 1 file changed, 17 insertions(+), 11 deletions(-)

diff --git a/libsoup/soup-init.c b/libsoup/soup-init.c
index 8a33c77..3392e8e 100644
--- a/libsoup/soup-init.c
+++ b/libsoup/soup-init.c
@@ -10,7 +10,6 @@
 #endif
 
 #include <glib/gi18n-lib.h>
-#include <gmodule.h>
 #include "gconstructor.h"
 
 #ifdef G_OS_WIN32
@@ -18,21 +17,28 @@
 #include <windows.h>
 
 HMODULE soup_dll;
+#else
+#include <dlfcn.h>
 #endif
 
 static gboolean
 soup2_is_loaded (void)
 {
-    GModule *module = g_module_open (NULL, 0);
-    gpointer func;
-    gboolean result = FALSE;
-
-    if (g_module_symbol (module, "soup_uri_new", &func))
-        result = TRUE;
-
-    g_module_close (module);
-
-    return result;
+	gboolean result = FALSE;
+
+	/* Skip on PE/COFF, as it doesn't have a flat symbol namespace */
+#ifndef G_OS_WIN32
+	gpointer handle;
+	gpointer func;
+
+	handle = dlopen (NULL, RTLD_LAZY | RTLD_GLOBAL);
+	if (handle != NULL) {
+		func = dlsym (handle, "soup_uri_new");
+		result = (func != NULL);
+		dlclose (handle);
+	}
+#endif
+	return result;
 }
 
 static void
