unoplatform/uno · error · NullReferenceException

Couldn't read the static field named _libraries in GLibrary

Error message

Couldn't read the static field named _libraries in GLibrary

What it means

Third link in the same GLibrary patch chain: the '_libraries' field is found but its static value is null at the time of the call, so field.GetValue(null) returns null and the code throws NullReferenceException. GLibrary._libraries is lazily populated; if the patch runs before WebKitGtk# has initialized that cache (or after a version changed initialization timing), the value is still null.

Source

Thrown at src/AddIns/Uno.UI.WebView.Skia.X11/X11NativeWebView.cs:103

			// Gtk# by default loads libwebkit2gtk-4.0 (+ libsoup-2.0), but we want libwebkit2gtk-4.1 (+ libsoup-3.0), so what
			// we do is that before WebKitGtk# makes its dlopen calls, we load libwebkit2gtk-4.1 and put it where the handle to
			// libwebkit2gtk-4.0 will be expected to be, so that when WebKitGtk# attempts to dlopen(libwebkit2gtk-4.0), it will
			// find the handle already there and will just use it instead.
			if (!NativeLibrary.TryLoad("libwebkit2gtk-4.1.so", typeof(X11NativeWebView).Assembly, DllImportSearchPath.UserDirectories, out var webkitgtk41handle))
			{
				if (typeof(X11NativeWebView).Log().IsEnabled(LogLevel.Error))
				{
					typeof(X11NativeWebView).Log().Error($"libwebkit2gtk-4.1 was not found. Attempting to call {nameof(ProcessNavigation)} with an {nameof(HttpRequestMessage)} instance will crash the process.");
				}
			}
			else
			{
				Assembly assembly = Assembly.Load("WebKitGtkSharp");
				Type glibraryType = assembly.GetType("GLibrary") ?? throw new NullReferenceException("Couldn't find GLibrary in WebKitGtkSharp");
				Type libraryType = assembly.GetType("Library") ?? throw new NullReferenceException("Couldn't find Library in WebKitGtkSharp");
				object webkitLibraryEnum = Enum.ToObject(libraryType, 11);
				FieldInfo field = glibraryType.GetField("_libraries", BindingFlags.NonPublic | BindingFlags.Static) ?? throw new NullReferenceException("Couldn't find a field named _libraries in GLibrary");
				object libraries = field.GetValue(null) ?? throw new NullReferenceException("Couldn't read the static field named _libraries in GLibrary");
				var setItemMethod = libraries.GetType().GetMethod("set_Item") ?? throw new NullReferenceException("Couldn't find a method named set_Item in _libraries");
				setItemMethod.Invoke(libraries, [webkitLibraryEnum, webkitgtk41handle]);
				_usingWebKit2Gtk41 = true;
			}

			if (!WebKit.Global.IsSupported)
			{
				_initException = new PlatformNotSupportedException("libwebkit2gtk-4.0 is not found. Make sure that WebKit2GTK 4.0 and GTK 3 are installed. See https://aka.platform.uno/webview2 for more information");
				return;
			}

			GLib.ExceptionManager.UnhandledException += args =>
			{
				if (typeof(X11NativeWebView).Log().IsEnabled(LogLevel.Error))
				{
					typeof(X11NativeWebView).Log().Error("GLib exception", args.ExceptionObject as Exception);
				}
			};

View on GitHub (pinned to 0418340488)

Solutions

  1. Touch a WebKitGtk# API before the patch to force GLibrary static initialization (e.g. reference WebKit.Global or a GTK type).
  2. Pin the WebKitGtkSharp version where _libraries is populated at type-init.
  3. Update the patch to initialize _libraries when null rather than throw.
Defensive patterns

Strategy: validation

Validate before calling

var f = asm.GetType("GLibrary")?.GetField("_libraries", BindingFlags.NonPublic | BindingFlags.Static);
bool populated = f?.GetValue(null) is not null;

Type guard

static bool GLibraryLibrariesPopulated(Assembly asm) { var f = asm.GetType("GLibrary")?.GetField("_libraries", BindingFlags.NonPublic | BindingFlags.Static); return f?.GetValue(null) is not null; }

Prevention

When it happens

Trigger: The '_libraries' static field exists but is null when patched — either because GLibrary static construction hasn't run yet (no WebKit type touched), or because a newer WebKitGtkSharp initializes the cache on first use rather than at type init.

Common situations: First navigation triggering the patch before any WebKitGtk# API has been touched; a WebKitGtkSharp version that defers _libraries population; running under a host that changed static-init ordering.

Related errors


AI-assisted analysis of unoplatform/uno@0418340488 (2026-08-13). Data as JSON: /api/errors/a8cc4f4e44a84b7c. Report an issue: GitHub.